Using Mailgun to send System Mail in Ubuntu 14.04 LTS

After running a mail server for a number of years I got fed up of managing spam and unreliable delivery for system mail. Expanding my recent of use mailgun for web applications I have now set it up to handle system mail. There was not a great deal on there on doing it with Ubuntu so here is a concise guide:

For simplicity I will assume you donโ€™t have an existing mailserver. If you do it may be easier to remove it using the purge option and start again.

  • Install the mail server by running: apt-get install postfix libsasl2-modules
  • Now you need to configure post fix edit the file in nano: sudo nano /etc/postfix/main.cf
  • Add the following lines to the main.cf file:

smtp_sasl_auth_enable = yes relayhost = smtp.mailgun.org smtp_sasl_security_options = noanonymous smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd

  • Create the sasl__passwd file in nano: sudo nano /etc/postfix/sasl_passwd
  • Enter the following line: smtp.mailgun.org username@domain.com:secretpassword
  • Set the permissions for the sasl_passwd file:
    sudo chmod 600 /etc/postfix/sasl_passwd
  • Postmap the file: sudo postmap /etc/postfix/sasl_passwd
  • Restart postfix sudo service postfix restart

Finished! A easy way of routing all your system mail through postfix. You will need to ensure you have added your custom domain into mailgun. For example if your server is called: box.howson.me then add this as a custom domain to mailgun. If some mail is sent as root@howson.me from the server donโ€™t worry this configuration will still work and is simple as it does not interfere with the mail provider serving your domain. Even DKIM and SPF work without any changes.