Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive emails in mailgun (configured for a subdomain) from the root domain

Tags:

dns

mailgun

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 [email protected].

What do I have to change now to be able to send and receive at [email protected]? 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!

like image 357
solimanelefant Avatar asked Apr 16 '16 17:04

solimanelefant


People also ask

Can you send emails from a subdomain?

An email subdomain delivers email from the subdomain instead of the root domain. Here's an example: [email protected]. Learn.mailgun.com is the subdomain that Mailgun uses to send marketing messages. Every user that signs up for a Mailgun account gets a welcome email from Sam, sent from that address.

How do I setup a subdomain for email?

To setup, simply navigate to Web Host Manager -> Create A New Account. Rather than using a domain, enter the subdomain (i.e. billing.yourdomain.com) and use the password generator to create a strong one. Once added, login to cPanel -> Email Accounts and create the address needed.

Should I use a subdomain for email?

Email subdomains are the best way to ensure your emails land in your subscribers' inboxes. A good step to start seeing a difference in deliverability is separating out your marketing and transactional emails on separate email subdomains.

Do you need a domain for Mailgun?

A verified domain is required to send any emails at Mailgun, and the verification process is critical to protecting our infrastructure and your domain's sending reputation.


3 Answers

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: '[email protected]',
    to: '[email protected]',
    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.

like image 151
Anton Egorov Avatar answered Sep 30 '22 21:09

Anton Egorov


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() {
    
    });
    
like image 41
Sandip Nirmal Avatar answered Sep 30 '22 21:09

Sandip Nirmal


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 [email protected]. Mine is running, but I did not create email addresses like that at all. My email formats are still [email protected].

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)
like image 2
jp2code Avatar answered Sep 30 '22 20:09

jp2code