我正在尝试使用nodemailer发送电子邮件,但是它没有发送任何电子邮件,而且奇怪的是它也没有记录任何错误。
这是我的路线代码:
router.post("/form", async (req, res) => {
try {
let { name, email, password } = req.body;
let user = await User.create({
name,
email,
password,
});
if (!user) return res.status(501).send("Something went wrong");
let token = await user.getSignedToken();
try {
user.emailToken = await user.verifyEmail();
} catch (ex) {
console.log(ex);
}
try {
await user.save({ validateBeforeSave: false });
} catch (ex) {
console.log(ex);
}
const options = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRE * 24 * 60 * 60 * 1000
),
};
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "myEmail",
pass: "myPass",
},
});
var mailOptions = {
from: "myEmail",
to: req.body.email,
subject: "Verify Email Token",
text: `go to the below link\nhttp://localhost:3000/verifyEmail/${emailToken}`,
};
smtpTransport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
res.cookie("token", token, options);
res.send("Check email for activation");
} catch (ex) {
res.send(ex);
}
});
因此,它没有发送任何电子邮件,之后,res.cookie和res.send行也无法正常工作。但是,服务器未记录任何错误/异常。那么,可能是什么问题呢?我在另一条路由中使用了nodemailer(具有与此帐户相同的gmail帐户),并且该路由正在运行。那么,这里的代码不起作用可能是什么问题?
答案 0 :(得分:1)
在mailoptions对象中,您正在使用 emailToken ,并且从未定义该变量。相反,您可能想使用user.emailToken