Virtual Alias Domain in Postfix

A reader Remo Mattei commented on my article about a Virtual Alias Domain issue. He got the Postfix 550 “Recipient address rejected: User unknown in virtual mailbox table;” error in maillog.

Since I don’t need this feature in my current setup, so it got ignored when I built and tested my mail system few months ago. After reading some documents online I managed get this work.

So what is Virtual Alias Domain?

Here is the defination from the official Postfix site:

VIRTUAL ALIAS DOMAINS
Besides virtual aliases, the virtual alias table can also be used to
implement virtual alias domains. With a virtual alias domain, all
recipient addresses are aliased to addresses in other domains.

Virtual alias domains are not to be confused with the virtual mailbox
domains that are implemented with the Postfix virtual(8) mail delivery
agent. With virtual mailbox domains, each recipient address can have
its own mailbox.

With a virtual alias domain, the virtual domain has its own user name
space. Local (i.e. non-virtual) usernames are not visible in a virtual
alias domain. In particular, local aliases(5) and local mailing lists
are not visible as localname@virtual-alias.domain.

Confused? A little bit. Let’s use an example to explain it:

Let’s say I am hosting emails for both domain example.com and example.net. All users will have both .com and .net email addresses. And we want all the emails send to .net to deliver to corrsponding .com address.

With the Virtual Alias Domain feature in Postfix, I can simply do:

  1. Create domain example.com and example.net;
  2. Setup virtual alias domain example.net with the target domain set to example.com;
  3. Add Mailbox for example.com users (e.g. joe@example.com and bob@example.com.,etc.)

Now all the email send to joe@example.net will be delivered to joe@example.com mailbox. So does for Bob.

Postfix Configuration

If you followed my previous post on setup Postfix and PostfixAdmin, then you can continue here. If you use different setup, then you need alter the configuration accordingly.

Here I’ll show you how my configuration to:

  1. Enable Postfix to use Virtual Alias Domain feature;
  2. Create MySQL connectors to connect to PostFixAdmin database.

Postfix main.cf

In the Part 2 I have the following settings in /etc/postfix/main.cf

relay_domains = proxy:mysql:/etc/postfix/mysql-relay_domains_maps.cf
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_alias_maps.cf,regexp:/etc/postfix/virtual_regexp
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains_maps.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_maps.cf
virtual_mailbox_limit_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_limit_maps.cf
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $smtpd_sender_login_maps $sender_bcc_maps $recipient_bcc_maps $smtp_generic_maps $lmtp_generic_maps $alias_maps $virtual_mailbox_limit_maps

Now use the following lines to replace the following three lines respectively:

virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains_maps.cf
virtual_alias_maps =
proxy:mysql:/etc/postfix/mysql-virtual_alias_maps.cf,
proxy:mysql:/etc/postfix/mysql-virtual_alias_domain_maps.cf,
proxy:mysql:/etc/postfix/mysql-virtual_alias_domain_catchall_maps.cf
virtual_mailbox_maps =
proxy:mysql:/etc/postfix/mysql-virtual_mailbox_maps.cf,
proxy:mysql:/etc/postfix/mysql-virtual_alias_domain_mailbox_maps.cf

MySQL connection configuration

Now we need Postfix to make connections to MySQL databse (Created by PostfixAdmin) and to qurey the database.
So let’s create 3 new configration files:

  1. /etc/postfix/mysql-virtual_alias_domain_maps.cf

    hosts = localhost
    user = postfix
    password = PApassword
    dbname = postfix
    query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
  2. /etc/postfix/mysql-virtual_alias_domain_catchall_maps.cf

    user = postfix
    password = PApassword
    hosts = localhost
    dbname = postfix
    query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
  3. /etc/postfix/mysql-virtual_alias_domain_mailbox_maps.cf

    user = postfix
    password = PApassword
    hosts = localhost
    dbname = postfix
    query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'

Do not forget to set the correct permission on these new files:

chgrp postfix /etc/postfix/mysql-virtual_alias_domain*
chmod 640 /etc/postfix/mysql-virtual_alias_domain_*

Restart Postfix (or MailScanner), check maillog and make sure no errors.

PostfixAdmin Setup

Go to PostfixAdmin and login as administrator. Add two new domain example.com and example.net.

Then go toVirtual List then Add Alias Domain. Select example.net as the Alias Domain. Set the Target Domain to example.com.

Now add an user mailbox for domain example.com. For example joe@example.com

Test

Send an email to see the virtual alias domain feature in action. The email send to joe@example.net should arrive to the inbox of joe@example.com