Mail Server

This walk-through will setup mail server that only accept connection from localhost only for Ubuntu Server 16.04 LTS.

True and right mail server is a pain in the ass to setup, the email we will send from this mail server is highly likely will end up in spam folder because we didn’t set SPF and DKIM.

Installation

Install postfix then choose Internet Site for mail configuration and type your_domain for System mail name.

$ sudo apt-get install mailutils

After installation finished, configure postfix. Edit /etc/postfix/main.cf while leaving the other untouched, change these lines.

mydestination = $myhostname, localhost.$mydomain, $mydomain
inet_interfaces = loopback-only
inet_protocols = ipv4

Sometimes, your ISP block connection to port 25 for security reason. Usually in ADSL connection. If you see No route to host in /var/log/mail.log you should set relay host.

relayhost = <YOUR ISP SMTP SERVER>

Finally, restart Postfix.

$ sudo systemctl restart postfix

Send Mail

You should also set return address if your linux machine hostname is not a qualified domain.

$ echo "Message" | mail -s "Subject" <TO> -r <FROM>

Notes

... follow error message ...
$ sudo tail -f /var/log/mail.log

... see mail queue ...
$ mailq

... delete all mail in queue ...
$ sudo postsuper -d ALL

Development

$ sudo python -m smtpd -n -c DebuggingServer localhost:25