Install Postfix Mail Server

Root Access

Log in as root. Open the terminal and check if the hash sign # is there. If yes, you are root and don't need to type sudo first.

root@vps#

apt update
apt upgrade

Postfix & Dovecot Installation

1. Preparation

Check your DNS settings:

dig mx checktls.nl

2. Install Postfix

apt install postfix

Choose Internet site and provide your mail server domain name (e.g. checktls.nl).

3. Install Mailutils

apt install mailutils

4. Create User

Create a user for info@checktls.nl:

useradd -m -s /bin/bash info
passwd info

5. Verification

Check if everything works:

postfix status
telnet localhost 25
quit

Test Sending Mail (Local)

Test sending an internal email from root to info.

telnet localhost 25
ehlo localhost
mail from: root@localhost
rcpt to: info@localhost
data
Subject: My first mail on Postfix

Hi,
Are you there?
regards,
Admin
.
quit

(Type the . on a new line and press Enter to send)

Check if the email arrived:

su - info
mail

Back to root:

su - root

Postfix Configuration (Maildir)

Configure Postfix to use Maildir format.

postconf -e "home_mailbox = Maildir/"
postconf -e "mailbox_command = "
/etc/init.d/postfix restart

Check again as user info:

su - info
MAIL=/home/info/Maildir
mail

Courier & IMAP/POP3 Setup

apt install courier-pop
apt install courier-imap

Configure Postfix parameters:

postconf -e "mydestination = mail.checktls.nl, localhost.localdomain, localhost, checktls.nl"
postconf -e "inet_interfaces = all"
postconf -e "inet_protocols = all"

Start services:

service courier-authdaemon start
systemctl enable courier-authdaemon
/etc/init.d/postfix restart

Test Sending Mail (External)

netcat mail.checktls.nl 25

ehlo checktls.nl
mail from: root@checktls.nl
rcpt to: info@checktls.nl
data
Subject: My first mail for my domain

Hi,
Are you there?
regards,
Admin
.
quit

Check receipt via POP3:

su - info
cd Maildir/new
ls

netcat mail.checktls.nl 110
user info
pass password
quit

SSL Certificate (LetsEncrypt)

Generate a certificate:

certbot certonly --standalone -d mail.checktls.nl

Configure Postfix for TLS:

postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/mail.checktls.nl/fullchain.pem'
postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/mail.checktls.nl/privkey.pem'

SASL Authentication (Dovecot)

Connect Postfix to Dovecot SASL:

postconf -e 'smtpd_sasl_type = dovecot'
postconf -e 'smtpd_sasl_path = private/auth'
postconf -e 'smtpd_sasl_local_domain ='
postconf -e 'smtpd_sasl_security_options = noanonymous'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'

TLS Security Settings

Harden the security:

sudo postconf -e 'smtp_tls_security_level = may'
sudo postconf -e 'smtpd_tls_security_level = may'
sudo postconf -e 'smtp_tls_note_starttls_offer = yes'
sudo postconf -e 'smtpd_tls_loglevel = 1'
sudo postconf -e 'smtpd_tls_received_header = yes'

Virtual Domains & Aliases

sudo postconf -e 'virtual_alias_domains = $mydomain'
sudo postconf -e 'virtual_alias_maps = hash:/etc/postfix/virtual'

Edit the virtual map:

sudo nano /etc/postfix/virtual

Add:

postmaster@checktls.nl root
root@checktls.nl root
info@checktls.nl info
bas@checktls.nl bas

Activate the map:

sudo postmap /etc/postfix/virtual
sudo systemctl restart postfix

Dovecot IMAP/POP3 Installation

If you want to use Dovecot instead of Courier:

sudo apt install dovecot-imapd dovecot-pop3d

Create maildirs in /etc/skel so new users get them automatically:

sudo maildirmake.dovecot /etc/skel/Maildir
sudo maildirmake.dovecot /etc/skel/Maildir/.Drafts
sudo maildirmake.dovecot /etc/skel/Maildir/.Sent
sudo maildirmake.dovecot /etc/skel/Maildir/.Trash
sudo maildirmake.dovecot /etc/skel/Maildir/.Templates

Set Permissions

For existing user (e.g. bas):

sudo adduser bas sudo
sudo cp -r /etc/skel/Maildir /home/$USER/
sudo chown -R $USER:$USER /home/$USER/Maildir
sudo chmod -R 700 /home/$USER/Maildir
sudo adduser $USER mail

For root:

sudo cp -r /etc/skel/Maildir /root/
sudo chown -R root:root /root/Maildir
sudo chmod -R 700 /root/Maildir

Set environment variables:

echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc

Dovecot Configuration

Auth Config: /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = yes
auth_mechanisms = plain login

Mail Location: /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir

Master Config: /etc/dovecot/conf.d/10-master.conf

service imap-login {
  inet_listener imap {
    port = 143
  }
}
service pop3-login {
  inet_listener pop3 {
    port = 110
  }
}

service auth {
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

SSL Config: /etc/dovecot/conf.d/10-ssl.conf

ssl = required
ssl_cert = </etc/letsencrypt/live/mail.checktls.nl/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.checktls.nl/privkey.pem
User