Published : 2013-08-19

Sender/recipient rewriting in Postfix

Postfix allows you to rewrite the senders and recipients of a mail. This feature is extremely handy if you have a local domain for sending mails (such as servers that send mails under their own name).

This feature uses canonical maps. These can be applied on sending, on receiving, or both.

Let’s see how to proceed with 3 simple examples.

Sender change

Assume you have a user root@monserveur.lan. To go through some SMTPs, this address must be valid, but your TLD is local. We will rewrite the address with a canonical map to make it appear as root-serveur@example.org.

Add the following line in your /etc/postfix/main.cf file:

sender_canonical_maps = hash:/etc/postfix/sender_canonical

Insert the following line in the /etc/postfix/sender_canonical file:

root@monserveur.lan root-serveur@example.org

Finally reload the map.

postmap /etc/postfix/sender_canonical

Recipient change

You want messages addressed to hostmaster@dns1.example.org to be addressed to dns-master@example.org. We will create a canonical map for this.

Add the following line in /etc/postfix/main.cf:

recipient_canonical_maps = hash:/etc/postfix/recipient_canonical

Insert the following line in the /etc/postfix/recipient_canonical file:

hostmaster@dns1.example.org dns-master@example.org

Finally reload the map.

postmap /etc/postfix/recipient_canonical

Change a domain name

In some cases it may be convenient to rewrite all addresses of a given domain name.

We will create a generic canonical map (sending + receiving) to replace domain.lan with example.org.

Add the following line in /etc/postfix/main.cf:

canonical_maps = hash:/etc/postfix/canonical

Insert the following line in the /etc/postfix/canonical file:

@domain.lan @example.org

Finally regenerate the map.

postmap /etc/postfix/canonical

Conclusion

You now know how to rewrite addresses going through your relay. Note however that this only applies to the next mails going through your relay. You will need to re-run the queue if you want your change to apply to the current queue (postsuper -r ALL).