4.3. MySQL

Install MySQL.

apt-get install postfix-mysql mysql-server

We are going to use Postfix Admin for managing virtual users and domains. Download latest version of Postfix Admin and move the content of the archive to /var/www/postfix.

cd /root/src
wget LINK-TO-THE-LATEST-VERSION
tar -xvzf postfixadmin-2.3.2.tar.gz
mv postfixadmin-2.3.2 /var/www/postfix

Before installing Postfix Admin we must create a database and a user to access the database.

mysql -u root

If you have a password set use the -p flag.

Once in MySQL:

CREATE DATABASE mail;
CREATE USER 'mailadmin'@'localhost' IDENTIFIED BY 'newpassword';
GRANT ALL PRIVILEGES ON `mail` . * TO 'mailadmin'@'localhost';
FLUSH PRIVILEGES;

Of course you need to change the newpassword with your own password. You can exit MySQL by typing exit.

Open /var/www/postfix/config.inc.php with nano. Set $CONF['configured'] to true, choose your setup password, set $CONF['postfix_admin_url'] to http://SERVERS-IP-ADDRESS/postfix and enter your database information and credentials.

While you are at it, you should also go through all of the other options you can set in the config mail.

[Note]Note

To use Postfix Admin you must setup PHP and Apache as described here.

Open up http://SERVERS-IP-ADDRESS/postfix/setup.php in your browser and finish the installation. Once logged in into the Postfix Admin create a new virtual domain example.com, and create new user accout username@example.com. Use your own domain name here.

Postifx Admin created MySQL tables for us and now it is time to tell Postfix to use this tables to get information about email addresses and domains it should serve.

First we need to create 4 files in the /etc/postfix folder.

/etc/postfix/sql/virtual_alias_maps.cf

user = mailadmin
password = newpassword
hosts = 127.0.0.1
dbname = mail
table = alias
select_field = goto
where_field = address

/etc/postfix/sql/virtual_domains_maps.cf

user = mailadmin
password = newpassword
hosts = 127.0.0.1
dbname = mail
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'

/etc/postfix/sql/virtual_mailbox_maps.cf

user = mailadmin
password = newpassword
hosts = 127.0.0.1
dbname = mail
table = mailbox
query = SELECT CONCAT(SUBSTRING_INDEX(username,'@',-1),'/',SUBSTRING_INDEX(username,'@',1),'/') FROM mailbox WHERE username='%s' AND active = 1

/etc/postfix/sql/relay_domains_maps.cf

user = mailadmin
password = newpassword
hosts = 127.0.0.1
dbname = mail
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '1'

As you can see, these files containt information that Postfix uses to fetch data from the database.

Since these files contain passwords, we need to protect them:

root@atlantis:~# cd /etc/postfix/sql
atlantis:/etc/postfix/sql# chgrp postfix *
atlantis:/etc/postfix/sql# chmod 640 *

Edit /etc/postfix/main.cf and at the bottom of the file add

##################
# Virtual Settings
##################
virtual_mailbox_base = /home/vmail
virtual_transport = virtual
virtual_alias_maps = mysql:/etc/postfix/sql/virtual_alias_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/sql/virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/sql/virtual_mailbox_maps.cf
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

# Domains for which we are a secondary MX
relay_domains = mysql:/etc/postfix/sql/relay_domains_maps.cf

You also need to delete everything in the mydestination directive because every domain that is entered in the mydestionation is treated as a “local” domain so the mail gets delivered to the local server users and it is located in /var/spool/mail.

Restart Postfix.

invoke-rc.d postfix restart

Now it's time to check if everything is working as expects. Send a test email to the email address you have created using Postfix Admin. Check your /home/vmail/example.com/username folder. There should be 3 folders inside : cur, new and tmp. Inside the folder new you should see your email message. If this didn't work, check /var/log/mail.log for possible hints on what went wrong.

[Note]Note

Since our system is now ready to receive email from the outside world you can uncomment the line that blocks port 25 in the firewall.sh script and run the script again.