24

I am trying to setup emails with my own website. Let's say the domain name is example.com.

The name server in use is digital ocean and I also have a gmail account linked to the same (say using contact@example.com).

While setting up things with mailgun, I used mg.example.com (as they said it would also let me email using the root domain). The verification step is done and I can send email using contact@mg.example.com.

However, trying to use the root domain (contact@example.com) gives the following error:

AnymailRequestsAPIError: Sending a message to me@gmail.com from contact@example.com
ESP API response 404:
{
"message": "Domain not found: example.com"
}

How do I resolve this issue?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
sudshekhar
  • 1,576
  • 3
  • 18
  • 31

7 Answers7

83

I got the same error when I copy-pasted the curl example from Mailgun help page.

My domain was set to EU region, and I had to set the api domain to api.eu.mailgun.net instead of api.mailgun.net.

Boom! Working! :)

wiktor
  • 1,605
  • 16
  • 13
  • Thanks! The curl example of their docs also includes multiple `to` fields. – Stuck May 07 '19 at 12:24
  • Well, to me it seemed to be this, but now the tutorial shows `mxa.eu.mailgun.org` and `mxb.eu.mailgun.org.` instead of `api.mailgun.net`, and I wonder, what is now the right behavior, are your api domain outdated ? – Dimitri Kopriwa May 07 '20 at 12:01
48

I am using the EU region with Mailgun and have run into this problem myself. My implementation is a Node.js application with the mailgun-js NPM package.

EU Region Implementation:

const mailgun = require("mailgun-js");
const API_KEY = "MY_API_KEY";   // Add your API key here
const DOMAIN = "my-domain.example"; // Add your domain here
const mg = mailgun({
    apiKey: API_KEY,
    domain: DOMAIN,
    host: "api.eu.mailgun.net"  // -> Add this line for EU region domains
});
const data = {
    from: "Support <support@my-domain.example>",
    to: "recipient@example.com",
    subject: "Hello",
    text: "Testing some Mailgun awesomness!"
};
mg.messages().send(data, function(error, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(body);
    }
});

Further options for the mailgun() constructor can be found here.

Thought I'd share a full answer for anybody that's still confused. Additionally, Mailgun Support was kind enough to supply the following table as a reference guide:

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
ProGrammer
  • 976
  • 2
  • 10
  • 27
  • 2
    Thank you for this. I was pulling out my hair trying to figure out why this is not working. Adding the `host` property fixed it for me. – henk.io Jul 10 '19 at 21:16
  • Absolute legend, took about an hour to find this but solved all hy issues in a single line of code (the fact that EU domains need the host property in the config is not obvious!) – Goddak Nov 10 '19 at 20:02
  • Thanks! Same experience here: didn't realise the `host` property defaults to the US domain, so I was getting "domain not found' until I added it with along with the EU mailgun host. – cloudworks Dec 07 '19 at 11:29
  • Finally: the **host** key!! - Thanks! It's always a great idea to read the package documentation. It would have saved me hours! – Kossi D. T. S. Dec 18 '19 at 11:26
8

IF:

  • your domain is an EU domain AND
  • you're using django-anymail as in Rob's answer above

THEN the ANYMAIL setting (in your Django project settings) should specify the API_URL to be the EU one, example:

ANYMAIL = {
    'MAILGUN_API_KEY': '<MAILGUN_API_KEY>',
    'MAILGUN_SENDER_DOMAIN': 'example.eu',
    'MAILGUN_API_URL': 'https://api.eu.mailgun.net/v3'  # this line saved me!
}

Before adding the MAILGUN_API_URL I was getting this error:

AnymailRequestsAPIError: Sending a message to xxx@example.com from noreply@example.eu <noreply@example.eu>
Mailgun API response 404 (NOT FOUND):
{
  "message": "Domain not found: mailgun.example.eu"
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
5

Update 8/22/16: Anymail has been updated to take a new MAILGUN_SENDER_DOMAIN in settings.py. See version .5+ docs.

-- Original Answer You did not post your code for how you're sending your email, but you are probably trying to send using the simple send_mail() function:

from django.core.mail import send_mail

send_mail("Subject", "text body", "contact@abc.example",
          ["to@example.com"],)

When you use this method, Anymail pulls the domain out of your From address and tries to use this with Mailgun. Since your From address (abc.example) doesn't include the subdomain mg., Mailgun is confused.

Instead, you need to send the email using the EmailMultiAlternatives object and specify the Email Sender Domain like so:

from django.core.mail import EmailMultiAlternatives

msg = EmailMultiAlternatives("Subject", "text body",
                             "contact@abc.example", ["to@example.com"])
msg.esp_extra = {"sender_domain": "mg.abc.example"}

msg.send()

Don't forget the brackets in your To field, as this needs to be a tuple or list even if you're only sending it to one recipient.

For more information, see Anymail docs on esp_extra.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Rob
  • 1,656
  • 2
  • 17
  • 33
  • Please note, a new setting is being added to AnyMail that makes this more straight-forward. See comments in [this issue](https://github.com/anymail/django-anymail/issues/26#issuecomment-237367993). – Rob Aug 03 '16 at 21:43
2

Struggled for days with correct DNS settings and finally found as @wiktor said, i needed to add "eu" to api endpoint to make it work. Its actually also documented here: https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions

Sorry for replying as an answer, dont have enough rep to add comment :(

Kasper K
  • 89
  • 5
0

I had the same problem: 404 error, domain not found.

The cause The EU region selection for the domain on Mailgun

The solution Change the region from EU back to the default of US.

Since I had not used the domain at all up to this point, I simply deleted it, re-added it, then changed my TXT, MX and CNAME records (for example, mailgun.org instead of eu.mailgun.org) at the domain registrar (which was GoDaddy in my case).

Darren Murphy
  • 1,076
  • 14
  • 12
0

I found my fix with this change:

ANYMAIL = {
   ...
   'MAILGUN_SENDER_DOMAIN': 'example.com', # Using the sending domain in Mailgun
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
James Broad
  • 1,210
  • 12
  • 17