Setting Up a Mail Server with Postfix and Dovecot
This guide sets up a basic mail server: Postfix handles sending/receiving (SMTP) and Dovecot handles retrieval (IMAP).
Prerequisites
- A domain with proper DNS: MX record, A record, and rDNS (PTR) configured
- Ports 25, 587, 993 open in your firewall
Step 1 — Install Postfix and Dovecot
apt update
apt install postfix dovecot-core dovecot-imapd -y
During Postfix setup, select Internet Site and enter your domain name.
Step 2 — Configure Postfix
nano /etc/postfix/main.cf
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost, $mydomain
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls=yes
Step 3 — Configure Dovecot
nano /etc/dovecot/dovecot.conf
protocols = imap
mail_location = maildir:~/Maildir
ssl = required
ssl_cert = </etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
Step 4 — Set Up DNS Records for Deliverability
# SPF:
TXT @ "v=spf1 ip4:YOUR_SERVER_IP ~all"
# DKIM: Install opendkim and add the public key
apt install opendkim opendkim-tools -y
Step 5 — Open Firewall Ports
ufw allow 25 # SMTP
ufw allow 587 # Submission
ufw allow 993 # IMAPS
ufw allow 110 # POP3 (optional)
Step 6 — Restart Services
systemctl restart postfix dovecot
Test with swaks
apt install swaks -y
swaks --to test@gmail.com --from you@yourdomain.com --server localhost