Now the database is ready to be filled with information on user accounts. The entry point for all email on your system is Postfix. So we need to tell Postfix how to get to the database-stored information. Let's start by telling it which virtual domains you have.
As described earlier a mapping in Postfix is just a table that contains a left-hand side (LHS) and a right-hand side (RHS). To make Postfix use MySQL to define a mapping we need a 'cf' file (configuration file). Start by creating a file called /etc/postfix/mysql-virtual-mailbox-domains.cf for the virtual_mailbox_domains mapping that contains:
user = mailuser password = mailuser2009 hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_domains WHERE name='%s'
Imagine that Postfix received an email for email@example.com and wants to find out if example.com is a virtual mailbox domain. It will run the above SQL query and replace '%s' by 'example.com'. If it finds such an entry in the virtual_domains table it will return a '1'. Actually it does not matter what exactly is returned as long as there is a result.
And you need to make Postfix use this database mapping:
$> postconf -e virtual_mailbox_domains=mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
(The postconf -e command conveniently adds configuration lines to your /etc/postfix/main.cf file. It also activates the new setting instantly so you do not have to reload the Postfix process.)
Postfix will now search your virtual_domains table to find out if "example.com" is a virtual mailbox domain. Let us see if this works. Create a new row in the virtual_domains table with one domain. Either do it in phpmyadmin through the "Insert" tab or connect to your database by running:
$> mysql -p mailserver
and execute this query:
mysql> INSERT INTO virtual_domains (id, name) VALUES (1, 'example.com'); exit
Back at your root shell you can now check if the 'example.com' domain is known as a virtual mailbox domain:
$> postmap -q example.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
You should get '1' as a result. Your first mapping is working. Great. Get straight to the second one.
If you get an error message telling you postmap: warning: connect to mysql server 127.0.0.1: Access denied... then you have a problem with the user account "mailuser" that you use to connect to the database. Check the MySQL privileges again.
If you get an error message reading postmap: warning: connect to mysql server 127.0.0.1: Can't connect to MySQL server on '127.0.0.1' then your MySQL server is either not running or at least not listening on 127.0.0.1. Check your MySQL server configuration (/etc/mysql/my.cnf).
You will now define the virtual_mailbox_maps which is mapping email addresses (left-hand side) to the location of the user's mailbox on your harddisk (right-hand side). If you saved incoming email to the hard disk using Postfix's built-in virtual delivery agent then it would be queried to find out the mailbox path. But in our setup the actual delivery is done by Dovecot's LDA (local delivery agent) so Postfix does not really care about the path. Postfix just needs to check if a certain email address is valid. Similar to the above you need an SQL query that searches for an email address and returns "1".
Go create an entry in the virtual_users table for a test user firstname.lastname@example.org:
mysql> INSERT INTO virtual_users (id, domain_id, email, password) VALUES (1, 1, 'email@example.com', MD5('summersun'));
Next you will need to create a ".cf" file to tell Postfix about the SQL query for this table. In addition to the email address it is also important to get the user's password later on. As the path of the user's mailbox is fixed it is not important to get that information from the database. The directory structure will always be /var/vmail/$DOMAIN/$USER. So for John's example it would be /var/vmail/example.com/john.
Now things are a bit simpler and you can finally create a ".cf" file at /etc/postfix/mysql-virtual-mailbox-maps.cf that is as simple as:
user = mailuser password = mailuser2009 hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_users WHERE email='%s'
Tell Postfix that this mapping file is supposed to be used for the virtual_mailbox_maps mapping:
$> postconf -e virtual_mailbox_maps=mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
Test if Postfix is happy with this mapping by asking it where the mailbox directory of our firstname.lastname@example.org user would be:
$> postmap -q email@example.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
You should get "1" back which means that firstname.lastname@example.org is an existing virtual mailbox user on your server. Later in the Dovecot configuration part you will also use the email and password fields but Postfix does not need them here. Great, there is just one mapping left to define:
The virtual_alias_maps mapping is used for forwarding emails from one email address to another. Examples of how entries in this mapping may look:
|source (LHS)||destination (RHS)||Meaning|
|email@example.comfirstname.lastname@example.org||Forward John's mail to another address|
|Deliver one copy to the original account email@example.com but also send a copy to firstname.lastname@example.org (yes, Postfix understands this - it does not create a loop)|
|@email@example.com||Deliver all email for the domain to firstname.lastname@example.org unless there is a specific virtual user account defined in the virtual alias map. If email@example.com does not exist as a specific virtual user then her mail would be sent to john. If firstname.lastname@example.org is a valid user then she would get the mail. This is called a catchall account.|
(See "man 5 virtual" for a more formal definition.)
Beware: Catch-all aliases catch spam. Lots of spam. They may look comfortable because they forward all email to one person without the need for creating aliases. But spammers often try to guess email addresses at a known domain. And with a catch-all alias you will receive spam for any of those guessed email addresses. Try to avoid them and rather define the existing email addresses. Even if it seems to be more work.
As you see it is possible to name multiple destinations. In the database this is achieved by using different rows. For example the second line above should be split into two rows:
|source (LHS)||destination (RHS)|
Let us add this example to the database:
mysql> INSERT INTO virtual_aliases (id, domain_id, source, destination) VALUES (1, 1, 'email@example.com', 'firstname.lastname@example.org'), (2, 1, 'email@example.com', 'firstname.lastname@example.org');
Create another ".cf" file at /etc/postfix/mysql-virtual-alias-maps.cf:
user = mailuser password = mailuser2009 hosts = 127.0.0.1 dbname = mailserver query = SELECT destination FROM virtual_aliases WHERE source='%s'
Test if the mapping file works as expected:
$> postmap -q email@example.com mysql:/etc/postfix/mysql-virtual-alias-maps.cf
You should see the two expected destinations:
The black magic "email-to-email" mapping
Before you define the virtual_alias_maps setting there is a small quirk you need to take care of. There is a special kind of forwarding: the "catchall" alias. Catchalls catch all emails for a domain if there is no specific user account. A catchall alias looks like "@example.com" and forwards email for the whole domain to one account. We have created the 'firstname.lastname@example.org' user and would like to forward all other email on the domain to 'email@example.com'. So we would add a catchall alias like:
Now imagine what happens when Postfix receives an email for 'firstname.lastname@example.org'. Postfix will first check if there are any aliases in the virtual_alias_maps table. (It does not look at the virtual_mailbox_maps table at the moment.) It finds the catchall entry as above and since there is no more specific alias the catchall account matches and the email is redirected to 'email@example.com'. This is probably not what you wanted. So you need to make the table rather look like this:
More specific aliases have precedence over general catchall aliases. Postfix will lookup all these mappings for each of:
- firstname.lastname@example.org (most specific)
- john (only works if "example.com" is the $myorigin domain)
- @example.com (catchall - least specific)
Postfix will find an entry for 'email@example.com' first and sees that email should be "forwarded" to 'firstname.lastname@example.org' - the same email address. This trickery may sound weird but it is needed if you plan to use catchall accounts. So the virtual_alias_maps mapping must obey both the "view_aliases" view and this "john-to-himself" mapping. This is outlined in the virtual(5) man page in the TABLE SEARCH ORDER section.
Create a ".cf" file /etc/postfix/mysql-email2email.cf for the latter mapping:
user = mailuser password = mailuser2009 hosts = 127.0.0.1 dbname = mailserver query = SELECT email FROM virtual_users WHERE email='%s'
Check that you get John's email address back when you ask Postfix if there are any aliases for him:
$> postmap -q email@example.com mysql:/etc/postfix/mysql-email2email.cf
The result should be the same address:
Now you need to tell Postfix that these two mappings should be searched by adding this line to your main.cf:
$> postconf -e virtual_alias_maps=mysql:/etc/postfix/mysql-virtual-alias-maps.cf,mysql:/etc/postfix/mysql-email2email.cf
The order of the two mappings is not important here.
You did it! All mappings are set up and the database is generally ready to be filled with domains and users. Make sure that only 'root' and the 'postfix' user can read the ".cf" files - after all your database password is stored there:
$> chgrp postfix /etc/postfix/mysql-*.cf $> chmod u=rw,g=r,o= /etc/postfix/mysql-*.cf