How-To: Virtual emails accounts with Postfix and Dovecot — page 2
We have set up the database in the first part. Now let’s go ahead and configure our email server to use that database.
4. Configuring Postfix
4.1. main.cf and master.cf
Your postfix main.cf file should look like:
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache
#vhost with mysql params
#virtual_alias_domains needs to be unset
virtual_alias_domains =
virtual_mailbox_domains = mysql:/etc/postfix/virtual/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/virtual/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/virtual/mysql-virtual-alias-maps.cf, mysql:/etc/postfix/virtual/mysql-virtual-email2email.cf
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1
myhostname = <your hostname here>
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = <your hostname here>, localhost
relayhost =
mynetworks = 127.0.0.0/8
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
And at the end of /etc/postfix/master.cf add:
dovecot unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -d ${recipient}
Do not restart/reload yet, the access to the users/domains informations in the database still need to be set, also, dovecot needs to be configured.
4.2. Access to the database
As per the main.cf configuration, the database configuration files will be kept in /etc/postfix/virtual so you need to create this directory:
# mkdir /etc/postfix/virtual
Then we will create the virtual mailbox maps database information:
# vi /etc/postfix/virtual/mysql-virtual-mailbox-maps.cf
and add the following:
user = vemailuser
password = vemailpass
hosts = 127.0.0.1
dbname = virtual_email
query = SELECT 1 FROM virtual_users AS U LEFT JOIN virtual_domains AS D ON U.domain_id=D.id WHERE CONCAT(U.user, '@',D.name)='%s'
Now, the virtual_mailbox_domains:
# vi /etc/postfix/virtual/mysql-virtual-mailbox-domains.cf
user = vemailuser
password = vemailpass
hosts = 127.0.0.1
dbname = virtual_email
query = SELECT 1 FROM virtual_domains WHERE name='%s'
and finally, for the aliases (virtual_alias_maps), we need 2 files:
# vi /etc/postfix/virtual/mysql-virtual-alias-maps.cf
user = vemailuser
password = vemailpass
hosts = 127.0.0.1
dbname = virtual_email
query = SELECT destination FROM virtual_aliases AS A LEFT JOIN virtual_domains AS D ON A.domain_id=D.id WHERE CONCAT(A.source, '@', D.name)='%s'
and
# vi /etc/postfix/virtual/mysql-virtual-email2email.cf
user = vemailuser
password = vemailpass
hosts = 127.0.0.1
dbname = virtual_email
query = SELECT CONCAT(U.user, '@',D.name) FROM virtual_users AS U LEFT JOIN virtual_domains AS D ON U.domain_id=D.id WHERE CONCAT(U.user, '@',D.name)='%s'
Now, we have to set the rights properly for those files:
# chown root:postfix -R /etc/postfix/virtual
# chmod 640 -R /etc/postfix/virtual
Now, we are done with the postfix side, let’s carry on with dovecot.