好的,为此您需要一个 cron 表达式,因此请点击此链接https://crontab.guru/#*/60_07,19_14-15_*_1,2,4,您将获得完成任务所需的表达式,您甚至可以根据您的需要生成多个不同的 cron 表达式,并将邮件发送给 n 个用户,您还需要用户电子邮件向他们发送电子邮件,遵循以下伪代码。
let cron = require('node-cron');
let nodemailer = require('nodemailer');
cron.schedule('*/60 07,19 14-15 * 1,2,4', () => {
sendEmailFunction();
});
sendEmailFunction(){
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '<FROM_EMAIL_ADDRESS>',
pass: '<FROM_EMAIL_PASSWORD>'
}
});
let mailOptions = {
from: '<FROM_EMAIL_ADDRESS>',
subject: 'A Test Message!',
text: 'Content to send'
};
//get all user email id or desired user email id from user collection according to your need
User.find({}, { email: 1 }).then((emailIds) => {
emailIds.foreach(emailId => {
mailOptions.to = emailId;
//send an email
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
})
}).catch((err) => {
console.log("error")
})
}