Add a custom header to Postfix with the relayed domain

I've got a Postfix setup that allows relays only from certain domains. I accomplish this using the relay_domains flag in main.cf and it queries mysql to find the list of allowed domains.

relay_domains = mysql:/etc/postfix/mysql_domains.cf

I would like to add a header_checks instruction to insert a custom header based on the result of the mysql check for relay_domains which would show which domain was relayed through Postfix. The reason for this is that if the allowed domain is .domain.com and the recipient email address is [email protected], I'd like to know which .domain.com Postfix resolved to in the lookup.

I've got the header_checks working:

/^From:/i PREPEND X-Relay-Domain: xxx

But I'm not sure how to capture the result of the domain query and use that for the header_checks PREPEND. I could also perform another mysql lookup on header_checks but I'd like to avoid that if possible.


Solution 1:

Perhaps, you can use check_recipient_access from postfix.

smtpd_recipient_restriction = ... 
                      ... 
                      check_recipient_access pcre:/etc/postfix/addheader
                      ...

File /etc/postfix/addheader has content like this

/.+@(.+)/  PREPEND X-Relay-Domain: $1

In access table, PREPEND action will add header in your email. In this case, this map will add domain parts of your recipient.