用mailgun通过node.js发送邮件 – 找不到请求的URL

我的堆栈:使用Express 4.x mailgun模块的node.js: 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 } 

我不知道从哪里开始debugging – 也许它本地不工作?

已经修复 – 问题是我写了md而不是mg

您的variables'http://mg.xxx.com/'是mailgun基础APIurl。 要发送消息,您需要POST以解决'http://mg.xxx.com/messages' 。 Mailgun有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 = 'Sandbox<**somebignumberhere**>.Mailgun.Org;

这是我在variables声明中犯的一个错字:

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

正确:

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