Tutorials > How to configure a POP3 / IMAP mail server with Dovecot on Ubuntu 18.04

How to configure a POP3 / IMAP mail server with Dovecot on Ubuntu 18.04

Published on: 18 January 2020

Dovecot Mail Server Ubuntu

Dovecot is an open source application that allows you to receive emails on a Linux server in total security both through IMAP and POP3 protocol.

In this tutorial all the steps necessary to configure Dovecot on Ubuntu 18.04 are explained.

Setting up DNS records

Before proceeding with the actual installation of Dovecot, make sure that the domain records are set correctly.

Assuming you own the domain domain.com, first thing to do is set up a third level A record named "mail" and point it to the same address. Then to receive emails on your server, create an MX type record that points to the third level domain just created. 

HOST NAME TYPE VALUE
mail A [SERVER_IP]
  MX mail.domain.com

At this point you can connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to connect securely with SSH. In case of a local server, go to the next step and open the terminal of your server.

Dovecot installation

After connecting to the server via SSH, install Dovecot by running the following command:

$ sudo apt update && sudo apt install dovecot-core dovecot-pop3d dovecot-imapd

All Dovecot configuration files are present in the/etc/dovecot/directory. In this directory there are several files. Each file concerns a specific set of parameters included by the main file, once the service is started. The service i.e. dovecot.conf, should be modified as follows :

# Abilitiamo i protocolli IMAP e POP3

protocols = imap pop3

#Allows Dovecot to listen to all input connections (ipv4 / ipv6)

listen = *, ::

Another important parameter is present in the configuration file /etc/dovecot/conf.d/10-mail.conf:

mail_location = maildir:~/Maildir

This parameter allows you to indicate the appropriate path for saving all the emails received from users. In the example above, the Maildir directory present in the home of the reference user is being set.

One more useful parameter in the file /etc/dovecot/conf.d/20-pop3.conf allows to maintain compatibility with some older Outlook clients:

pop3_uidl_format = %08Xu%08Xv

pop3_client_workarounds = outlook-no-nuls oe-ns-eoh

Creating a user

The current Dovecot configuration is based on the users present in the system for accessing mailboxes. After completing the basic installation, proceed by creating the first user:

# User creation

$ sudo useradd john -m

# set password of user
$ sudo passwd john
# mails directory

$ sudo mkdir /home/john/Maildir

$ sudo chown john:john /home/john/Maildir

$ sudo chmod -R 700 /home/john/Maildir

Firewall configuration

In case of a firewall on your system, configure it to enable mail traffic.

When using the UFW firewall, pre-installed profiles for Dovecot are provided. So let's see how to enable them.

To check the available profiles installed in the UFW firewall run this command:

$ sudo ufw app list

A list similar to the following will be shown on the screen:

Available applications:

  Dovecot IMAP

  Dovecot POP3

  Dovecot Secure IMAP

  Dovecot Secure POP3

  Nginx Full

  Nginx HTTP

  Nginx HTTPS

  OpenSSH

  Postfix

  Postfix SMTPS

  Postfix Submission

To allow mail traffic, enable Dovecot profiles. 

To check profile information:

$ sudo ufw app info "Dovecot IMAP"

After checking the profiles, you’re ready to enable them:

$ sudo ufw allow "Dovecot POP3"

$ sudo ufw allow "Dovecot IMAP"

$ sudo ufw allow "Dovecot Secure IMAP"

$ sudo ufw allow "Dovecot Secure POP3"

Checking the operation of the mail server

To check the proper functioning of your POP3 server, use telnet. If it is not installed on your system, do it by running this command:

$ sudo apt install telnet

To start testing the mail server, start telnet:

$ sudo telnet localhost 110

If everything works correctly, the following output should be displayed on the screen:

Connected to localhost.

Escape character is '^]'.

+OK Dovecot ready. 

Then enter the username and password using the user and pass command:

user john

pass test

Once the login is completed, the server will send the following output:

+OK Logged in. 

To try to receive a real message, use the mail command provided by the postfix package (you can install Postfix following our guide How to configure a SMTP mail server with Postfix on Ubuntu 18.04): 

$ echo "TEST" |  mail -s "TEST" john@localhost

Once the email is sent, with the commands above, use telnet once again as above to access the mailbox:

$ telnet localhost 110

Trying ::1...

Connected to localhost.

Escape character is '^]'.

+OK Dovecot ready.

user john

+OK

pass test

+OK Logged in. 

Once logged-in, to get a list of the messages received run the list command:

list

+OK 1 messages:

1 533

.

As you can see, you have an unread message: use the RETR command to access the content:

retr 1

+OK 533 octets

Return-Path: <[email protected]>

To: [email protected]

Subject: Test

User-Agent: Heirloom mailx 12.5 7/5/10

MIME-Version: 1.0

Content-Type: text/plain; charset=us-ascii

Content-Transfer-Encoding: 7bit

Message-Id: <[email protected]>

From: [email protected] (root).