21

Mailgun recommends creating DNS (TXT, MX,..) records for a subdomain, but states sending and receiving with the root domain is possible by later configuration. I created all necessary steps for mail.example.com at my registrar and at Mailgun (adding a domain, setting up routes, etc.). I can now receive and send emails to the configured example@mail.example.com.

What do I have to change now to be able to send and receive at example@example.com? What are the necessary changes at the registrar, in mailgun, and in my smtp settings at gmail (for sending from gmail via mailgun)?

Thank you very much!

solimanelefant
  • 546
  • 1
  • 5
  • 17

4 Answers4

20

If you configure Mailgun for a subdomain, you can send emails from your main domain passing a proper to variable. For instance, using Node.js + nodemailer + nodemailer-mailgun-transport:

var nodemailer = require('nodemailer'),
    mg = require('nodemailer-mailgun-transport'),
    auth = { api_key: 'foobar', domain: 'mail.example.com' },
    nodemailerMailgun = nodemailer.createTransport(mg({ auth: auth }));

nodemailerMailgun.sendMail({
    from: 'helloworld@example.com',
    to: 'recipient@domain.com',
    subject: 'Hey you, awesome!',
    text: 'Mailgun rocks, pow pow!'
}, someCallback);

Or you can read about other methods of sending through an API in their docs. Anyway, even if your Mailgun is configured for a subdomain, you can send email from your main domain.

However (!) your MX records are configured for your subdomain, and hence you can only receive emails there. To be able to receive email to your main domain, add your main domain in Mailgun's control panel, e.g. not mail.example.com, but example.com, and make an according configuration in your DNS control panel for this main domain, example configuration for DigitalOcean's DNS for example.com (not subdomain):

TXT    @                v=spf1 include:mailgun.org ~all
TXT    krs._domainkey   k=rsa; p=MIGfM...blablabla
CNAME  email            mailgun.org.
MX     10               mxa.mailgun.org.
MX     10               mxb.mailgun.org.

Keep in mind, that Mailgun does not have mailbox functionality, it can only forward incoming emails, if you have an appropriate rule set. Most people delegate their main domain's MX records to a more manageable ESP, like Gmail. You can only have one set of MX records for a domain, so you have to choose, either Gmail, or Mailgun.

Anton Egorov
  • 1,328
  • 1
  • 16
  • 33
  • Thank you Anton. I'm setting this up for the first time, so things are a little tricky. Do I understand correctly, that I can not use the smtp protocol if I want to send from the main domain? Only nodemailer or their API? – solimanelefant Apr 26 '16 at 15:33
  • 1
    I can have MX records on domain.com for gmail, to receive emails `@domain.com` via gmail, and then separate MX records on mg.domain.com to receive `@mg.domain.com` emails via mailgun, can't I? Or can you only have one set of MX records for an entire domain, and subdomains don't count? – Codemonkey Aug 01 '17 at 12:34
  • Just pointing out that this doesn't seem to work for SMTP. MailGun refuses to create SMTP credentials for `yourdomain.com` if you set the DNS to `mg.yourdomain.com` as they recommend. So if you need SMTP, add the DNS records to the root domain. – Eric Eskildsen Nov 10 '22 at 21:49
4

You need to use mailgun-js for this

  1. Require mailgun-js from npm

    var Mailgun = require('mailgun-js');

2.Set options for mailgun. i.e. apiKey and domain.

var options = {
  apiKey: 'YOUR_API_KEY',
  domain: 'YOUR_DOMAIN'
};
  1. Instantiate mailgun with these options.

    var mailgun = new Mailgun(options);

  2. Send email after setting required parameter for it.

    var data = {
        //From email
        from: '',
        // Email to contact
        to: 'To Email address',
        // CC email
        ccTo: 'CC address if any'
        // Subject
        subject: 'Mail subject',
        // Email msg
        html: 'email message or html'
    };
    
    // Send email
    mailGun.messages().send(data, callbackFunction() {
    
    });
    
Sandip Nirmal
  • 2,283
  • 21
  • 24
  • You actually don't *need* to use any module for this (although you may if you want). Sending messages is done via an API call to Mailgun's endpoints, you can do even with `curl` or `ajax`, e.g. with jQuery: `$.ajax({ type: 'POST', url: 'https://api.mailgun.net/v3/example.com/messages', username: 'api', password: 'api_key', data: { from: 'hello@example.com', to: 'user@domain.com' ... }, success: callback });` (as of jQuery 1.7.2+), although you should probably consider sending messages from your server. – Anton Egorov Apr 26 '16 at 10:41
2

I've only been using Mailgun a short time, but I can help with what I've learned so far.

Your DNS records can be setup for Mailgun or a third party like Gmail. I don't think they will use both. I'm not sure what that would do to the routing, because it would not know where to go.

For your Mailgun subdomain, you used mail.example.com with email address example1@mail.example.com. Mine is running, but I did not create email addresses like that at all. My email formats are still example1@example.com.

I am going to paste this in from an email I received, and edit it to match your provided example:

It looks like you have set the MX records for the root domain, example.com, however the domain you are using with Mailgun is mail.example.com. You will need to change the hostname from example.com to mail.example.com for these to route correctly.

As Mailgun does not have mailboxes, receiving email with Mailgun requires using a subdomain with MX records pointing to Mailgun as well as using our Routes functionality. A good way to understand Routes is as a sophisticated filtering and forwarding mechanism. With Routes, you can either:

  • forward the incoming email to another environment for storage (such as an email address or an endpoint on your server
  • store a message temporarily (for up to 3 days) and retrieve it using the Messages API
  • stop a message from being processed (i.e. dropping certain messages instead of forwarding or storing them)
Community
  • 1
  • 1
  • Adding a comment: I got a reply from the Mailgun guy Chris. He said I should leave the existing MX record provided by the service provider and add in an extra pair of MX records for the `mg.example.com` domain: One for the **MXA** record and one for the **MXB** record. Mailgun's support staff is really good! –  Apr 27 '16 at 18:44
0

If you're trying to use Django's Anymail package to send Mailgun email from a subdomain, 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@example.com", ["to@somedomain.com"])
msg.esp_extra = {"sender_domain": "mg.example.com"}

msg.send()
Rob
  • 1,656
  • 2
  • 17
  • 33