Mailgun delivery issues when sender and recipient domains match

I’m having trouble with my Mailgun setup and emails aren’t being delivered when I send to addresses that use the same domain as my Mailgun configuration. Everything works fine when sending to different domains though.

For instance, if my Mailgun domain is set to example.org, sending emails to [email protected] fails to deliver. The Mailgun dashboard shows the messages as accepted but they never reach the inbox.

I suspect this might be a configuration problem rather than a coding issue. Here’s my implementation:

const mailgunClient = require('mailgun-js')({
  apiKey: process.env.MG_API_KEY,
  domain: process.env.MG_DOMAIN,
  host: process.env.MG_HOST
})

const emailData = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Test Message',
  text: 'Hello world'
}

mailgunClient.messages().send(emailData, (err, response) => {
  if (err) {
    console.error('Send failed:', err)
    return
  }
  console.log('Email sent:', response)
})

My DNS configuration looks correct to me:

MX Records:

I’ve also added the required TXT records for verification.

No errors appear in my application logs. Any suggestions on what might be causing this same-domain delivery issue?

You’ve encountered a common MX record conflict with Mailgun setups. When the sender and recipient domains are the same, the mail server struggles to determine whether to process the email locally or through Mailgun. I faced a similar challenge last year with our domain’s transactional emails. A solid solution is to use a subdomain, such as mail.example.org, for your Mailgun configuration, which eliminates the delivery confusion. Alternatively, if you prefer to maintain your existing setup, ensure your Mailgun dashboard has the correct routing rules for inbound emails directed to domain addresses.

check your mailgun logs for bounces or rejects - the dashboard might show ‘accepted’ but you’re probably getting delivery failures downstream. also, make sure your domain’s fully verified in mailgun. partial verification can cause weird issues with same-domain routing.