【发布时间】:2021-12-02 05:18:10
【问题描述】:
我在 MERN 网站上使用 nodemailer 根据日期发送提醒电子邮件。当网站在浏览器中运行时它工作正常,否则即使日期匹配也不会发送电子邮件!
nodemailer 配置:
const asyncHandler = require("express-async-handler");
const nodemailer = require("nodemailer");
module.exports.sendEmail = asyncHandler(async (req, res, next) => {
const transporter = nodemailer.createTransport({
service: //service,
host: //host,
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: //email,
pass: //pass,
},
});
transporter.verify((err, success) => {
if (err) console.error(err);
console.log("Your config is correct: ");
});
// mapping through the database to grab the needed data based on the date
const mailOptions = {
from: //email,
to: //email2,
subject: "You should recied an email",
text: `This is a test email!`,
};
transporter.sendMail(mailOptions, async (error, info) => {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
transporter.close();
});
//end of mapping
next();
});
server.js:
...
const { sendEmail } = require("./...sendEmail path");
...
app.use(sendEmail);
...
我无法弄清楚问题出在哪里以及如何解决它,所以如果有人能提供帮助,我将不胜感激!
【问题讨论】:
-
您需要使用其中一个作业调度第三方包。您可以使用以下流行库之一Node Schedule、Node Cron、Agenda 等。
-
@ArifKhan 谢谢!!我使用 Node Corn 并且它有效
标签: node.js reactjs mongodb express nodemailer