Postfix Add Custom Header Based on MySQL Result

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: http://saaboke.com/?p=22, but I do not know how to incorporate that with MySQL. Any suggestions would be greatly appreciated.

Thanks for your time.


Solution 1:

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.