Tested on:

Supporting multiple domains with Postfix

—by Joel Rennich, mactroll@afp548.com

13 November 2003

One of the features of Postfix that Server Admin won’t let you control is virtual domains. Think of these much like virtual hosts in Apache: they allow you to have multiple, distinct e-mail domains on the same server.

For example, with Server Admin you could set up your mail server to be authoritative for both “afp548.com” and “example.com.” If you created a user with a short name of “joel” that user would get all mail for joel@afp548.com and joel@example.com. Sometimes you want this, and sometimes you don’t. Virtual domains will allow you to have to separate “joel” users. One for each domain that you have.

If you have visions of being an e-mail hoster for a number of sites, the method I’m going to describe isn’t for you. You should look into using MySQL as a backend for all of this. However, if you plan on hosting a dozen or two different domains with not too many users in each domain, you can set this up with a minimum of effort.

First off, what we will be doing will virtualize the domain but not the users. By that I mean all of your e-mail users will need a local account. Again, if you want to have both virtual users and virtual domains look into MySQL-based users and domains for both Postfix and Cyrus.

1. Start the process by creating your virtual mail domain user. You’ll most likely want to include the virtual mail domain in the user’s name. For example, for the e-mail user joel@example.com I would create a user with a shortname of “example_com_joel.” That way I can always tell at a glance what this user is supposed to be used for. Plus, if you follow this syntax you’ll easily be able to organize the users into domains when you use Workgroup Manager. It also makes things easy for your users if you add the e-mail address into the user record as a secondary short name. This will allow your users to use their e-mail address as their e-mail login.

2. Now you need to edit /etc/postfix/virtual. This is the lookup table that Postfix uses when trying to deliver mail. The beginning of this file has a number of comments about the file’s format. It’s at the end of this that you want to begin adding your virtual mail domains. Start off a section by listing the domain that you are going to make virtual.

example.com virtual

Then add your users in one per line in the form of “virtual mail address, shortname.” Here’s an example:

joel@example.com example_com_joel

You can also set up a catch-all account that will receive all mail for a domain that wasn’t specifically matched to a user name already in the list. This example delivers all mail to anyone at example.com to me.

@example.com example_com_joel

3. Now you need to add a directive to /etc/postfix/main.cf that makes Postfix aware of its new virtualness. You can either do this using a text editor, or just do the following from the command line.

sudo echo “virtual_alias_maps = hash:/etc/postfix/virtual” >> /etc/postfix/main.cf

4. Now you can tell Postfix to re-map its user lookup tables.

sudo postmap /etc/postfix/virtual

5. Then you’ll need to restart Postfix.

sudo postfix restart

Try it out. Send a few e-mails to yourself and see what happens.

The one catch to this situation is that the user “example_com_joel” is a valid user for all of the non-virtual domains also. For example, you could send an e-mail to “example_com_joel@afp548.com” and it would get through. To get around this you can virtualize all of your e-mail domains. Just add them all into /etc/postfix/virtual and repeat the process described above. Now you won’t have any overlapping e-mail boxes.

NOTE: If you want to support mailing lists for your domains (for example, through the built-in support for Mailman), you need to do two things differently: 1. Add the domain name in Server Admin, or add it directly to the mydestination line in /etc/postfix/main.cf. 2. Do not include the “virtual” line for the domain in /etc/postfix/virtual
This is because mailing lists are supported through an “aliases” file, and Postfix virtual domains exclude support for aliases in the “aliases” file.