我有一份预定的工作,我需要每天向客户发送电子邮件。
现在我只是测试我将使用的基本框架,因此我Node Heroku Scheduler尝试使用Mandrill API每10分钟发送一封测试电子邮件。
在正常操作情况下,我可以使用以下后端代码成功发送测试邮件:
控制器:
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');
}
};
好的,所以这一切都有效。问题是当我尝试使用预定作业发送它时。
我从this StackOverflow post和SailsJS的Gitter频道借用了一个概念,我在那里寻求帮助。我被建议使用Sails.lift()
让我的预定工作能够在环境中运行,以便使用我所有的常规控制器,模型和服务。
更新 - 我将所有代码放入同一文件以帮助解决问题
该代码如下:
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()
无法发送预定的电子邮件吗?
感谢您的帮助!
最佳 - 扎克
答案 0 :(得分:2)
您的process.exit()
在发送电子邮件之前杀死了预定的任务。试着把它拿出来。如有必要,您可以在回调中调用process.exit()
,从API调用中获取结果。