Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a quick question about accessing a MySQL database via Postfix and adding a custom header to an email based on the result. So we have setup an Ubuntu server with Postfix which is using a MySQL DB for virtual addresses, now my question is this: Is it possible to lookup an address in the virtual addresses table and if it matches the search criteria, add a custom Header to the email? We would also need this to happen when the email arrives in case there are any forwarders attached to the email account.

I have found resources on how to add custom headers via Postfix:, but I do not know how to incorporate that with MySQL. Any suggestions would be greatly appreciated.

Thanks for your time.

share|improve this question
Please add more information. Do you want the header to be a result of a virtual address lookup? Or want to do a mysql query based on the recipient and then add the result to the header? – mailq Aug 18 '11 at 18:52
Hey mailq, sorry I want to add Header information based on the recipient. So if the receipents record is found in the MySQL query (with specific search criteria) result it will add custom headers to the email. If a record is not found then it won't add anything. – thiesdiggity Aug 18 '11 at 19:36
up vote 4 down vote accepted

That's an easy one. You need something to add to the

 smtp_recipient_restrictions = (...), check_recipient_access mysql:/etc/postfix/header_based_on_recipient.mysql, (...), permit

The /etc/postfix/header_based_on_recipient.mysql must contain everything needed to query mysql as described here. I can't give you a template as it depends on what you are trying to query.

The result of the query must be blank to add no header. Or the result of the query must be

PREPEND X-fancy-new-header: Found you in the database

If you need more than one header, then this is not applicable. In this case you should consider using the MILTER interface with a self-made milter. Doing such extended stuff is not the job of Postfix and is delegated to external programs as Postfix is a mailer and not a message rewriter.

I'm sorry - as a Postfix lover - to recommend Exim for extended message manipulation as such things are built-in to Exim.

share|improve this answer
Hey mailq, thanks for the answer! Unfortnately I'm required to add a couple custom headers to the email. I will look into creating a milter. – thiesdiggity Aug 19 '11 at 16:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.