如何在 mailgun(为子域配置)中从根域发送和接收电子邮件

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

Mailgun 建议为子域创建 DNS(TXT、MX、..)记录,但指出通过以后的配置可以使用根域发送和接收。我在我的注册商和 Mailgun 为 mail.example.com 创建了所有必要的步骤(添加域、设置路由等)。 我现在可以接收和发送电子邮件到配置的 example@mail.example.com.

我现在必须更改什么才能在 example@example.com 发送和接收? 注册商、mailgun 和我在 gmail 的 smtp 设置(通过 mailgun 从 gmail 发送)有哪些必要的更改?

非常感谢!

我使用 Mailgun 的时间很短,但我可以用我目前学到的知识来提供帮助。

可以为 Mailgun 或 Gmail 等第三方设置您的 DNS 记录。我认为他们不会同时使用两者。我不确定这会对路由产生什么影响,因为它不知道去哪里。

对于您的 Mailgun 子域,您使用了 mail.example.com 和电子邮件地址 example1@mail.example.com。我的是 运行,但我根本没有创建那样的电子邮件地址。我的电子邮件格式仍然是 example1@example.com.

我将从收到的电子邮件中粘贴此内容,并对其进行编辑以匹配您提供的示例:

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)

如果您为子域配置 Mailgun,则可以通过适当的 to 变量从主域发送电子邮件。例如,使用 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);

或者您可以阅读其他通过 API in their docs 发送的方法。无论如何,即使您的 Mailgun 配置为子域,您也可以从主域发送电子邮件。

但是 (!) 您的 MX 记录是为您的子域配置的,因此您只能在那里接收电子邮件。为了能够接收到您的主域的电子邮件,请在 Mailgun 的控制面板中添加您的主域,例如不是 mail.example.com,而是 example.com,并在您的 DNS 控制面板中为此主域进行相应的配置,DigitalOcean 的 DNS example.com(不是子域)的示例配置:

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.

请记住,Mailgun 没有邮箱功能,如果您有适当的规则集,它只能转发收到的电子邮件。大多数人将他们主域的 MX 记录委托给更易于管理的 ESP,例如 Gmail。 一个域只能有一组 MX 记录,因此您必须选择 Gmail 或 Mailgun。

为此您需要使用 mailgun-js

  1. 需要来自 npm 的 mailgun-js

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

2.Set mailgun 选项。即 apiKey 和域。

var options = {
  apiKey: 'YOUR_API_KEY',
  domain: 'YOUR_DOMAIN'
};
  1. 使用这些选项实例化 mailgun。

    var mailgun = new Mailgun(options);

  2. 设置必要参数后发送邮件

    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() {
    
    });
    

如果您尝试使用 Django 的 Anymail 包从子域发送 Mailgun 电子邮件,您需要使用 EmailMultiAlternatives 对象发送电子邮件并指定电子邮件发件人域,如下所示:

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()