Quick Postfix setup for email forwarding

Quick Postfix setup for email forwarding

In this article, I’ll present a very basic and quick Postfix setup process for those interesting in email forwarding. This might come in handy if you want to create personal email addresses for your team, but don’t want full inboxes stored on your server. Because Debian is the most widely used distribution for web servers (or is it CentOS? Who knows…), this article will use apt-get and other Debian-related utilities. Configuration is mainly distribution-independent, you just need to adapt the utilities on other distros (pacman, aptitude, yum, and so on…)

Email forwarding

As I like the Wikipedia definition, allow me to quote it :

Email forwarding generically refers to the operation of re-sending an email message delivered to one email address on to a possibly different email address. – Wikipedia

Mapping addresses

Mapping is the key operation. When you associate an address, let’s say me@mydomain.tld to your actual inbox (something like me@gmail.com), you’re mapping the source address to its destination. Postfix configuration will basically be about setting up these mappings.


Bring Postfix packages

First of all, you’ll need to bring Postfix onto your system. Easy enough :

Choose Internet site, and type your domain in. I would suggest not using www in the domain name. apt-get will take care of the rest for you.

Add mail exchange entry to your domain zones

Another important step : you need to setup your domain to permit mail exchanges to take place. This can be done on your registrar panel. Find a way to edit your DNS zones (available with most registrars), and add the following zones :

On a UNIX system, you can use the dig utility to test your records :


Basic Postfix settings

Now, setting Postfix up can be a tricky thing. Here are the key settings for me, make sure your configuration includes them.

An empty relayhost parameter is very important (except in specific cases of course). You don’t want your SMTP server to become an open relay (see the Abuse by spammers” section), trust me…

Mapping-related settings

Let’s get down to business. Mapping forwarding addresses requires two steps :

  1. Create a file containing mappings themselves : sourceaddress@domain.tld yourinbox@gmail.com.
  2. Specify the path to this file in the main configuration.

This first one is easy. First, create the file (common configurations use /etc/postfix/virtual) :

Easy enough right ? Now, you need to create the associated .db file for it, even easier :

Now, /etc/postfix/virtual.db should be available. First step is complete, back to your main.cf file. We need to add a relation to the first configuration file. This is done using the virtual_alias_maps parameter :

Guess what ? You’re done ! Just restart Postfix, and forwarding should be operational. Now, for a few other tricks, let’s get to the next section.

More tricks

Create a noreply (blackhole) address

If your web applications send emails, you may need a noreply address as a sender. Emails sent to this address should be dropped by your server, and Postfix can take care of this for you. First, you need to create a fake user, an alias to /dev/null. For cultural reasons, I like to call this user davenull. Open your /etc/aliases file, and add the following line :

Now, you need to create an email address for this fake user. We’ll just use our previous setup, and create an additional address in /etc/postfix/virtual :

Don’t forget to run postmap again, and restart your Postfix server. Your noreply address is now recognised by your SMTP server!

Use MySQL instead of the maps file

If you want a dynamic configuration for your email forwarding addresses, you can use MySQL as a backend, and store them directly into a database. Now that’s a little trickier : first of all, you need the postfix-mysql package :

This should go quite fluently. Now, let’s create the appropriate tables (you need to store both domains and addresses) :

Before going any further, you may want to fill those tables. Add your domain names into virtual_domains, and link addresses to it in virtual_aliases. Take care of the relation between the two tables when adding entries.

Now, you need to link Postfix to the database. This takes place in three files :

  1. virtual-domains.cf configures the relation between Postfix and the virtual_domains table.
  2. virtual-aliases.cf configures the relation between Postfix and the virtual_aliases table.
  3. main.cf links Postfix to the two previous files.

The first two ones are quite self-explanatory, just have a look :

About the query parameter : %s will be replaced by the address’ domain when a forwarding request is made. The other parameters are quite obvious, just make sure to grant proper privileges to yourmysqluser. Another important note : this query doesn’t return an actual entry field value, but will output 1 if your domain is found : that’s fairly enough after all, we just want to check whether you accept this domain or not. Now about virtual-aliases.cf :

Same mechanism. Here, %s will be replaced by the source address (me@yourdomain.tld). As you can see, this query returns the destination address associated with a source one. The SQL structure remains quite easy to understand. Now, just associate these two files to Postfix, in main.cf :

Don’t miss the mysql: prefix which tells Postfix where to find its data. If you don’t use it, Postfix will search for the contents directly into the files, which isn’t what we want. By the way, this prefix is basically the reason why we installed the postfix-mysql package. Anyway, now you should be able to restart your SMTP server, and add additional entries into your tables. You’re done!

Well, I think we went far enough for a single article. See you later!