How To Set Up Mailgun as an SMTP Relay on Fedora 30

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 Fedora 30 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 Fedora 30 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, run the following command to install mailx:

dnf -y install mailx

Mailx is a Mail User Agent which provides support for several protocols such as IMAP, POP3 and SMTP.

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:

dnf -y install postfix

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:

vi /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

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 mailx 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.

Be sure to set postfix to start automatically upon bootup of your server if you don’t want to manually start the service after each reboot:

systemctl enable postfix

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 *