gpt4 book ai didi

node.js - Mandrill API 在按计划作业运行时不发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:52 25 4
gpt4 key购买 nike

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

现在我只是测试我将使用的基本框架,所以我有 Node Heroku Scheduler尝试使用 Mandrill API 每 10 分钟发送一封测试电子邮件。

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

Controller :

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 channel 。建议我使用 Sails.lift() 来让我的预定作业能够在预期的环境中运行,以便使用我所有的常规 Controller 、模型和服务。

已更新 - 我将所有代码放在同一个文件中以帮助排除故障

该代码如下所示:

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() 在发送电子邮件之前终止了计划任务。试着把它拿出来。如有必要,您可以在回调中调用 process.exit(),从中获取 API 调用的结果。

关于node.js - Mandrill API 在按计划作业运行时不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32641871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com