sudo apt update && sudo apt upgrade -y
sudo apt install postfix dovecot-core dovecot-imapd mailutils -y
Edit the main configuration file:
sudo nano /etc/postfix/main.cf
Add or update these settings:
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
inet_interfaces = all
home_mailbox = Maildir/
Restart Postfix:
sudo systemctl restart postfix
Enable IMAP and Maildir format:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Ensure this setting is present:
mail_location = maildir:~/Maildir
Restart Dovecot:
sudo systemctl restart dovecot
Install Certbot and get a certificate:
sudo apt install certbot -y
sudo certbot certonly --standalone -d mail.yourdomain.com
sudo nano /etc/postfix/main.cf
Add:
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
sudo nano /etc/dovecot/conf.d/10-ssl.conf
Add:
ssl = required
ssl_cert =
Restart services:
sudo systemctl restart postfix dovecot
Add a cron job to renew SSL automatically:
sudo crontab -e
Insert this line:
0 0 * * * certbot renew --quiet && systemctl reload postfix dovecot
Configure Postfix to use Gmail or another relay:
sudo nano /etc/postfix/main.cf
Add:
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
Create credentials:
sudo nano /etc/postfix/sasl_passwd
Add:
[smtp.gmail.com]:587 your-email@gmail.com:your-password
Apply changes:
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl restart postfix