Forwarding e-mails with Postfix on Debian

by on

My personal e-mail address is managed by Google for Work, as the spam filtering in Gmail is better than anything I could do myself. The problem is that I hava a few additional domains that don’t have any e-mail users. I have to use an external forwarding service as my grandfathered Google account doesn’t allow me to add more domains. Fortunately setting up Postfix to forward mail from my other domains was pretty easy.

Forwarding an e-mail to another address.

This also works if you’re using a normal hosted webmail service (gmail, hotmail, yahoo…) and want to receive e-mail from your own domain straight to your usual inbox. This allows you to use your favorite e-mail service yet look a bit more professional.

The Postfix server in my example receives an e-mail adressed to email@example.com and forwards that to vic@demuzere.be. Note that this breaks SPF but keeps DKIM signatures intact. An intact DKIM signature is enough to pass DMARC validation.

Installing Postfix

Postfix is included in the Debian repositories. Exim is installed by default and will be removed when you attempt to install Postfix. Unless you’re already using e-mail on the server this shouldn’t be an issue.

apt-get install postfix

When the configuration dialog pops up, simply select No configuration.

Postfix configuration

The relevant configuration is located in /etc/postfix/main.cf.

Define for which domains you want to forward e-mail. Note that you can specify multiple virtual domains. We let Postfix know that our address mappings will be listed in the virtual file inside the configuration directory.

virtual_alias_domains = example.com example.net
virtual_alias_maps = hash:/etc/postfix/virtual

Now that postfix knows for which domains to accept e-mail, we can start creating the individual addresses inside the virtual file. The syntax is a simple key-value pair list. Note that it’s also possible to define more complex regex-based addresses, but I didn’t need that for my simple setup.

email@example.com vic@demuzere.be
other@example.com destination@gmail.com
third@example.net hello@hotmail.com

In case you want to forward all e-mail (catch-all) for a certain domain, simply leave away the user part. If you’re setting both specific users and a catch-all address, the latter will only work for unknown users.

@example.com destination@gmail.com

Save the changes by running postmap on the virtual file.

postmap /etc/postfix/virtual

Issues when delivering to the local domain

In my case, the destination e-mail address is on my own domain. Even though e-mail for the domain is hosted elsewhere, Postfix thought it was responsible and kept the e-mail instead of forwarding it. This was solved by setting the domain for the Postfix server to a subdomain that I don’t actually use.

myhostname = myserver.demuzere.be
mydomain = myserver.demuzere.be
myorigin = myserver.demuzere.be

Note that this solution only works if you’re using the Postfix server to forward to external services. In case you’re using the server for actual mailboxes, watch out when modifying these settings!