Postfix Domain Rewriting

Posted on Wednesday May 4th, 2016

Following on from my previous postfix post, this configuration details how to rewrite the domain for addresses sent to the mail server for sending onwards.

The main entries to go in main.cf are:

# The domains that the server will accept mail for
relay_domains = $myhostname, hash:/etc/postfix/relay_domains
# The rewrite mapping for each domain
canonical_maps = hash:/etc/postfix/canonical

relay_domains details all the domains that the server will accept mail for, including ones that are to be rewritten. A sample entry is as follows:

example1.com    OK

and a sample entry of the canonical file:

@example1.com   @domain1.com

detailing that mail to example1.com will be rewritten to domain1.com.

As usual, the two files need to be converted to their db counterparts:

$ postmap relay_domains
$ postmap canonical

Restart postfix for the changes to take place.

Credit where credit is due: https://www.oostergo.net/node/89