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.

1. 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.
3. Click the Groups tab and now note down the Group ID of vmail. We’ll be needing all of them later.Configuring Postfix

1. Install a version of Postfix with MySQL support. The Postfix in RHEL/CentOS 6 already includes MySQL support.

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 -q acme.com mysql:/etc/postfix/mysql-domains.cf
Replace acme.com with your own domain name. It should echo your domain.


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 -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.


hosts = localhost user = postfix password = your_password dbname = postfix table = alias select_field = goto where_field = address additional_conditions = and active = '1'
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.
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.
Make 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.
9. If you are using RHEL/CentOS 6, configure SELinux using the commands below. Here’s the content of postfixmysql.tewget 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

10. Restart the Postfix or MailScanner service if you have installed it. Learn how to start and stop services here.
11. 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

1. 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
}

For 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.
Comment 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.

2. For RHEL/CentOS 6, install the Dovecot MySQL support package using the command
yum install dovecot-mysql

3. Create a file containing the lines below using the filename specified below.
| RHEL/CentOS Version | Filename |
|---|---|
| 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'

4. Restart the dovecot service. Learn how to start and stop services here.
5. 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).
If 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





April 4th, 2009 at 4:18 am
Is there a good way to support multiple domains without using a database? It seems like overkill for just a few personal domains, most of which only have one address.
April 23rd, 2009 at 8:03 am
i have problem in receiving mail & sending is fine i got following error
from=, size=315, nrcpt=1 (queue active)
Apr 23 18:53:34 cu postfix/virtual[2856]: 358705B004E: to=, orig_to=, relay=virtual, delay=1790, delays=1790/0.03/0/0.02, dsn=4.2.0, status=deferred (delivery failed to mailbox /home/vmail/abc.com/test1/Maildir: cannot open file: Is a directory)
May 5th, 2009 at 12:34 am
Thanks for your tutos, i already have postfix with multiple domains working, is there a way to integrate this with squirrelmail? i would like to give my users a webmail alternative.
June 12th, 2009 at 10:21 pm
[...] Hardy, and it seems there is no simple or supported way of setting this up on CentOS. I found a couple tutorials in other places on the web that helped me get the basics running eventually. Finally I [...]
June 29th, 2009 at 8:21 pm
Hi,
I am getting an error as given below when am trying to login as a virtual user. Anybody give me an idea to getridoff this.
Error connecting to IMAP server: localhost.
111 : Connection refused
Thanks
prakash
July 7th, 2009 at 1:45 am
@ Parkash.
I ran into the same problem
Check for /var/log/maillog. if permission denied appears, look for the files’ permission in Maildir/new/
chown -R vmail:vmail usr/Maildir/ resolved the problem for me.
July 7th, 2009 at 1:50 am
I actually have another issue at hand.
Followed the above tutorial step by step.
When i log into thru squirrel mail and enter the login id (user@domain.com) in small alphabets, it takes me into right Maildir directory (/home/vmail/domain.com/user/Maildir/…. BUT entering the user id in Capital letter (user@DOMAIN.COM) creates another directory in /home/vmail/DOMAIN.COM/user/Maildir/…).
Can someone let me know what is going wrong and where to look?
Many thanks in advance..
Mani
July 7th, 2009 at 1:52 am
Anyone found the way to use postfixadmin without using a database?
July 11th, 2009 at 9:56 pm
Hi Mani,
Add the line below to /etc/dovecot.conf to convert all usernames to lowercase
auth_username_format = %Lu
July 11th, 2009 at 9:57 pm
Hi colani,
A database (mysql or postgre sql) is an absolute must to use postfixadmin.
August 8th, 2009 at 6:11 am
Hi
how enable dovecot SASL auth with this configuration.
tanks.
August 8th, 2009 at 8:01 am
Hi Abdiel,
See Postfix SMTP Authentication and Dovecot SASL Howto
August 16th, 2009 at 7:27 am
tanks You very much!! consultant…
August 19th, 2009 at 12:44 am
hi to all.
i have some problem with SMTP AUTH, the client can logon in the server, but in oulook cant send message to outgoing client.
i have check in Outlook my server requiere smtp auth.
this is my config on main.cf on postfix
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
this is the message reject–> relay access denied to some client from outlook.
some idea.
August 19th, 2009 at 8:42 am
Hi Abdiel,
Make sure you have
mechanisms = plain login
Outlook does not support plain authentication
August 21st, 2009 at 10:24 pm
tanks consultan, i forgot change this value in dovecot.conf
auth default {
mechanisms = plain login
#other config is done
.
.
.
}