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

时间:2016-03-07 16:47:43

标签: node.js mailgun

我的堆栈:带有Express 4.x的node.js mailgun模块:mailgun-js

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

typedef struct  philo_s
{
  pthread_t     handler; // The thread of the philosopher
  char          state; // Either 'E' for eat, 'T' for think or 'R' for rest
  int           dur_think; // Defines the maximum duration of the think state
  int           dur_eat; // Defines the maximum furation of the eat state
  int           max_eat;
  int           num;
}               philo_t;

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

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 } 而没有http时,我收到此错误:

"mg.xxx.com"

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

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

4 个答案:

答案 0 :(得分:2)

您的变量'http://mg.xxx.com/'是mailgun基本API网址。要发送消息,您需要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!'

答案 1 :(得分:2)

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

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

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

中提及此问题

答案 2 :(得分:1)

这是我在变量声明中输入的拼写错误:

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

正确的:

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

答案 3 :(得分:1)

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

该区域的端点为:https://api.eu.mailgun.net

此更改后,所有功能都开始工作。