Mandrill API在按计划作业运行时不会发送电子邮件

我有一个预定的工作,我需要每天发送电子邮件给客户。

现在我正在testing我将要使用的基本框架,所以我使用Mandrill API每隔10分钟让Node Heroku Scheduler尝试发送testing电子邮件。

在正常的操作情况下,我可以使用以下后端代码成功发送testing邮件:

控制器:

sendTestEmail: function (req, res) { console.log("Trying to send the hourly email."); var emailInfo = { message: "Sample Message - should send hourly.", subject: "Hourly email test", fromEmail: "collegepregame@gmail.com", fromName: "SmallChangeProject", to: "zach_cook@texasca.com", name: "Zachary Cook" }; EmailService.simpleSendEmail(emailInfo, function(err, message) { if (err) { console.log("Email did not send.", err); res.send(404, '\n\nerror occurred\n\n'); } console.log("Message sent: ", message); return res.send(message); }); } 

EmailService代码如下所示:

服务:

 var mandrill = require('mandrill-api/mandrill'); var mandrill_client = new mandrill.Mandrill('dDIJDHIDDEN2893KEYUNDSECRET'); module.exports = { simpleSendEmail: function (emailInfo, cb) { var message = { "html": emailInfo.message || "Accidental email, please ignore", "text": emailInfo.text || "Example text content", "subject": emailInfo.subject || "Password Reset Email", "from_email": emailInfo.fromEmail || "customerservice@smallchangeproj.com", "from_name": emailInfo.fromName || "SmallChangeProject", "to": [{ "email": emailInfo.to || "zacharycook2014@utexas.edu", "name": emailInfo.name || 'Name Unavailable' }], "headers": { "Reply-To": emailInfo.fromEmail || "customerservice@smallchangeproj.com" }, "important": false, "track_opens": null, "track_clicks": null, "auto_text": null, "auto_html": null, "inline_css": null, "url_strip_qs": null, "preserve_recipients": null, "view_content_link": null, "tracking_domain": null, "signing_domain": null, "return_path_domain": null, "merge": true, "merge_language": "mailchimp" }; var async = false; var ip_pool = "Main Pool"; // console.log('\n-------\nprenteding to send the emails!\n-------\n'); mandrill_client.messages.send({"message": message, "async": async, "ip_pool": ip_pool}, function(result) { console.log(result); }, function(e) { // Mandrill returns the error as an object with name and message keys console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message); return cb('an error occurred'); // A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123' }); return cb(null, 'email has been sent'); } }; 

好,那么所有的工作。 问题是当我尝试使用预定作业发送它。

我从这个StackOverflow文章和SailsJS的Gitter频道借用了一个概念,在那里我寻求帮助。 我build议使用Sails.lift()来让我的预定作业能够在环境中操作,以便使用我所有的常规控制器,模型和服务。

更新 – 我把所有的代码在相同的文件,以帮助TROUBLESHOOT

该代码看起来像这样:

 var Sails = require('sails'); var mandrill = require('mandrill-api/mandrill'); var mandrill_client = new mandrill.Mandrill('W9IVcGVz1E1oTmvG_KdiXQ'); Sails.lift(function(err, sails) { console.log("Trying to send the hourly email."); if (mandrill) { console.log("Have mandrill itself."); } if (mandrill_client) { console.log("Have mandrill client initiated."); } var emailInfo = { message: "Sample Message - should send hourly.", subject: "Hourly email test", fromEmail: "collegepregame@gmail.com", fromName: "SmallChangeProject", to: "zach_cook@texasca.com", name: "Zachary Cook" }; var message = { "html": emailInfo.message || "Accidental email, please ignore", "text": emailInfo.text || "Example text content", "subject": emailInfo.subject || "Password Reset Email", "from_email": emailInfo.fromEmail || "customerservice@smallchangeproj.com", "from_name": emailInfo.fromName || "SmallChangeProject", "to": [{ "email": emailInfo.to || "zacharycook2014@utexas.edu", "name": emailInfo.name || 'Name Unavailable' }], "headers": { "Reply-To": emailInfo.fromEmail || "customerservice@smallchangeproj.com" }, "important": false, "track_opens": null, "track_clicks": null, "auto_text": null, "auto_html": null, "inline_css": null, "url_strip_qs": null, "preserve_recipients": null, "view_content_link": null, "tracking_domain": null, "signing_domain": null, "return_path_domain": null, "merge": true, "merge_language": "mailchimp" }; var async = false; var ip_pool = "Main Pool"; mandrill_client.messages.send({"message": message, "async": async, "ip_pool": ip_pool}, function(result) { console.log(result); console.log("Should have sent!"); }, function(e) { console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message); }); console.log("Email should have sent."); process.exit(); }); 

在给我的控制台上:

  Starting process with command `node scheduled-job.js` scheduler@addons.heroku.com heroku[scheduler.1963]: Starting process with command `node scheduled-job.js` heroku[scheduler.1963]: State changed from starting to up app[scheduler.1963]: Trying to send the hourly email. app[scheduler.1963]: Have mandrill itself. app[scheduler.1963]: Have mandrill client initiated. app[scheduler.1963]: Email should have sent. heroku[scheduler.1963]: State changed from up to complete heroku[scheduler.1963]: Process exited with status 0 

成功消息使得它看起来像一切正常, EmailService.simpleSendEmail()不给我任何错误 – 没有发送电子邮件。

它没有发送的原因是什么,但Mandrill没有给我提供错误信息? 请问sails.lift()不适用于发送预定的电子邮件吗?

感谢任何/所有的帮助!

最好 – 扎克

您的process.exit()会在发送电子邮件之前杀死计划的任务。 试着把它拿出来。 如有必要,您可以在callback中调用process.exit() ,从API调用中获得结果。