Setting up Dovecot

Published on December 29, 2020

Installation

Install dovecot:

apk add dovecot dovecot-lmtpd dovecot-pgsql dovecot-pigeonhole-plugin dovecot-pop3d

Configuration

Edit /etc/dovecot/dovecot.conf:

protocols = imap pop3 lmtp

Edit /etc/dovecot/conf.d/10-mail.conf:

first_valid_uid = 105
last_valid_uid = 105
first_valid_gid = 107
last_valid_gid = 107
mail_location = maildir:/var/mail/domains/%d/%n

Edit the imap service in /etc/dovecot/conf.d/10-master.conf:

service imap {
  vsz_limit = 1024M
}

service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0666
    user = postfix
    group = postfix
  }

  user = vmail
  group = postdrop
}

service auth {
  unix_listener auth-userdb {
  }

  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

  unix_listener /var/spool/postfix/auth-master {
    mode = 0660
    user = vmail
    group = postfix
  }

  user root
}

Edit the mailboxes in /etc/dovecot/conf.d/15-mailboxes.conf:

namespace inbox {
  inbox = yes

  mailbox Trash {
    auto = create
    special_use = \Trash
  }

  mailbox Spam {
    auto = no
    special_use = \Junk
  }

  mailbox Sent {
    auto = subscribe
    special_use = \Sent
  }
}

Edit /etc/dovecot/conf.d/10-auth.conf:

auth_mechanisms = plain login
auth_username_format = %Lu
disable_plaintext_auth = no

Edit /etc/dovecot/conf.d/20-lmtp.conf:

protocol lmtp {
  # Space separated list of plugins to load (default is global mail_plugins).
  mail_plugins = $mail_plugins sieve
}

Edit /etc/dovecot/conf.d/20-managesieve.conf:

protocols = $protocols sieve

Make sure to include auth-sql.conf.ext from /etc/dovecot/conf.d/10-auth.conf:

!include auth-sql.conf.ext

Edit /etc/dovecot/conf.d/auth-sql.conf.ext:

passdb {
  driver = sql
  args = /etc/dovecot/dovecot-sql.conf.ext
}

userdb {
  args = uid=105 gid=107 home=/var/mail/domains/%d/%n
  driver = static
}

Edit /etc/dovecot/dovecot-sql.conf.ext:

connect = host=localhost dbname=postfix user=postfix password=password
default_pass_scheme = SHA512-CRYPT
password_query = \
  SELECT username,password \
  FROM mailbox \
  WHERE local_part = '%n' AND domain = '%d'

Allow TCP traffic on the IMAP and POP3 port (143 and 110) and port 4190:

ufw allow imap
ufw allow pop3
ufw allow 4190/tcp

Check whether the Dovecot configuration is valid:

dovecot -n

Start the service:

/etc/init.d/dovecot init
rc-update add dovecot

Check if Dovecot is listening on the IMAP and the POP3 port (143 and 110), as well as the /var/spool/postfix/private/dovecot-lmtp and /var/spool/postfix/private/auth UNIX sockets:

netstat -anp | grep dovecot

Configuring DNS

Add the following DNS records:

_imap._tcp SRV 143 imap.example.com.
_pop3._tcp SRV 110 pop3.example.com.

Thunderbird Add-on

Download and install the Sieve Add-on for Thunderbird. If you go to Tools > Sieve Message Filters, Thunderbird should open the page that lists your server connections. You should be able to connect to the Sieve server and manage the sieve scripts for your accounts.


If you like my work or if my work has been useful to you in any way, then feel free to donate me a cup of coffee. Any donation is much appreciated!