Use Postfix and Dovecot to set up the simplest email system

If you’ve read my post How to install and configure Postfix on CentOS, you may find the whole process for setting up an email system is quite complex. This is mainly because I chose a wrong pop/imap server: cyrus-imap, which is proven a buggy program that causes endless problems. Today, I switched from cyrus-imap to Dovecot, and set up the complete email system from the beginning. The whole process is much smoother. Postfix and Dovecot can cooperate seamlessly. But if you search tutorials on the Internet about postfix/dovecot configurations, you may still get lost soon because they teach you so many advanced techniques that are hard to understand. We will set up a minimal and simple email system using dovecot and postfix here. The communication between postfix and dovecot is through file sytem,i.e., the mail directory instead of lmtp as most guides teach you. We use Linux login as the the authentication mechanism so you do not need complex PAM plugins or mysql databases.

Set up Dovecot

After installing Dovecot on CentOS 7, you will see Dovecot’s configuration files are put in /etc/dovecot. The main Dovecot configuration file is /etc/dovecot/dovecot.conf, which includes other configuration files in /etc/dovecot/conf.d/. This is most like the configuration structure of httpd. For /etc/dovecot/dovecot.conf, we only need to un-comment the following two lines:

The first line enables Dovecot imap,pop3, and lmtp service. Since we do not need lmtp, we can remove it from protocols. The second line makes Dovecot listen on all IPV4 addresses and IPV6 addresses on your host.

Next, cd conf.d, we will modify some configuration files there.

First, you need to un-comment the following line in /etc/dovecot/conf.d/10-mail.conf to use ~/Maildir as the mail location.

Few people really know the meaning of the Dovecot mail_location parameter. Does Dovecot read emails from that location, or store emails to that directory? The answer is both. ~/Maildir is a directory under Linux user’s home directory. We will configure Postfix to deliver emails it receives to that directory as well. Specifically, Postfix will deliver emails to ~/Maildir/new/. When you run your email client such as Thunderbird to retrieve emails from Dovecot, Dovecot will read the emails from ~/Maildir/new/, and save them to ~/Maildir/cur meanwhile updating the information in ~/Maildir/dovecot.index.log, ~/Maildir/dovecot-uidvalidity, ~/Maildir/dovecot.index.cache, ~/Maildir/dovecot-uidlist.

Second, you need to modify /etc/dovecot/conf.d/10-auth.conf to enable authentication via unix login mechanism. Un-comment the following line:

and change disable_plaintext_auth to no, i.e.,

Add login to auth_mechanisms:

Third, you need to edit /etc/dovecot/conf.d/10-master.conf to provide a authentication mechanism for Postfix. Change the following lines:

to:

The changed part is enclosed in the service auth section, i.e., we want Dovecot to provide an auth service by creating and listening on a unix socket file: /var/spool/postfix/private/auth. We will configure Postfix to communicate with Dovecot through that socket to complete user authentication.

Last, you need to add ssl certificate and key information in /etc/dovecot/conf.d/10-ssl.conf. If you use letsencrypt issued certificate, the modified lines look like:

Now we finish setting up Dovecot. Restart the service

If you meet problems, you can check Dovecot logs for further information. The Dovecot log location is the same as the Postfix log: /var/log/maillog by default. If you cannot see content in that file, please make sure you have installed rsyslog and started the service:

 Set up Postfix

The configuration of Postfix is almost the same as in my previous post mentioned at the beginning, with several changes.

First, you need to  comment the mailbox_transport line:

As said before, we do not use lmtp as the communication protocol between Postfix and Dovecot. Instead, we use maildir as the communication mechanism. So we need to add the following line in /etc/postfix/main.cf:

According to the Postfix official document, when delivering received emails destined to this host, Postfix will try several mechanisms one by one in order. Without lmtp,Postfix will try home_mailbox.

Second, we need to guide Postfix to use Dovecot to complete the user authentication so authorized users can send emails through Postfix. In order to do that, we need to add the following two lines to main.cf:

Note that, the sasl path is a relative path. The full path is /var/spool/postfix/private/auth which has been specified in Dovecot configuration file.

Now reload Postfix:

If everything is ok, you can send/receive emails using this email system. You do not need to create extra email accounts. The users on your Linux system get their email accounts automatically. When configuring your email client such as Thunderbird, you need to fill the Linux user name in the Username box, i.e., you need to input admin rather than admin@domainhostseotool.com. The password is the Linux user(admin)’s password. When sending emails, Postfix smtpd receives the user name from the email client and calls Dovecot service to do the authentication(check /etc/passwd,/etc/shadow). When receiving emails from outside that destine to local host such as admin@domainhostseotool.com, Postfix will extract the user name from the email address(admin), and save the email content to the Maildir/new/ under the user(admin)’s home directory.

Posted in tips of hosting