Heroku上的NodeMailer请求超时(Mandrill,SMTP),适用于localhost

时间:2015-08-24 15:25:44

标签: node.js heroku express smtp nodemailer

我在express / angular应用程序中使用nodemailer,并使用nodemailer使用基本邮件程序尝试了一些操作。 我尝试使用Hotmail作为服务,它在localhost上工作但在heroku上工作 它会不断返回503Invalid Credentials,即使它有效。

其次我试图用我的Mandrill帐户连接它,代码看起来像这样:

Angular(ES6):

sendEmail(form) {

var data = {
  contactName: form.name,
  contactEmail: form.email,
  comments: form.comments || null
};

var req = {
 method: 'POST',
 // url: 'http://localhost:9030/email', <- my express port
 url: location.origin + '/email',
 // url: 'https://myherokuapp.herokuapp.com/email', <- my heroku app
 headers: {
   'Content-Type': 'application/json'
 },
 data: data
};

this.$http(req)
.then((result) => {
  console.log('success is...', result);
  return result;
})
.catch((error) => {
  console.log('error is...', error);
  return error;
});

}

快递:

    var transport = nodemailer.createTransport("SMTP", {
    service: 'Mandrill', // use well known service.
    // If you are using @gmail.com address, then you don't
    // even have to define the service name
    auth: {
        user: "xxxxx@gmail.com",
        pass: "xxxxxxxxxxxxxxxx"
    }
});

console.log('SMTP Configured');

var message = {

    // sender info
    from: 'Sender: <' + req.body.contactEmail + '>',

    // Comma separated list of recipients
    to: '"Info:" <info@example.com>',

    // Subject of the message
    subject: 'Nodemailer is unicode friendly ✔', //

    headers: {
        'X-Laziness-level': 1000
    },

    // plaintext body
    text: "Interest\n" +
        "===========\n" +
        "\n" +
        "**How** are you?"
};

console.log('Sending Mail');
transport.sendMail(message, function(error) {
    if (error) {
        console.log('Error occured');
        console.log(error.message);
        return;
    }
    console.log('Message sent successfully!');

    // if you don't want to use this transport object anymore, uncomment following line
    transport.close(); // close the connection pool
});

我真的被卡住了,感觉就像一个简单的任务,适用于localhost但不适用于heroku。使用Mandrill SMTP,当我在表单中发送内容时,我会在heroku上获取这些日志:

2015-08-24T15:12:58.800676+00:00 app[web.1]: POST /email - - ms - -
2015-08-24T15:13:15.858302+00:00 app[web.1]: { contactName: 'testing',
2015-08-24T15:13:15.858307+00:00 app[web.1]:   contactEmail: 'asdf@adsf.com',
2015-08-24T15:13:15.858309+00:00 app[web.1]:   comments: 'asdfasdf' }
2015-08-24T15:13:15.858316+00:00 app[web.1]: Email params (name):testing
2015-08-24T15:13:15.858376+00:00 app[web.1]: Email params (email):asdf@adsf.com
2015-08-24T15:13:15.858417+00:00 app[web.1]: Email params (comments):asdfasdf
2015-08-24T15:13:15.858498+00:00 app[web.1]: SMTP Configured
2015-08-24T15:13:15.858537+00:00 app[web.1]: Sending Mail
2015-08-24T15:13:15.889774+00:00 app[web.1]: Message sent successfully!
2015-08-24T15:13:45.845230+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/email" host=secret-river-5414.herokuapp.com request_id=5c1e8378-6971-4253-86e2-a9647fc7e0d4 fwd="14.200.153.28" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0
2015-08-24T15:13:45.857582+00:00 app[web.1]: POST /email - - ms - -

如果有人能说清楚,谢谢!

1 个答案:

答案 0 :(得分:-1)

好的,所以如果有其他人碰到这个, 我发现这真的只是我正在使用的免费托管问题。 Heroku超时了我的请求,因为完成操作需要很长时间,这会关闭邮件程序的连接池。我注意到电子邮件最终会在很晚的时候通过。

通过简单升级主机方案/缩放动力和增加工人

来解决这个问题