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 CentOS 7 server up to send mail using Postfix and Mailgun’s SMTP relay.
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:
yum -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:
yum -y install postfix
3) If Postfix prompts you with a full-screen form (it’s pink if you are using a color terminal) asking for some information from you, choose Internet Site for the type of mail configuration. On the remaining prompts, read the details and then provide the information in the field given. It will ask you to enter the System Mail address, the root/postmaster mail recipient address, and the domains for which this server would accept mail. You’ll also be prompted for the mailbox size in bytes for any mailboxes you create, and whether you’d want to use IPv4/IPv6. 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:
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:firstname.lastname@example.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 ‘email@example.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" firstname.lastname@example.org
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
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.