Dovecot IMAP server

21 September,2023

What is Dovecot?

Dovecot is an open source IMAP and POP3 email server.

What is an IMAP/POP3 server?

Both Internet Message Access Protocol (IMAP) and Post Office Protocol Version 3 (POP3) are Internet standard protocols used by email clients to retrieve email. An IMAP server typically listens on port number 143 and IMAP over SSL/TLS (IMAPS) is assigned the port number 993. POP3 listens on port 110 and POP3 over TLS listens on port 995.

Difference between IMAP & POP3

When an email client connects to a server using POP, it grabs all the mail on the server. It then stores every mail locally on your device, so you can access it in your email client. Finally, it deletes the mails from the email server before disconnecting. This means that the messages only exist on the device you downloaded them to.

Email clients using IMAP leave messages on the server until the user explicitly deletes them. This and other characteristics of IMAP operation allow multiple clients to manage the same mailbox. Mails are fetched and cached locally.

Install Dovecot and other necessery packages

  apt install dovecot-core dovecot-ldap dovecot-lmtpd dovecot-sieve \
  dovecot-managesieved dovecot-imapd dovecot-pop3d

Configure Dovecot

Dovecot configuration is fairly straightforward as it requires minimal changes to default config, unlike postfix. Dovecot have fragmented its config files. Hence each service, be it LMTP or LDAP, is configured in its own file. This improves readability and ease of configuration. Configuration files are present in /etc/dovecot. Check tree listed below for location of files mentioned in confguration.

  /etc/dovecot
  |-- conf.d
  |   |-- 10-auth.conf
  |   |-- 10-director.conf
  |   |-- 10-logging.conf
  |   |-- 10-mail.conf
  |   |-- 10-master.conf
  |   |-- 10-ssl.conf
  |   |-- 15-lda.conf
  |   |-- 15-mailboxes.conf
  |   |-- 20-imap.conf
  |   |-- 20-lmtp.conf
  |   |-- 20-managesieve.conf
  |   |-- 20-pop3.conf
  |   |-- 90-acl.conf
  |   |-- 90-plugin.conf
  |   |-- 90-quota.conf
  |   |-- 90-sieve.conf
  |   |-- 90-sieve-extprograms.conf
  |   |-- auth-checkpassword.conf.ext
  |   |-- auth-deny.conf.ext
  |   |-- auth-dict.conf.ext
  |   |-- auth-ldap.conf.ext
  |   |-- auth-master.conf.ext
  |   |-- auth-passwdfile.conf.ext
  |   |-- auth-sql.conf.ext
  |   |-- auth-static.conf.ext
  |   |-- auth-system.conf.ext
  |   |-- auth-vpopmail.conf.ext
  |-- dovecot.conf
  |-- dovecot-dict-auth.conf.ext
  |-- dovecot-dict-sql.conf.ext
  |-- dovecot-ldap.conf.ext
  |-- dovecot-sql.conf.ext

You can view all the services running in dovecot using netstat command

  $ netstat -tnpl | grep dovecot
  tcp        0      0 0.0.0.0:4190            0.0.0.0:*               LISTEN      610761/dovecot
  tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN      610761/dovecot
  tcp        0      0 0.0.0.0:24              0.0.0.0:*               LISTEN      610761/dovecot
  tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN      610761/dovecot

Enable and configure AUTH and LMTP

  service lmtp {
    unix_listener /var/spool/postfix/private/dovecot-lmtp {
      mode = 0600
      user = postfix
      group = postfix
    }
  }
  
  service auth {
    unix_listener /var/spool/postfix/private/auth {
      mode = 0600
      user = postfix
      group = postfix
    }
  }
  
  disable_plaintext_auth = yes
  auth_username_format = %Lu
  auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@
  auth_mechanisms = plain login
  
  ## Password and user databases
  !include auth-ldap.conf.ext
Configure LDAP User Database
  hosts = localhost
  auth_bind = yes
  auth_bind_userdn = uid=%n,ou=users,dc=example,dc=in
  base = dc=example,dc=in
  scope = subtree
  user_attrs = homeDirectory=home
  # By default, dovecot reads quota attribute in bytes. Uncomment following in such case
  # user_attrs = homeDirectory=home,mailQuota=quota_rule=*:bytes=%$
  # In case you want to read mail quota attribute in MB, uncomment following.
  # user_attrs = homeDirectory=home,mailQuota=quota_rule=*:storage=%$M
  user_filter = (&(objectClass=inetOrgPerson)(mail=%s))
  pass_attrs = uid=user,userPassword=password
  pass_filter = (&(objectClass=inetOrgPerson)(mail=%s))
Configure LMTP
  # If quota plugin is used remove uncomment following parameter.
  # lmtp_rcpt_check_quota = yes
  lmtp_add_received_header = yes
  protocol lmtp {
    # Without quota plugin uncomment following
    mail_plugins = $mail_plugins sieve
    # With quota plugin uncomment following
    # mail_plugins = $mail_plugins quota sieve
  }

Configure Mailbox

  mail_location = maildir:/home/%n/Maildir/
  #mail_location = maildir:/home/example.com/%n/
  # In this case dovecot will use home value mentioned in user db(LDAP)
  #mail_location = maildir:%{userdb:home}/Maildir
  
  namespace inbox {
    inbox = yes
  }
  
  mail_uid = dovecot
  mail_gid = dovecot
  first_valid_uid = dovecot
  mail_privileged_group = dovecot
  # auto:
  #   Indicates whether the mailbox with this name is automatically created
  #   implicitly when it is first accessed. The user can also be automatically
  #   subscribed to the mailbox after creation. The following values are
  #   defined for this setting:
  #
  #     no        - Never created automatically.
  #     create    - Automatically created, but no automatic subscription.
  #     subscribe - Automatically created and subscribed.
  # NOTE: Assumes "namespace inbox" has been defined in 10-mail.conf.
  
  namespace inbox {
    # These mailboxes are widely used and could perhaps be created automatically:
    mailbox Drafts {
      auto = subscribe
      special_use = \Drafts
    }
    mailbox Junk {
      auto = subscribe
      special_use = \Junk
    }
    mailbox Trash {
      auto = subscribe
      special_use = \Trash
    }
  
    mailbox Sent {
      auto = subscribe
      special_use = \Sent
    }
    mailbox "Sent Messages" {
      auto = subscribe
      special_use = \Sent
    }
  }
     

Configure TLS

  ssl = yes
  ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem
  ssl_key = </etc/letsencrypt/live/example.com/privkey.pem
  ssl_min_protocol = TLSv1.1
  inet_listener imaps {
    port = 993
    ssl = yes
  }

Configure Logging

  log_path=syslog

Configure sieve

  protocols = $protocols sieve
  service managesieve-login {
   inet_listener sieve {
      port = 4190
    }
  }
  service managesieve {
      process_limit = 1024 
  }
  plugin {
    sieve = file:/home/vmail/%n/sieve;active=/home/vmail/%n/.dovecot.sieve
    }
  mail_plugins = $mail_plugins sieve

A Dovecot server with IMAP(S), POP3(S) have been set up with LMTP for mail delivery and Sieve for mail filtering.