mailgun 的域名示例是 nodejs 的吗?

Example of the domain name for mailgun be for nodejs?

嘿,我正在使用 mailgun 尝试发送电子邮件,所以我正在使用这个脚本:

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

//get requests
expressApp
    .get("/", function routeHandler(req, res) {
        res.sendFile(path.join(__dirname, "../client/index.html"));

        var api_key = 'key-00000000000000000000';
        var domain = "https://api.mailgun.net/v3/mydomain.com"; //I think the error must be here
        var mailgun = new Mailgun({apiKey: api_key, domain: domain});

        var data = {
            from: "me@mydomain.com", //I tried also with me@samples.mailgun.org which was in the example

            to: 'myemail@gmail.com',
            subject: 'Hello',
            text: 'Testing some Mailgun awesomness!'
        };

        mailgun.messages().send(data, function (err, body) {
            if (err) {
                console.log("error ", err);
            }
            console.log(body);
        });
    })

我想我的域名有误,但我完全按照在他们网站的 mailgun 控制台面板中显示的那样粘贴它:

任何人都可以给我粘贴域名的示例吗?

这是我遇到的错误:

error  { [Error: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.</p><p>If you entered the URL manually please check your spelling and try again.</p>
] statusCode: 404 }
undefined

我的工作示例将 Mailgun 域设置为 "sandbox-blahblahblah.mailgun.org"

查看此文档:https://www.npmjs.com/package/mailgun-js - 我认为您不应该将域设置为它们的 API 根目录,而是您设置为通过 Mailgun 发送的域.

使用代码示例进行编辑:

我从主配置文件 (main.js) 导出以下内容:

  // Configuring Mailgun API for sending transactional email
  'mailgun_priv_key': 'key-XXXXXXXXXXXXXX',
  // Configuring Mailgun domain for sending transactional email
  'mailgun_domain': 'sandboxXXXXXXXXXXXXXX.mailgun.org'

在我的 Mailgun 配置文件 (mailgun.js) 中,我有以下内容:

const config = require('./main');
const mailgun = require('mailgun-js')({ apiKey: config.mailgun_priv_key,
domain: config.mailgun_domain });

// Create and export function to send emails through Mailgun API
exports.sendEmail = function(recipient, message) {
    const data = {
      from: 'My Name <email@mydomain.com>',
      to: recipient,
      subject: message.subject,
      text: message.text
    };

    mailgun.messages().send(data, function(error, body) {
      console.log(body);
    });
  }

然后我可以从我的控制器导入我的 Mailgun 配置:

const mailgun = require('../config/mailgun');

我可以发邮件:

const message = {
            subject: 'Subject here',
            text: 'Some text'
          }

          // Otherwise, send user email via Mailgun
          mailgun.sendEmail(user.email, message);

这是一个包含一些我需要修复的东西的 repo,但是 Mailgun 集成有效:https://github.com/joshuaslate/mern-starter/tree/master/server/config

这对我有用:

var api_key = 'key-################';
var domain = 'mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

var data = {
    from: 'Excited User <me@mydomain.com>',
    to: 'recepeint@gmail.com',
    subject: 'Hello',
    text: 'Testing some Mailgun awesomness!'
};
mailgun.messages().send(data, function (error, body) {
    console.log(body);
});

截至 2020 年附近,我 post 您将 mailgun api 用于 EU mailgun 服务器的工作示例。您需要为 mailgun-js 构造函数指定主机。我这里也是用express

let mailgun = require("mailgun-js")({
    apiKey: "key-xxxxxxxxxxx", 
    domain: "YOUR-DOMAIN.com",     // or MG.YOUR-DOMAIN.com NOT https://api.eu.mailgun.net/v3/YOUR-DOMAIN.com
    host: "api.eu.mailgun.net"
});

router.get("/sendMail", (req, res) => { 
    const data = {
        from: "no-reply@YOUR-DOMAIN.com",
        to: "bob@example.com",
        subject: "Hello",
        html: "<b>Testing some Mailgun awesomeness!</b>"
    };

    mailgun.messages().send(data, (error, body) => {
        if(!error) res.send("Hurray! Email sent.");
    });
});