I am trying to add a custom header to all messages handled by my postfix server.
I tried using a guide on the internet without any luck.
In my master.cf i added:
-o smtp_header_checks=regexp:/etc/postfix/test_tag_header
test_tag_header: Prepend X-Mailgun-Tag: testing123
made sure I restarted postfix
I did a few tests to yahoo mail which has a full headers option.
The headers i added did not show up.
My goal is to add a custom header with a random value so that I can later parse the message using mailgun.org's api.
X-Mailgun-Tag: randomvalue (similar to url shortener)
Based upon the answer from ank this is what i have so far:
main.cf
smtp_recipient_restrictions = check_recipient_access
proxy:mysql:/etc/postfix/mailgun_header.mysql
mailgun_header.mysql
user = vmail
password = mypass
hosts = 127.0.0.1
port = 3306
dbname = vmail
query = select CONCAT(prefix, MD5(RAND())) as 'check_recipient_access' from mailgun_tokens where prefixID = 1;
when i try to restart postfix i get thef ollowing errors:
/usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter:
smtp_recipient_restrictions=check_recipient_access
proxy:mysql:/etc/postfix/mailgun_header.mysql
*
The sql query works fine through MYSQL and returns a row with a random number.
Best Answer
Here is the solution that worked for me. I am now able to successfully track each individual message sent through our postfix serves using MailGun.org as our relay.
Please not this is a simple install of Postfix / Dovecot using iRedmail.org install script for debian /ubuntu. Your own postfix configuration may be different.
Main.cf
/etc/postfix/mailgun-header.cf
MySQL Requirements:
You pretty much need to create a table called mailgun_tokens in your vmail database. If your database name is different make sure to account for that. The table is simple with only two columns. One prefix (varchar 100), and one called prefixId int(1). It will only have one record. You can get more fancy in a multi domain setup and create a record for each domain or even user.
Here is the basic code to get your mysql setup to continue with the code samples above:
Last Step: Restart Posftfix (service postfix restart)
Once you get everything up and running, postfix will now add a custom header X-Mailgun-Variables with the appropriate JSON formatted string. If you have configured mailgun to post to your application for Message Read notifications, and link click notifications then Mailgun will post the custom-msg-id to your application also so that you can lookup the email.
Couple this with logging all messages to your DB, and you could easily create an application, crm, webmail which keeps track of messages once they have left your mailbox. Read Notifications for email.