In this tutorial, we will setup a Postfix Email/SMTP server and a Dovecot IMAP server. We then configure the SSL certificate to enforce the security/privacy.
This is the communication path we will setup:
- Send: Your Email client -> Postfix SMTP server -> Forward to other email address.
- Receive: Send from other Email server -> Postfix receive the email -> Put into Dovecot directory -> you receive Email through IMAP protoco.
If you are look for a quick working example, just jump to my configuration.
Dovecot IMAP Server
Where we Store Mails?
We store the mails based on domain and user:
mail_location = maildir:/%d/%n/Maildir
mail_attribute_dict = file:/%d/%n/Maildir/dovecot-attributes
%d
indicate domain, and %n
indicate username. For more details, take a look at Dovecot VirtualUsers.
Configure the User and Password
Dovecot supports multiple auth methods. We use the very basic Passwd-file authenticate method.
passdb {
args = username_format=%n /%d/passwd
driver = passwd-file
}
userdb {
args = username_format=%n /%d/passwd
driver = passwd-file
}
The passwd
file use the format similar to /etc/passwd
. Example:
i:{PLAIN}password:1000:1000::/janzhou.org/i/:/bin/bash
Secure Dovecot
It is highly suggested to use ssl certificate to secure your IMAP transfer. Otherwise your email will show to the public:
ssl_cert = </janzhou.org/apache2/namecheap/janzhou_org.pem
ssl_key = </janzhou.org/apache2/namecheap/janzhou_org.key
In the setup process, you can temporary disable the ssl to debug the configuration:
ssl = no
Postfix Server
Use Dovecot Auth
We use dovecot auth in Postfix:
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
queue_directory = /var/spool/postfix
/var/spool/postfix/private/auth
is where Dovecot and Postfix communicate. To create this file, we need to config Dovecot:
service auth {
unix_listener /var/spool/postfix/private/auth {
group = postfix
mode = 0660
user = postfix
}
}
Then restart Dovecot.
Store Email to Dovecot
We store emails to Dovecot when Postfix receives them:
mailbox_command =
mailbox_transport = lmtp:unix:private/dovecot-lmtp
mailbox_size_limit = 0
home_mailbox = Maildir/
local_recipient_maps =
We need to enable lmtp
service in Dovecot:
service lmtp {
inet_listener lmtp {
address = 127.0.0.1
port = 2424
}
unix_listener /var/spool/postfix/private/dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
}
}
Secure Postfix
To secure SMTP and mail receive service:
smtpd_use_tls=yes
smtpd_tls_cert_file=/janzhou.org/apache2/namecheap/janzhou_org.pem
smtpd_tls_key_file=/janzhou.org/apache2/namecheap/janzhou_org.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
Configure Submission and SMTP
In /etc/postfix/master.cf
, uncomments configurations for submission and smtps:
submission inet n - - - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_security_options=noanonymous
-o smtpd_sasl_local_domain=$myhostname
-o smtpd_sender_login_maps=hash:/janzhou.org/mail/passwd
-o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - - - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
-o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
Quick Example
/etc/dovecot/dovecot.conf
# 2.2.13: /etc/dovecot/dovecot.conf
# OS: Linux 3.2.0-4-amd64 x86_64 Debian 8.2 ext4
auth_mechanisms = plain login
auth_verbose = yes
disable_plaintext_auth = no
log_path = /var/log/dovecot.log
mail_attribute_dict = file:/janzhou.org/mail/%n/Maildir/dovecot-attributes
mail_debug = yes
mail_location = maildir:/janzhou.org/mail/%n/Maildir
passdb {
args = username_format=%n /janzhou.org/mail/passwd
driver = passwd-file
}
protocols = imap lmtp
service auth {
unix_listener /var/spool/postfix/private/auth {
group = postfix
mode = 0660
user = postfix
}
}
service lmtp {
inet_listener lmtp {
address = 127.0.0.1
port = 2424
}
unix_listener /var/spool/postfix/private/dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
}
}
ssl_cert = </janzhou.org/apache2/namecheap/janzhou_org.pem
ssl_key = </janzhou.org/apache2/namecheap/janzhou_org.key
userdb {
args = username_format=%n /janzhou.org/mail/passwd
driver = passwd-file
}
verbose_ssl = yes
protocol imap {
imap_metadata = yes
}
/etc/postfix/main.cf
# /etc/postfix/main.cf
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myhostname = janzhou.org
myorigin = /etc/mailname
mydestination = janzhou.org, localhost.org, , localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
relayhost =
recipient_delimiter = +
inet_interfaces = all
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
queue_directory = /var/spool/postfix
mailbox_command =
mailbox_transport = lmtp:unix:private/dovecot-lmtp
mailbox_size_limit = 0
home_mailbox = Maildir/
local_recipient_maps =
smtpd_use_tls=yes
smtpd_tls_cert_file=/janzhou.org/apache2/namecheap/janzhou_org.pem
smtpd_tls_key_file=/janzhou.org/apache2/namecheap/janzhou_org.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
/etc/postfix/master.cf
#
# Postfix master process configuration file. For details on the format
# of the file, see the master(5) manual page (command: "man 5 master" or
# on-line: http://www.postfix.org/master.5.html).
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n - - - - smtpd
#smtp inet n - - - 1 postscreen
#smtpd pass - - - - - smtpd
#dnsblog unix - - - - 0 dnsblog
#tlsproxy unix - - - - 0 tlsproxy
submission inet n - - - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_security_options=noanonymous
-o smtpd_sasl_local_domain=$myhostname
-o smtpd_sender_login_maps=hash:/janzhou.org/mail/passwd
-o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - - - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
-o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
#628 inet n - - - - qmqpd
pickup unix n - - 60 1 pickup
cleanup unix n - - - 0 cleanup
qmgr unix n - n 300 1 qmgr
#qmgr unix n - n 300 1 oqmgr
tlsmgr unix - - - 1000? 1 tlsmgr
rewrite unix - - - - - trivial-rewrite
bounce unix - - - - 0 bounce
defer unix - - - - 0 bounce
trace unix - - - - 0 bounce
verify unix - - - - 1 verify
flush unix n - - 1000? 0 flush
proxymap unix - - n - - proxymap
proxywrite unix - - n - 1 proxymap
smtp unix - - - - - smtp
relay unix - - - - - smtp
# -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq unix n - - - - showq
error unix - - - - - error
retry unix - - - - - error
discard unix - - - - - discard
local unix - n n - - local
virtual unix - n n - - virtual
lmtp unix - - - - - lmtp
anvil unix - - - - 1 anvil
scache unix - - - - 1 scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent. See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop unix - n n - - pipe
flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# Recent Cyrus versions can use the existing "lmtp" master.cf entry.
#
# Specify in cyrus.conf:
# lmtp cmd="lmtpd -a" listen="localhost:lmtp" proto=tcp4
#
# Specify in main.cf one or more of the following:
# mailbox_transport = lmtp:inet:localhost
# virtual_transport = lmtp:inet:localhost
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus unix - n n - - pipe
# user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
# Old example of delivery via Cyrus.
#
#old-cyrus unix - n n - - pipe
# flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
uucp unix - n n - - pipe
flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail unix - n n - - pipe
flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp unix - n n - - pipe
flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix - n n - 2 pipe
flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman unix - n n - - pipe
flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
${nexthop} ${user}