Monday, May 2, 2011

Postfix with MySQL Backend

Here I am trying to describe how I configured Postfix with MySQL backend. Here I have given only the important parameter, however you can find tune this in various ways.


Configure your Postfix with MySQL support.

I used following command - make -f Makefile.init makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm'

Before do this, please make sure that you have installed all the MySQL components, including mysql-devel.


Then do the make


DO the make install. At the end of this phase you will be prompt with many questions, answer them appropriate.


Then start configure your Postfix, normally you can find the configuration in

The important settings (though there are several settings) you need to add are :

data_directory = /var/lib/postfix
mail_owner = postfix
- make sure that you have created the user postfix
myhostname =
mydomain = XX,
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = 192.NN.N.N
home_mailbox = Maildir/
- This is important, and this is where you say that you want to use MailDir, not Mailbox
virtual_gid_maps = static:506
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_domains = mysql:/etc/postfix/
virtual_mailbox_maps = mysql:/etc/postfix/
virtual_minimum_uid = 100
virtual_transport = virtual
virtual_uid_maps = static:504
- You must create another user, may be vmail, who should have permission to write mails to maildir. After adding those user, you need to specify the uid and gid of that user here in uid_maps and gid_maps. Also make sure that you have given sufficient permission to the mailbox_base to the user that you have created.

After doing all these settings in the file, you need to create another two files that you have given in "virtual_mailbox_domains" and "virtual_mailbox_maps".

In my case etc/postfix/ contains following parameters :

hosts = localhost
dbname = maildb
user = mailuser
password = xxxxxxx
table = mail_domains
select_field = mail_domain
where_field = mail_domain

This tells postfix that what are the domains should be handled by the postfix it self and what are should be routed to other MTAs.

/etc/postfix/ contains :
hosts = localhost
dbname = maildb
user = mailuser
password = XXXX
table = mail_accounts
select_field = maildir
where_field = mail_user

This is used to select the MailDir name for the respective email account. For example if someone sends a mail to, this will find and tells that where that message should go in the virtual_mailbox_base.

Hope this will work for you all as well.

Good luck

Reference :

No comments: