Take the 2-minute tour ×

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


user        = vmail
password    = mypass
hosts       =
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.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.


smtpd_recipient_restrictions = check_recipient_access mysql:/etc/postfix/mailgun-header.cf, reject_unknown_sender_domain, reject_unknown_recipient_domain, reject_non_fqdn_sender, reject_non_fqdn_recipient$



user        = vmail
password    = PutYourPasswordHere (in iRedMail tips if you used iRedmail to setup)
hosts       =
port        = 3306
dbname      = vmail
query       = Select CONCAT(prefix, '{"custom-msg-id": ','"',MD5(RAND()),'"','}') as 'check_recipient_access' from mailgun_tokens where prefixID = 1;

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:

-- Table structure for `mailgun_tokens`
DROP TABLE IF EXISTS `mailgun_tokens`;
CREATE TABLE `mailgun_tokens` (
  `prefix` varchar(100) DEFAULT NULL,
  `prefixID` int(1) unsigned zerofill NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`prefixID`)

-- Records of mailgun_tokens
INSERT INTO `mailgun_tokens` VALUES ('PREPEND X-Mailgun-Variables: ', '1');

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.

share|improve this answer
add comment

Instead of messing around with master.cf (I rarely do that) try the following in your main.cf

smtp_recipient_restrictions = (...), check_recipient_access <type>:<table>, (...), permit

where in the table have the following

PREPEND X-Mailgun-Tag: testing123

Look at this example also for more hints.

share|improve this answer
add comment

Your Answer


