Simple mail forwarding using Postfix

published on in category Linux FreeBSD , Tags: FreeBSD Linux Debian Ubuntu Mail Postfix selfhosted

Table of contents

It’s more than likely that your email provider of choice, especially the ones that offer mail services free of charge, will not support receiving email to custom domain names like, in my case, davd.net. Running your own mail server would solve this problem but running a fully featured mail stack including POP, IMAP, Sieve filters et cetera requires a fairly powerful machine. Additionally, if not configured properly, there’s big potential for abuse, e.g. spam.

As an alternative, it’s possible to just run a MTA which redirects all incoming email to an external mail server. This can be ran on almost any machine, even on a low-budget computer like the Raspberry Pi or a cheap virtual server.

The following instruction works for both Linux and FreeBSD systems. For the sake of simplicity I use the Debian GNU/Linux file names and directories. When using FreeBSD, just replace apt-get with pkg and prepend /usr/local to every /etc/postfix path, everything else will work just fine.

I presume that you already set up a proper MX record for your domain. Additionally, your servers’ hostname should match the mail servers’ domain name, say mail.davd.net, as well as the reverse DNS record for your IP address.

First, we’ll install Postfix:

apt-get install postfix

Now append the following lines to /etc/postfix/main.cf, all other settings may remain for the most Linux distributions:

virtual_alias_domains = domain1.tld domain2.tld domain3.tld
virtual_alias_maps = hash:/etc/postfix/virtual

The referred file /etc/postfix/virtual holds the information, which source addresses or domains should be redirected to which target address.

Just create that file and specify your redirects as follows:

Simple redirect:

me@domain1.tld target@zieldomain.tld

Catch-all redirect:

@domain2.tld target@target-domain.tld

Note that by any change of this file, the lookup table needs to be recreated. This can be accomplished by running the following command:

postmap /etc/postfix/virtual

Now (re)start the Postfix service. On FreeBSD you’ll need to enable it first within the rc.conf file.

service postfix restart

From this point, email forwarding should work as intended. I’d recommend checking /var/log/mail[.]log for any abnormality. Additionally, you should consider running a MTA security check, like http://emailsecuritygrader.com/ or http://mxtoolbox.com/diagnostic.aspx. This is particularly important if you don’t trust your distributions’ default Postfix configuration.