Get Started

Hosting

Get Started

Service
Mailgun Relay on Postfix

How To Set Up an SMTP Relay with Postfix and Mailgun on Ubuntu Server

Introduction

This guide will help you set up an SMTP relay with Postfix using the Mailgun transactional mail service. Postfix is the standard SMTP service running on most Linux servers. The guide uses Ubuntu server as an example, but the process is similar on all Linux distributions.

Why I choose Mailgun? This is because I find it easy to configure, reliable and suitable for my needs. There are other providers you can choose from, such as SendGrid, that offers the same service.

Mailgun offers a free plan that allows you to send 100 emails a day. This is plenty for most websites needed only transactional emails, such as notifications when a customer completes a form, etc. If you plan on doing email marketing, you’ll need a paid plan.

Configure Mailgun for SMTP relay

Add your domain to Mailgun

When you see your Dashboard, go to ‘Send’ → ‘Sending’ → ‘Domains’ and click ‘Add new domain’. Type in ‘mg.yourdomain.com’ (replace with your actual domain). Under ‘DKIM key length’ choose ‘2048’ for maximum security.

Add new domain on Mailgun
Add new domain on Mailgun

Set up and verify DNS records for Mailgun

From your Dashboard, go to ‘Send’ → ‘Sending’ → ‘Domain settings’ and click on the ‘DNS records’ tab. The ‘sending’ records are the only ones that are strictly necessary, but I recommend you set up all the records, including ‘receiving’ and ‘tracking’.

After you have set up the DNS records on your domain registrar (Cloudflare, GoDaddy, Namecheap, etc.) make sure to click ‘Verify’ to make sure they are set up correctly.

DNS records on Mailgun
DNS records on Mailgun

Set up SMTP credentials on Mailgun

Next, click on the ‘SMTP credentials’ tab. Here you’ll see the postmaster account. You need the password, so click ‘Reset password’ and copy the new password.

As you can see in the screenshot, the SMTP relay domain is ‘smtp.mailgun.org’ and the port we will use is 587 for encrypted transmission.

SMTP credentials on Mailgun
SMTP credentials on Mailgun

Set up Mailgun SMTP relay in Postfix

Next, open your Postfix with this command:

$ sudo nano /etc/postfix/main.cf

Scroll down to the bottom of the file and make sure it looks like below. If any lines are missing from your config file, you need to add them.

Under ‘myhostname’, add the domain you set up in Mailgun. Under ‘relayhost’ add ‘[smtp.mailgun.org]:587’. Then edit ‘smtp_sasl_password_maps’ to match your SMTP credentials on Mailgun.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = mg.yourdomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, speedy, localhost.localdomain, , localhost
relayhost = [smtp.mailgun.org]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

# set the authentication using static method:
smtp_sasl_password_maps = static:postmaster@mg.yourdomain.com:2ea95663970078362ec426bbd180d13e-72e4a3d5-0f8bf8c8
# set sasl authentication on
smtp_sasl_auth_enable = yes
# set sasl security options to no anonymous:
smtp_sasl_security_options = noanonymous

After you have entered all your information, save the file and restart the Postfix service:

$ sudo systemctl restart postfix

Now your system is set up to send SMTP mail!

If no emails are going out after setting up Postfix

Make sure the port you use (in our case, 587) is open in your outgoing firewall rules. Oftentimes, hosting services block SMTP ports to avoid spammers, so if this is the case, you need to contact your provider and ask them to open the port for you.

Do you need help setting up an SMTP relay on your Linux server?

If you do, just reach out. I offer Linux consulting based on your needs. If you are using our managed hosting services, this will already be setup and configured for you.

Leave a Reply

Your email address will not be published. Required fields are marked *