gpt4 book ai didi

javascript - 如何让 Email.send 在未来(7 天、14 天后等)发送邮件

转载 作者:行者123 更新时间:2023-11-29 19:31:15 25 4
gpt4 key购买 nike

我以后需要向人们发送电子邮件提醒。我已经实现了 MomentJS,我可以获得我需要发送电子邮件的日期,我只是在 meteor 文档中找不到 Email.send(options) 的任何内容。您可以推荐其他套餐吗?

根据@richsilv 的建议,这是我的一些代码

createTransfer.js(客户端)

Template.createTransfer.events({
'submit form': function (event, template) {
event.preventDefault();

Transfers.insert(transfer, function (error, id) {
if (error) {
showError(error);
} else {
Session.set('files');
showAlert('Transfers sent!');
transfer._id = id;
Meteor.call('sendEmail', transfer);
scheduleEmail(transfer); // send reminder email
Router.go('viewTransfer', {_id: id});
}
});
// scheduleEmail(transfer);


console.log(transfer);

function scheduleEmail(transfer) {

if (transfer.invoice.due == "7 Days"){
console.log('its due in 7 days');
if (moment(transfer.date).add(7, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
Meteor.call('sendMail',transfer);
}
} else if (transfer.invoice.due == "14 Days") {
if (moment(transfer.date).add(14, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
Meteor.call('sendMail',transfer);
}
} else if (transfer.invoice.due == "30 Days") {
if (moment(transfer.date).add(30, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
Meteor.call('sendMail',transfer);
}
} else if (transfer.invoice.due == "90 Days") {
if (moment(transfer.date).add(90, 'days').calendar() == moment(new Date()).format("MM/DD/YYYY")) {
Meteor.call('sendMail',transfer);
}
} else {
console.log('need to initiate cron!');
var thisId = FutureEmails.insert(transfer);
console.log('inserted into db');
Meteor.call('addCronMail',thisId, transfer);
}

}

方法.js(库)

Meteor.methods({

sendMail: function(transfer) {

check([transfer.senderEmail,
transfer.recipientEmail,
transfer.message,
// transfer.invoice.total
], [String]);

// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();

transfer.url = Meteor.absoluteUrl() +'transfer/' + transfer._id;

var template = 'invoice-due';

Email.send({
to: transfer.recipientEmail,
bcc: transfer.senderEmail,
from: transfer.senderEmail,
subject: transfer.senderEmail + ' sent you files!',
html: Handlebars.templates[template](transfer)
});

},

addCronMail: function(id, transfer) {

SyncedCron.add({
name: id,
schedule: function(parser) {
return parser.recur().on(transfer.date).fullDate();
},
job: function() {
sendMail(transfer);
FutureEmails.remove(id);
SyncedCron.remove(id);
return id;
}
});

}
});

cron.js(服务器)

Meteor.startup(function() {

FutureEmails.find().forEach(function(mail) {
if (moment(mail.date).format("MMM Do YY") == moment(new Date()).format("MMM Do YY")) {
sendMail(mail)
} else {
addCronMail(mail._id, mail);
}
});
SyncedCron.start();

});

最佳答案

根据要求,尽管请注意这 测试过,因此您可能不得不尝试一下。

$ meteor add percolatestudio:synced-cron

然后在服务器上是这样的:

FutureEmails = new Meteor.Collection('future_emails'); // server-side only

// "details" should be an object containing a date, plus required e-mail details (recipient, content, etc.)

function sendMail(details) {

Email.send({
from: details.from,
to: details.to,
etc....
});

}

function addCronMail(id, details) {

SyncedCron.add({
name: id,
schedule: function(parser) {
return parser.recur().on(details.date).fullDate();
},
job: function() {
sendMail(details);
FutureEmails.remove(id);
SyncedCron.remove(id);
return id;
}
});

}

function scheduleEmail(details) {

if (details.date < new Date()) {
sendMail(details);
} else {
var thisId = FutureEmails.insert(details);
addCronMail(thisId, details);
}

}

Meteor.startup(function() {

FutureEmails.find().forEach(function(mail) {
if (mail.date < new Date()) {
sendMail(mail)
} else {
addCronMail(mail._id, mail);
}
});
SyncedCron.start();

});

然后只要您想安排一封新邮件,只需调用 scheduleEmail(details)

希望对您有所帮助!

关于javascript - 如何让 Email.send 在未来(7 天、14 天后等)发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27430689/

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