通过 node.js 使用 mailgun 发送邮件 - 请求 URL 未找到

Sending mail with mailgun through node.js - requested URL not found

我的堆栈:node.js 与 Express 4.x 邮件枪模块:mailgun-js

我目前正在本地机器上开发并尝试发送一些简单的确认电子邮件,如本模块示例所示:

var api_key = 'key-xxx';
var domain = 'http://mg.xxx.com/';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

var data = {
      from: 'Billing <no-reply@mydomain.com>',
      to: data.email, 
      subject: 'Thanks for buying ' + data.product,
      text: 'You can create your plan right now or visist http://www.someurl.com later.'
    };

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

根据这个例子,这应该可行,不幸的是我不断收到以下错误:

{ [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 }

当我将域更改为:"mg.xxx.com" 而没有 http 时,我收到此错误:

{ [Error: Domain not found: md.xxx.com] statusCode: 404 }

我不知道从哪里开始调试 - 也许它在本地不起作用?

已经修复 - 问题是我写的是 md 而不是 mg

您的变量 'http://mg.xxx.com/' 是 mailgun 基地 API URL。要发送消息,您需要 POST 至地址 'http://mg.xxx.com/messages'。 Mailgun 有 documentation for curl 可以用来练习:

curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
-F to=YOU@YOUR_DOMAIN_NAME \
-F to=bar@example.com \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!'

当您注册时,您将获得一个如下所示的域

Sandbox<**somebignumberhere**>.Mailgun.Org

var domain = 'Sandbox<**somebignumberhere**>.Mailgun.Org;

中提到这个

这是我在变量声明中的错别字:

var domain = 'http://mg.xxx.com/';

正确:

var domain = 'http://md.xxx.com/';

我遇到了类似的问题,但我的解决方案是为 mailgun api 定义另一个端点,因为我的域是在欧盟地区创建的。

该区域的终点是:https://api.eu.mailgun.net

此更改后,一切开始工作。