How To Set Up Mailgun as an SMTP Relay on Ubuntu 20.04

Mailgun is a powerful transactional e-mail relay service and API that allows you to send, receive, and track e-mails through their trusted relay servers. Mailgun is capable of integrating services like postfix, sending e-mail through an SMTP relay provided by Mailgun, and integrating with your existing applications. For example, Mailgun, when properly configured, can deliver emails from a CMS such as WordPress.

In this guide, we’ll walk through setting your Ubuntu 20.04 server up to send mail using Postfix and Mailgun’s SMTP relay.


A Mailgun account with a custom domain.
• Access to a Linux server running Ubuntu 20.04 and root or sudoer privileges.


1) SSH into your server as root or a user with equivalent permissions. Once you’ve logged in, first make sure your server’s repositories are up todate:

apt update

Then run the following command to install mailutils:

apt install mailutils

Mailutils contains a collection of mail related programs for your server and Postfix is installed alongside it.

2) Postfix will prompt you with a full-screen form asking for some information from you. In the first prompt, choose Internet Site for the type of mail configuration. In the second prompt, enter your System Mail address. Once you’ve followed through the prompts, it will bring you back to the command line.

3) Now we need to configure Postfix to send mail using our Mailgun SMTP relay. In your Mailgun control panel, navigate to Sending > Domains  then click on the domain name you want to use as the domain sending from Postfix. Now click Select under SMTP. You’ll receive a box of information below which we will grab some information from to configure out Postfix setup. Copy the SMTP hostname, the username, and the Default password provided, as shown in the screenshot below:

4) Now we need to edit the actual Postfix config file to have it use our Mailgun SMTP server. Execute the following command to open the config file with the vi text editor:

nano /etc/postfix/

Now, at the bottom of that file, place the following block of text. Note that I’ve entered the username and password for my Mailgun domain, so be sure to replace those with your own.

relayhost =
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps =
smtp_sasl_security_options = noanonymous

5) Now let’s restart Postfix to put the changes into place. Execute the following command:

systemctl restart postfix

6) Test sending an email by entering the following command, which uses the ‘mail’ command from our earlier mailutils install. Be sure to replace ‘’ with a valid address you want to try and send an email to.

echo "This is the body of the email" | mail -s "This is the subject line"

Now in your Mailgun portal, under the Sending section, click Logs and you should see the log entry for the email you sent. You also should receive the email in the mailbox you entered at the end of the above command.


There are a lot of other ways to send email from your server, and you can even use Mailgun’s API to integrate its functions directly into your application. For further reading, check out Mailgun’s Help Center.

Leave a Reply

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