Using Mailgun to Send Emails Through Exim on a WHM Installation

Using Mailgun to Send Emails Through Exim on a WHM Installation

Email is hard. It requires setting up a mail server, DNS records (MX/SPF/DKIM/DMARC), and contacting the hosting provider to create an RDNS record. And after all that, email providers like Yahoo or Hotmail/MSN/Outlook.com (all hosted by Microsoft) may still decide to block your emails which is very frustrating.

There are 3rd party email-sending services that will help with this including Mailgun, SendGrid, Amazon Simple Email Services, and more. In a perfect world, because it's their job to send emails that means they have the resources needed to ensure email delivery. Given that, I figured I should give one of them a try and went with Mailgun's free tier. (Click here to learn how to set up a Mailgun account.)

Mini Mailgun Review

Overall, I've been happy with their support. They are responsive to tickets and treat me like a human being, which I appreciate. The email sending service, however, hasn't been the grand slam I was hoping it would be.

Emails still get blocked by some recipients (school districts, for example, are notorious for bouncing emails) and instead of getting bounce messages in my inbox, I have to pay close attention to the Mailgun sent messages report (which are only available for a limited time on the free tier) on their website.

Free accounts use shared IP addresses, so when another account holder that's on the same IP address as you goes on an email-sending spree, Yahoo (and others) might deem the IP address as suspect and hold your legit emails for a pre-determined amount of time. Translation: they will delay delivery of your emails.

While this typically resolves itself, it still creates an unnecessary bottleneck. If you don't want to wait, you can open a support ticket and ask them to change your IP address -- they have honored this request every time I've made it (top tip: include fail records from your sent message reports for credibility).

Could the IP address issues be resolved by upgrading to a paid account (which gives you a dedicated IP address)? Likely. But the blocking problem (school districts) still exists so overall I was left a little disappointed by the performance of the solution during my proof of concept testing.

That said, you might still want to give it a try. Given how easy it is to set up a Mailgun account and configure Exim, I think it's worth it to see if this works for your projects/needs.

Configuration Summary

This configuration assumes you are running WHM on your web server, have access to edit your DNS records, can set up a Mailgun account, and want all sent emails from your web server to route through Mailgun. If you'd prefer that Mailgun is only used for a particular application you can use their APIs instead of configuring Exim.

This also assumes your domain is the only one being hosted on this WHM install. If you have multiple domains on this server, editing these configurations will affect the sent mail for all domains that are using Exim.

Configuration Steps

  1. Create a Mailgun account
  2. Add your domain to your Mailgun account
  3. Click on "Domains" and save your credential info for later (Default SMTP Login and Default Password)
  4. Scroll down to "Domain Verification & DNS" and make note of the TXT and CNAME records that need to be added
    • You don't need to add the MX records if you're only sending (not receiving) email through Mailgun
  5. Add the Mailgun DNS records (if you're not using WHM to manage your DNS records you'll need to apply this information to your DNS record holder)
    • Log into WHM
    • Go to "Edit DNS Zone"
    • Select the domain you wish to edit
    • Add the DNS records that are displayed on the Mailgun domain details page
    • Save your changes
  6. Verify the domain by clicking the "Check DNS Records Now" button in Mailgun's domain details page (it could take a few minutes for the DNS records to propagate, be patient [unlike me *click click click* 😅])

Assuming everything above is working, it's time to edit your Exim configuration. Please note, it's always good to make a copy of your Exim configuration before you change it (backups are our friends).

  1. Head back to WHM and open the Exim Configuration Manager
  2. Click on "Advanced Editor"
  3. Search for "Section: AUTH" and add the following (YourMailGunEmailAddress is the Default SMTP Login and YourMailgunDomainPassword is the Default Password from step 3 above):
    1. mailgun_login:
    2. driver = plaintext
    3. public_name = LOGIN
    4. hide client_send = : YourMailgunEmailAddress : YourMailgunDomainPassword
  4. Search for "Section: PREROUTERS" and add the following:
    1. mailgun:
    2. driver = manualroute
    3. domains = ! +local_domains
    4. transport = mailgun_transport
    5. route_list = * smtp.mailgun.org byname
  5. Search for "Section: TRANSPORTSTART" and add the following:
    1. mailgun_transport:
    2. driver=smtp
    3. hosts_require_auth = *
  6. Scroll alllllll the way down to the bottom and click save
  7. Send a test email from your system
  8. Navigate to the inbox that just received that email, view the headers, and search on "mailgun" -- if there's a match you're good to go! If not, you may need to open a ticket with Mailgun support to determine if there's an error in the configuration.

Closing Thoughts

I have to give credit where it's due. The folks at Grepitout put this tutorial together which was the second configuration I tried. While it fixed most of the issues I had with the first configuration, it didn't fully work. It was the folks at Mailgun support who suggested changing that very last line to "hosts_require_auth = *" and that did the trick.

If you need to revert, simply delete the elements we added to the Exim configuration and save those changes. If you decide this solution is not for you, then I'd suggest going back through the DNS server to clean up the additional records that we added as well.

Lastly, if you're reading this and your eyes are rolling into the back of your head because it's just too much (don't feel bad, this is a lot) feel free to contact me to assist -- I'm always happy for the opportunity to help a client out.

Happy emailing!

Supplemental update, Amazon SES/Exim configuration instructions can be found here.

Want to be notified when a new post goes live?

Thanks for reading! If you have any thoughts, questions, or anything else please reach out. I always enjoy hearing from people.