This article will show you how to configure Postfix and Dovecot to handle multiple domains stored in MySQL. Postfix Admin is used to manage the domains, mailboxes and aliases in MySQL. See How to Install Postfix Admin to setup the MySQL database that will be used by Postfix and Dovecot.

Creating the Virtual Mail User Account

Since the user names will be stored in MySQL, we will have to create a user that will be the owner for all the files belonging to the MySQL user names.

Create user vmail1. Create a new user, we will call it vmail. Change the Login Shell to /sbin/nologin, this user account should not be used for logging in. Learn how to use the User Manager application here.
User Manager2. Take note of the User ID and Home Directory of vmail.
User Manager3. Click the Groups tab and now note down the Group ID of vmail. We’ll be needing all of them later.

Configuring Postfix

postconf1. Install a version of Postfix with MySQL support. The Postfix in RHEL/CentOS 6 already includes MySQL support.
Edit mysql-domains.cf
2. Create the file /etc/postfix/mysql-domains.cf containing the lines below.
hosts = localhost
user = postfix
password = your_password
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'
postmap mysql-domains.cf3. Test /etc/postfix/mysql-domains.cf using the command below.
postmap -q acme.com mysql:/etc/postfix/mysql-domains.cf

Replace acme.com with your own domain name. It should echo your domain.

Edit config.inc.php
4. Create the file /etc/postfix/mysql-users.cf containing the lines below.
hosts = localhost
user = postfix
password = your_password
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
additional_conditions = and active = '1'
result_format = %sMaildir/
postmap mysql-users.cf5. Test /etc/postfix/mysql-users.cf using the command below.
postmap -q johndoe@acme.com mysql:/etc/postfix/mysql-users.cf

Replace johndoe@acme.com with your own email address. You should see the mailbox path.

Edit mysql-aliases.cf
6. Create the file /etc/postfix/mysql-aliases.cf containing the lines below.
hosts = localhost
user = postfix
password = your_password
dbname = postfix
table = alias
select_field = goto
where_field = address
additional_conditions = and active = '1'
postmap mysql-aliases.cf7. Test /etc/postfix/mysql-aliases.cf using the command below.
postmap -q john@acme.com mysql:/etc/postfix/mysql-aliases.cf

Replace john@acme.com with your own alias address. You should see the destination email.

Edit main.cf8. Edit the postfix configuration file /etc/postfix/main.cf and edit the line below.
mydestination = $myhostname, localhost.$mydomain, localhost

and add the lines below

virtual_mailbox_domains = mysql:/etc/postfix/mysql-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-users.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-aliases.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:501
virtual_gid_maps = static:501

virtual_mailbox_base, virtual_uid_maps and virtual_gid_maps should contain the home directory, user id and group id of vmail respectively.

NoteMake sure $mydomain in mydestination has been removed, otherwise the lookup will not work and you will get a “User unknown in local recipient table” error.
Install postfixmysql.te9. If you are using RHEL/CentOS 6, configure SELinux using the commands below. Here’s the content of postfixmysql.te
wget linuxmail.info/files/rhel6/postfixmysql.te
checkmodule -M -m -o postfixmysql.mod postfixmysql.te
semodule_package -o postfixmysql.pp -m postfixmysql.mod
semodule -i postfixmysql.pp
Service Configuration10. Restart the Postfix or MailScanner service if you have installed it. Learn how to start and stop services here.
Terminal11. You should now be able to send email to addresses found in MySQL. See Test Postfix using Telnet and try using MySQL email addresses instead of the system user names.

Configuring Dovecot

Edit dovecot.conf1. For RHEL/CentOS 5, edit the file /etc/dovecot.conf and change the value of the following keys below.
auth_username_format = %Lu

passdb sql {
  args = /etc/dovecot-mysql.conf
}

userdb static {
  args = uid=501 gid=501 home=/home/vmail/%d/%n
}
Edit dovecot.confFor RHEL/CentOS 6, edit the file /etc/dovecot/conf.d/10-auth.conf and add the lines below.
auth_username_format = %Lu

passdb {
  driver = sql
  args = /etc/dovecot/dovecot-mysql.conf
}

userdb {
  driver = static
  args = uid=501 gid=501 home=/home/vmail/%d/%n
}

uid, gid and home should contain the user id, group id and home directory respectively of the vmail user account.

NoteComment out all the other passdb and userdb sections or include lines except for those specified above to ensure that nothing will conflict with our MySQL virtual accounts.

yum install dovecot-mysql2. For RHEL/CentOS 6, install the Dovecot MySQL support package using the command
yum install dovecot-mysql
Edit dovecot-mysql.conf3. Create a file containing the lines below using the filename specified below.
RHEL/CentOS VersionFilename
5/etc/dovecot-mysql.conf
6/etc/dovecot/dovecot-mysql.conf
driver = mysql
connect = host=localhost dbname=postfix user=postfix password=password
default_pass_scheme = PLAIN
password_query = SELECT password FROM mailbox WHERE username = '%u' AND active = '1'
Service Configuration4. Restart the dovecot service. Learn how to start and stop services here.
Terminal5. You should now be able to login using the user names found in MySQL. See Test Dovecot using Telnet and use MySQL user names (johndoe@acme.com) instead of the system user names (johndoe).
NoteIf you encounter any problems, check the log file at /var/log/maillog.

Visit the forum to ask for help or to give a comment.

***
Posted on 2/22/2009 and last updated on 8/29/2011
Filed under CentOS 5 , CentOS 6 , Postfix , Red Hat Enterprise Linux 5 , Red Hat Enterprise Linux 6