I’ve always wanted a custom mail server. This attempts to cover my endeavors in the topic and serves as a howto guide for setting up a Postfix/Dovecot/Mailgun Server on a DigitalOcean server.
Please note most VPS providers block outbound on port 25, but allow inbound which will allow us to use postfix/dovecot for inbound messages, and I am using postfix/mailgun for outbound emails.
Dovecot Config for the Beginner
Step 1: Make sure to cop that SSL Cert from Letsencrypt!
Install Dovecot.. Make Dovecot Config look like this:
(But first understand this config file is not an end all be all to config files for dovecot. I am 99.99% sure this file will change due to the fact I want a mysql backend setup for virtual domains. This means, yes, I will be setting up a front end for people to buy and host email services. I just need to figure out SMTP authentication with Mailgun first. Their reset password method is quite a beast and I will likely need a wrapper class for it…)
disable_plaintext_auth = no
mail_privileged_group = mail
mail_location = mbox:~/mail:INBOX=/var/mail/%u
userdb {
driver = passwd
}
passdb {
args = %s
driver = pam
}
protocols = " imap"
protocol imap {
mail_plugins = " autocreate"
}
namespace inbox {
# prefix = INBOX. # the namespace prefix isn't added again to the mailbox names.
inbox = yes
# ...
mailbox Trash {
auto = no
special_use = \Trash
}
mailbox Drafts {
auto = no
special_use = \Drafts
}
mailbox Sent {
auto = subscribe # autocreate and autosubscribe the Sent mailbox
special_use = \Sent
}
mailbox "Sent Messages" {
auto = no
special_use = \Sent
}
mailbox Spam {
auto = create # autocreate Spam, but don't autosubscribe
special_use = \Junk
}
mailbox virtual/All { # if you have a virtual "All messages" mailbox
auto = no
special_use = \All
}
}
service auth {
unix_listener /var/spool/postfix/private/auth {
group = postfix
mode = 0660
user = postfix
}
}
ssl=required
ssl_cert = </etc/letsencrypt/live/mail.jmoore53.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.jmoore53.com/privkey.pem
Gotchas
Error Mailbox Sent: Failed to Autocreate Mailbox: Permission Denied
Logs:
Jul 27 19:18:17 mail dovecot: master: Dovecot v2.3.7.2 (3c910f64b) starting up for imap (core dumps disabled) Jul 27 19:18:19 mail dovecot: imap-login: Login: user=
, method=PLAIN, rip=1.1.1.1, lip=1.1.1.1, mpid=26650, TLS, session=<8ap1L3GrPpZKU9sj> Jul 27 19:18:19 mail dovecot: imap(jack)<26650><8ap1L3GrPpZKU9sj>: Warning: autocreate plugin is deprecated, use mailbox { auto } setting instead Jul 27 19:18:22 mail dovecot: imap-login: Login: user= , method=PLAIN, rip=1.1.1.1, lip=1.1.1.1, mpid=26652, TLS, session= Jul 27 19:18:22 mail dovecot: imap(jack)<26652> : Warning: autocreate plugin is deprecated, use mailbox { auto } setting instead Jul 27 19:18:22 mail dovecot: imap(jack)<26652> : Error: Mailbox Sent: Failed to autocreate mailbox: Permission denied Jul 27 19:18:22 mail dovecot: imap(jack)<26652> : Error: Mailbox Spam: Failed to autocreate mailbox: Permission denied
Fix (Give correct ownership and permissions to user):
sudo useradd jack
sudo mkdir /home/jack/mail
sudo chown jack:jack /home/jack/mail
sudo chmod -R 700 /home/jack/mail
Dovecot < Character in SSL Configuration
SSL was unable to create a connection. Was giving the error no CA was signing the certificate however the certificate was already generated.
Running below would barf out an error and prevent any kind of login:
openssl s_client -connect mail.jmoore53.com:993 -quiet
Fix:
ssl=required
ssl_cert = </etc/letsencrypt/live/mail.jmoore53.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.jmoore53.com/privkey.pem
Configuring SMTP
Errors showing something along the lines of (below) for a user’s mailbox:
Jul 27 15:44:41 mail dovecot: imap(jack)<24052>
: Error: Mailbox Spam: Failed to autocreate mailbox: Internal error occurred. Refer to server log for more information. [2020-07-27 15:44:41] Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: file_dotlock_open(/home/jack/mail/.imap/dovecot.list.index.log) failed: Permission denied (euid=1000(jack) egid=1000(jack) missing +w perm: /home/jack/mail/.imap, dir owned by 0:0 mode=0755) Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Couldn't create mailbox list lock /home/jack/mail/.imap/mailboxes.lock: file_create_locked(/home/jack/mail/.imap/mailboxes.lock) failed: safe_mkstemp(/home/jack/mail/.imap/mailboxes.lock) failed: Permission denied Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Mailbox Sent: Failed to autocreate mailbox: Internal error occurred. Refer to server log for more information. [2020-07-27 15:46:23] Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Couldn't create mailbox list lock /home/jack/mail/.imap/mailboxes.lock: file_create_locked(/home/jack/mail/.imap/mailboxes.lock) failed: safe_mkstemp(/home/jack/mail/.imap/mailboxes.lock) failed: Permission denied Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Mailbox Drafts: Failed to autocreate mailbox: Internal error occurred. Refer to server log for more information. [2020-07-27 15:46:23] Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Couldn't create mailbox list lock /home/jack/mail/.imap/mailboxes.lock: file_create_locked(/home/jack/mail/.imap/mailboxes.lock) failed: safe_mkstemp(/home/jack/mail/.imap/mailboxes.lock) failed: Permission denied Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Mailbox Archive: Failed to autocreate mailbox: Internal error occurred. Refer to server log for more information. [2020-07-27 15:46:23] Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Couldn't create mailbox list lock /home/jack/mail/.imap/mailboxes.lock: file_create_locked(/home/jack/mail/.imap/mailboxes.lock) failed: safe_mkstemp(/home/jack/mail/.imap/mailboxes.lock) failed: Permission denied Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Mailbox Trash: Failed to autocreate mailbox: Internal error occurred. Refer to server log for more information. [2020-07-27 15:46:23] Jul 27 15:46:23 mail dovecot: imap(jack)<24052> : Error: Couldn't create mailbox list lock /home/jack/mail/.imap/mailboxes.lock: file_create_locked(/home/jack/mail/.imap/mailboxes.lock) failed: safe_mkstemp(/home/jack/mail/.imap/mailboxes.lock) failed: Permission denied Jul 27 15:46:23 mail dovecot:
Fix:
mkdir -p /home/jack/mail/.imap
chown jack:jack /home/jack/mail/.imap/
Primary Links
- How to Set Up a Mail Relay with Postfix and Mailgun on Ubuntu 16.04
- How To Set Up a Postfix E-Mail Server with Dovecot
- DO - How To Configure a Mail Server Using Postfix, Dovecot, MySQL, and SpamAssassin
- Dovecot - SSL Configuration