Configuring Mailgun via SMTP


#1

Requirements:

  1. Postfix
  2. Mailgun Account
  3. Domain

Create a Mailgun account.

Follow the guide to add your domain to the account.

Once your domain is added and verified, head over to the Domains page, select your domain, and note the Default SMTP Login and Default Password
If you want to set up a new account, click Manage SMTP Credentials, and use the account credentials later as the account username and password.

SSH into your server.
Open up /etc/postfix/main.cf with a text editor.

Add the following lines:

##start mailgun settings
smtp_sasl_auth_enable = yes 
smtp_sasl_password_maps = static:USERNAME:PASSWORD
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_tls_loglevel = 1
relayhost = [smtp.mailgun.org]:2525
##end mailgun settings

Replace USERNAME and PASSWORD with the credentials you received from mailgun.

Reload Postfix configurations: sudo postfix reload

Test sending an email by writing a php script, or by using the emergence command line tool. (Follow this guide for more information)

In emergence shell or php script type:
\Emergence\Mailer\Mailer::send("youremail@example.com", "Email Subject", "Email Body", "support@example.com");

Replace youremail@example.com with an email account that can receive emails.

Replace support@example.com with an email address from your domain
Note: this account does not have to exist, or be able to receive email.

If you receive the email, you’re good to go!


#2

Remember to remove the existing relayhost entry if it already exists in the main.cf. I got the following error before I removed it:

postfix: warning: /etc/postfix/main.cf, line 52: overriding earlier entry: relayhost=