Ansible playbook to set up an SMTP mail server with the following components:
- Postfix: A free and open-source Mail Transfer Agent (MTA) that routes and delivers email.
- Dovecot: An open-source IMAP and POP3 server for email retrieval and storage.
Use the following command to set up your mail server:
ansible-playbook -i inventory --user=root --become setup-mailserver.yml --tag setupCheck whether the mail services are running:
systemctl status dovecot
systemctl status postfixWhen developing or testing, you can easily destroy the host environment:
ansible-playbook -i inventory --user=root --become setup-mailserver.yml --tag destroy
⚠️ Note:
Running the playbook without specifying a tag will execute bothsetupanddestroytasks, effectively canceling each other out.
Install mail utilities and send a test email:
sudo apt-get install mailutils -y # For Debian/Ubuntu
echo "This is a test email" | mail -s "Test Email" -r test@smtp.domain.at me@example.com- Set your desired test user in
defaults/main.yml. - On your local client machine, run:
sudo apt-get install swaks -y # For Debian/Ubuntu
swaks -t me@example.com -s mail.example.com:587 -tls -a LOGINYou’ll be prompted for a username and password — enter the credentials of your test user. If successful, an email should arrive at the specified destination address.
Check recent logs for Dovecot or Postfix:
journalctl -u dovecot --since "10 minutes ago"
journalctl -u postfix --since "10 minutes ago"If you’re using Let’s Encrypt for certificates, view the Certbot logs here:
cat /var/log/letsencrypt/letsencrypt.logYou can configure which IPs are allowed to relay mail through the server by editing:
roles/mailserver/defaults/main.ymlThe variable that controls this is mynetworks.
This is used both directly for the permit_mynetworks restriction and to generate the Postfix access table.
# Localhost + Private subnets
mynetworks: 127.0.0.0/8 163.221.23.0/20 11.12.0.0/16 13.109.0.0/20
# IPv4 + IPv6 localhost
mynetworks: 127.0.0.0/8 [::1]/128
# Public single IPs
mynetworks: 122.21.216.176/32 163.221.23.0/32