Prepend X-header to all relayed messages in Postfix
I've got several production servers running a LAMP stack. They each have a local Postfix server catching any mail from the system and from PHP, and relaying it via a smarthost (the SendGrid SMTP service).
I'd like to add a custom header to every outgoing message sent to the smarthost. This allows me to filter statistics per server in SendGrid. Something like:
X-SMTPAPI: {"category": "www1"}
The Postfix docs mention using the PREPEND action in a Postfix 'access' table. So, I added the following line to /etc/postfix/access
:
PREPEND X-SMTPAPI: {"category": "www1"}
and hashed the access
file with postmap.
However, I have no idea how to use the map. Something like the following doesn't work:
smtp_client_restrictions = check_client_access hash:/etc/postfix/access
How do I make Postfix prepend this header?
Solution 1:
This answers your exact question: https://web.archive.org/web/20150706131729/http://hoursofop.tumblr.com/post/17760274650
Quick steps reported here:
-
create a file /etc/postfix/sendgrid_headers and add this line to it:
/^From:/ PREPEND X-SMTPAPI: {“category” : “Category Name”}
-
update your master.cf file with the following lines:
smtp unix - - n - - smtp -o smtp_header_checks=regexp:/etc/postfix/sendgrid_header
It applies to a Ubuntu system and worked perfectly for me. Be careful to choose the right "smtp" line in master.cf. I used a tab to indent the -o
line.
Also note that SendGrid strips out the X-SMTPAPI
header from the email before it is sent on - so you won't find it there but you will see the category appear within the SendGrid dashboard.
Solution 2:
You seem to have mis-spelled header_checks
as smtp_client_restrictions
, which isn't even the correct spelling of the wrong parameter. ☺
This sort of thing is far better done with a simple shim around sendmail
, that your PHP (or whatever) scripts are configured to use, you know. The shim script would be a simple exercise in the use of the cat
and echo
commands. The MTS is really the wrong place to be doing this.