Tuesday, February 26, 2013

How to add custom header to outgoing messages

I'll demonstrate how to add headers to an email in Postfix. If you want to ONLY add custom header to all messages you might use ACCESS and PREPEND, see Postfix documentation for details.

But what if we assume that our custom header must be added only to outgoing messages and only if there is no one already? Unfortunately you cannot do this with bare metal MTA.

The best and easiest solution is to use MSH Traffic Policy milter.

Our first condition is to check if there is already a header in the message. Message Header condition will scan message for our X-Header. It have four options for matching headers - we need not have Header condition type.

Second conditions is to match only outgoing messages. If your domain is local.net then insert *@local.net value into Message Sender condition. This will filter only messages with internal sender.
The last element is to check if All conditions must met option is selected.

We have defined conditions, now it is time for actions. Add message header is what we looking for.
After these steps we have configured rule which will: Add custom header to all outgoing messages, but only when header does not exists.

Learn more about MSH Traffic Policy.