How To Set Up Mailgun as an SMTP Relay on Debian 9

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 Debian 9 server up to send mail using Postfix and Mailgun’s SMTP relay.

Prerequisites:

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

Step-by-step

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 to date:In this guide, we’ll walk through setting your Debian 9 server up to send mail using Postfix and Mailgun’s SMTP relay.

apt-get update

Then run the following command to install mailutils:

apt install mailutils

Mailutils contains a collection of mail related programs for your server.

2) Next, we need to install Postfix. Postfix is a free and open-source mail transfer agent that routes and delivers e-mail. Execute the following command to install Postfix:

apt install postfix

3) 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.

4) 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:

5) 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/main.cf

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.mailgun.org
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:postmaster@maildomain.org:defaultpassword123
smtp_sasl_security_options = noanonymous

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

systemctl restart postfix

7) Test sending an email by entering the following command, which uses the ‘mail’ command from our earlier mailutils install. Be sure to replace ‘testaddress@domain.com’ 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" testaddress@domain.com

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.

Conclusion

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 *