node.js - 如果网站未运行,nodemailer 不会发送电子邮件
问题描述
我在我的 MERN 网站上使用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();
});
服务器.js:
...
const { sendEmail } = require("./...sendEmail path");
...
app.use(sendEmail);
...
我无法弄清楚问题出在哪里以及如何解决它,所以如果有人可以提供帮助,我将不胜感激!
解决方案
我修复了node-cron作业调度包的问题。
server.js:
...
const cron = require("node-cron");
const { sendEmail } = require("./...sendEmail path");
...
//app.use(sendEmail); //no need anymore
cron.schedule("*/5 * * * *", () => {
sendEmail();
console.log("running a task every five minutes");
});
...
但我必须next()
从我的sendEmail中间件中删除
如果你想了解更多关于 node-cron点击这里
推荐阅读
- android - 为什么使用 \u1F512 格式的 unicode 在 strings.xml 中不起作用?
- jsp - 使用 JSTL 时未执行 toString() 方法
- r - R——在没有循环的情况下移动数据帧中的多行
- javascript - 如何为html表上同一行具有不同值的行赋予红色?
- windows - 使用 LuaMacros 选择另一种语言时无法发送英语输入
- ios - 如何从 UISegmentedControl 获取字符串字体
- loops - 遍历作为参数传递的文件夹
- javascript - 如何添加更多要替换的文本 java 脚本?
- javascript - 如何使用 Google Apps 脚本中的 google.script.run 将 JS 对象传递给服务器函数?
- azure - Azure Databricks 上的最大消息大小