Revision as of 16:31, 22 February 2014

This article describes how to set up a complete virtual user mail system on an Arch Linux system in the simplest manner possible. However, since a mail system consists of many complex components, quite a bit of configuration will still be necessary. Roughly, the components used in this article are Postfix, Dovecot, PostfixAdmin and Roundcube.

In the end, the provided solution will allow you to use the best currently available security mechanisms, you will be able to send mails using SMTP and SMTPS and receive mails using POP3, POP3S, IMAP and IMAPS. Additionally, configuration will be easy thanks to PostfixAdmin and users will be able to login using Roundcube. What a deal!

This article assumes that you have a working LAMP setup as we will need a working Apache2 as well as MYSQL database. Of course, with a few changes to these instructions you could easily use another httpd and database. For the purposes of this tutorial, however, the choice made above will be used. Additionally, the article assumes all-default settings for every package installed below. No changes except for those mentioned will be required.

Should any unforeseen problems occur, feel free to use the discussion page to voice your problems and I will try to answer.

Configuration

User

A gid and uid of 5000 is used in both cases so that we do not run into conflicts with regular users. All your mail will then be stored in /home/vmail. You could change the home dir to something like /var/mail/vmail but careful to change this in any configuration below as well.

Database

You will need to create an empty database and corresponding user. We will be using PostfixAdmin's tables to fill the database later on. In this article, postfix_user will have read/write access to postfix_db using hunter2 for a password. You are expected to create your database and user yourself as shown in the following code. Make sure to assign proper permissions.

Now obviously we also need the /etc/dovecot/dovecot-sql.conf we just referenced in the config above. Go ahead and create a /etc/dovecot/dovecot-sql.conf with these contents:

driver = mysql
connect = host=localhost dbname=postfix_db user=postfix_user password=hunter2
# The new name for MD5 is MD5-CRYPT so you might need to change this depending on version
default_pass_scheme = MD5-CRYPT
# Get the mailbox
user_query = SELECT '/home/vmail/%d/%u' as home, 'maildir:/home/vmail/%d/%u' as mail, 5000 AS uid, 5000 AS gid, concat('dirsize:storage=', quota) AS quota FROM mailbox WHERE username = '%u' AND active = '1'
# Get the password
password_query = SELECT username as user, password, '/home/vmail/%d/%u' as userdb_home, 'maildir:/home/vmail/%d/%u' as userdb_mail, 5000 as userdb_uid, 5000 as userdb_gid FROM mailbox WHERE username = '%u' AND active = '1'
# If using client certificates for authentication, comment the above and uncomment the following
#password_query = SELECT null AS password, ‘%u’ AS user

Then assuming localhost is the hostname of the machine you are installing this on, navigate to http://localhost/postfixAdmin/setup.php. The setup will guide you through the remaining steps to set up PostfixAdmin.

Note: For a detailed section on setting up domains and mailboxes in PostfixAdmin see the related [Gentoo wiki article]

Roundcube

Roundcube is available in the community repo.

pacman -S roundcubemail

The post install process is similar to any other webapps like PhpMyAdmin or PostFixAdmin. Copy the example configuration file to your webserver configuration directory. If you are using Apache do:

Make some directories writable by the webserver in /usr/share/webapps/roundcubemail/:

chown -R http:http temp logs

Roundcube now requires the pdo_mysql.so extension, make sure this extension is un-commented in your php.ini file. Also check .htaccess for access restrictions. Assuming that localhost is your current host, navigate a browser to http://localhost/roundcubemail/installer/ and follow the instructions. You could use the same database for Roundcube that you already used for PostfixAdmin though you shouldn't. For a proper setup, create a second database "roundcube_db" and a "roundcube_user" for use with Roundcube. Update this info in db.inc.php.

While running the installer, make sure to address the IMAP host with ssl://localhost/ or tls://localhost/ instead of just localhost. Use port 993. Likewise with SMTP, make sure to provide ssl://localhost/ on port 465 if you used the wrapper mode and tls://localhost/ on port 587 if you used the proper TLS mode. See here for an explanation on that.

systemd

Make sure your daemons start on boot:

systemctl enable dovecot postfix

Fire it up

Since now hopefully everything is set up correctly, all necessary daemons should be started for a test run:

systemctl start postfix dovecot

Now for testing purposes, create a domain and mail account in PostfixAdmin. Try to login to this account using Roundcube. Now send yourself a mail.

Optional Items

Although these items are not required, they definitely add more completeness to your setup

Troubleshooting

If you get errors like your imap/pop3 client failing to receive mails, take a look into your /var/log/mail.log file.
It turned out that the maildir /home/vmail/mail@domain.tld is just being created if there is at least one email waiting. Otherwise there wouldn't be any need for the directory.

Tips and Tricks

When sending email with Postfix, how can I hide the sender’s IP and username in the Received header

This is a privacy concern mostly, if you use Thunderbird and send an email. The received header will contain your LAN and WAN IP and info about the email client you used.
(Original source: AskUbuntu)
What we want to do is remove the Received header from outgoing emails. This can be done by applying header_checks exclusively to the submission port. What we need to do is pass the cleanup_service_name option to the submission service so that we can set up a new cleanup service, “subcleanup.” The relevant section of /etc/postfix/master.cf might look like this: