Configuring Mailgun as a mail relay allows Postfix to forward emails destined for remote delivery.
Connect to your instance using SSH
Install the Postfix Mail Transport Agent. When prompted, accept the default choices for domain names but select the
Local Only configuration.
apt-get update && apt-get install postfix libsasl2-modules -y
Modify the Postfix configuration options. Postfix configuration options are set in the
main.cf file. Open the file with the text editor of your choice:
If they exist, comment out the following lines:
# default_transport = error # relay_transport = error
Add the Mailgun SMTP service by adding the following line to the end of the file:
relayhost = [smtp.mailgun.org]:2525
Note: You must use port 2525, as port 25 is not allowed on any plan.
Next, add the following lines to enforce SSL/TLS support and to configure STMP authentication for these requests.
A simple access and security layer (SASL) module handle authentication in the Postfix configuration. Add the following lines to the end of the file:
smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous
Save your changes and close the file.
Generate the SASL password map:
postmap utility to generate a
postmap /etc/postfix/sasl_passwd ls -l /etc/postfix/sasl_passwd*
Next, remove the file containing your credentials as it is no longer needed:
Set the permissions on your
chmod 600 /etc/postfix/sasl_passwd.db ls -la /etc/postfix/sasl_passwd.db
Finally, reload your configuration to load the modified parameters:
Test your configuration. Install the
mailutils package and test your configuration.
apt-get install mailutils -y
Send a test message:
echo 'Test passed.' | mail -s Test-Email EMAIL@EXAMPLE.COM
Look in your systems logs for a status line containing
status and the successful server response code
tail -n 5 /var/log/syslog
For more detailed examples and information about many other topics including tracking and routing messages, read the Mailgun documentation.