Setting up Mailgun on Digital Ocean

Not having to manage an email server is awesome, thank you Mailgun. I am currently integrating with Digital Ocean and ran into some documentation confusion with DNS records, especially when trying to configure for a subdomain.

After following Mailgun's instructions (including the Digital Ocean guide, alternate guide, and community help) I was able to get it setup.

Note that all links to Digital Ocean are using my referral link.

Configure Your Domain

If you are setting up your account directly against your domain, then things are a bit easier (and the instructions work a little more as expected). If you are setting up against a subdomain, skim this section as the subdomain instructions are a bit different.

New domain on mailgun

I wanted to provide my view of how to get things configured should the shorthand editor in Digital Ocean change between now and when you are reading me.

This is what the Zone file looks like to confirm mydomain.com for sending and receiving emails using Mailgun.

$ORIGIN mydomain.com.
$TTL 1800
mydomain.com. IN SOA ns1.digitalocean.com. hostmaster.mydomain.com. 123412341234 10800 3600 604800 1800
mydomain.com. 1800 IN NS ns1.digitalocean.com.
mydomain.com. 1800 IN NS ns2.digitalocean.com.
mydomain.com. 1800 IN NS ns3.digitalocean.com.
mydomain.com. 1800 IN A 10.20.30.40
mydomain.com. 1800 IN MX 10 mxa.mailgun.org.
mydomain.com. 1800 IN MX 10 mxb.mailgun.org.
mydomain.com. 1800 IN TXT “v=spf1 include:mailgun.org ~all”
k1._domainkey.mydomain.com. 1800 IN TXT “k=rsa; p=ABC1234+DEF/aSDF@!#$/FWAEF”
email.mydomain.com. 1800 IN CNAME mailgun.org.

Digital Ocean's data entry provides shortcuts in the editor, so be sure to leave out mydomain.com, and use the @ symbol where necessary. Here's what the values look like in Digital Ocean's editor

Digital Ocean DNS editor

Please note that 10.20.30.40 is the IP address of your droplet and NOT that provided by mailgun.

Sending Emails (Not Receiving) From Your Sub Domain

If you want to keep your primary domain free and clear from Mailgun, you might consider setting up a subdomain to route all email through.

Using instead a subdomain on mailgun

Please note that in this example I have NOT configured the MX records, and you will not be able to receive emails. This is because at the moment Digital Ocean does not directly support subdomain MX records. If you do configure your MX records, your account will still show as active, but the MX records will not be configured as you expect in Mailgun.

MX records on mailgun

Skip to the next section if you will want to both send AND receive emails from your subdomain. See here and here for discussions about the limitations in the DNS editor.

This is what the Zone file looks like to confirm mydomain.ca with a sub domain of mg.mydomain.ca for sending emails using Mailgun.

$ORIGIN mydomain.ca.
$TTL 1800
mydomain.ca. IN SOA ns1.digitalocean.com. hostmaster.mydomain.ca. 123412341234 10800 3600 604800 1800
mydomain.ca. 1800 IN NS ns1.digitalocean.com.
mydomain.ca. 1800 IN NS ns2.digitalocean.com.
mydomain.ca. 1800 IN NS ns3.digitalocean.com.
mg.mydomain.ca. 1800 IN A 1.2.3.4
mydomain.ca. 1800 IN A 10.20.30.40
mg.mydomain.ca. 1800 IN TXT “v=spf1 include:mailgun.org ~all”
pic._domainkey.mg.mydomain.ca. 1800 IN TXT “k=rsa; p=MIGfMA0GCSqGSASDFASDFASDFBiQKBgQC+v78XOweoD+JGrE8STwHLQaxX310gDHAgK2IaJ/TEGZ4GS5xr/sb/AxX+crzEkMDp9e58PASDFASDFASDFASDFASDFQ9r4I/ni3LDC+ORkBzzmy3syU7UKCN3fRKPba4d8fvIE/GvqTGnuJuwDeLZ8lJfIfwIDAQAB”
email.mg.mydomain.ca. 1800 IN CNAME mailgun.org.

Digital Ocean's data entry provides shortcuts in the editor, so you will want to leave out mydomain.ca, but NOT mg. Here's what the values look like in Digital Ocean's editor.

Digital Ocean editing subdomains in DNS editor

Here's what you need to change for your specific configuration

  • A record for mg should by the IP provided by mailgun (not 1.2.3.4)
  • A record for @ should be your droplets' IP (not mailgun's)
  • TXT record for mg should be wrapped in quotes
  • TXT record for pic._domainkey.mg should also be wrapped in quotes

Sending And Receiving Emails From Your Sub Domain

If you wish to receive emails from mg.mydomain.com, then you have a little bit more work to do.

First, you will need to create an entirely new domain for your subdomain in Digital Ocean

Digital Ocean add a domain

The IP address of 1.2.3.4 should that from Mailgun and NOT your droplet's IP. Here is what the records will look like in editor.

Digital Ocean DNS points to mailgun

In the above the 1.2.3.4 IP address is the one provided by Mailgun, and NOT your DigitalOcean droplet. Once those settings propagate you should be able to receive emails from @mg.mydomain.com.

Mailgun working MX records

Do not copy and paste anything from this post, and instead use the values provided by Mailgun.

Happy Emailing.