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.

1.

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.

2.

Then do the make

3.

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

4.

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

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 = mail.yy.xx.lk
mydomain = XX,cc.lk
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/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
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 main.cf 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/mysql_virtual_domains_maps.cf 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/mysql_virtual_mailbox_maps.cf 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 sarves@xyz.com, 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 :
http://hostingsoftware.net/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=56
http://www.postfix.org/INSTALL.html

No comments: