Nodemailer error: verification email doesn't works
Nodemailer error: verification email doesn't works
我正在编写此代码来注册用户。
我使用 Nodemailer 发送验证邮件来激活帐户。
post 请求运行良好,但我没有收到验证邮件,这是我的代码:
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});
exports.signup = async(req, res) => {
const { email } = req.body
if (!email) {
return res.status(422).send({ message: "Missing email." })
}
try {
const existingUser = await User.findOne({ email }).exec();
if (existingUser) {
return res.status(409).send({
message: "Email is already in use."
});
}
const user = await new User({
_id: new mongoose.Types.ObjectId,
email: email
}).save();
const verificationToken = user.generateVerificationToken();
const url = `http://localhost:5000/api/verify/${verificationToken}`
transporter.sendMail({
to: email,
subject: 'Verify Account',
html: `Click <a href = '${url}'>here</a> to confirm your email.`
})
return res.status(201).send({
message: `Sent a verification email to ${email}`
});
} catch (err) {
return res.status(500).send(err);
}
}
当我发送请求时,显示如下
转到Google帐户,然后安全和Less secure app access
,并设置为ON
.重新启动您的应用程序,现在应该启动 运行 ;-)
请注意 - 在您的 .env
文件中,输入您的凭据,但不要输入任何 quotes
我正在编写此代码来注册用户。 我使用 Nodemailer 发送验证邮件来激活帐户。 post 请求运行良好,但我没有收到验证邮件,这是我的代码:
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});
exports.signup = async(req, res) => {
const { email } = req.body
if (!email) {
return res.status(422).send({ message: "Missing email." })
}
try {
const existingUser = await User.findOne({ email }).exec();
if (existingUser) {
return res.status(409).send({
message: "Email is already in use."
});
}
const user = await new User({
_id: new mongoose.Types.ObjectId,
email: email
}).save();
const verificationToken = user.generateVerificationToken();
const url = `http://localhost:5000/api/verify/${verificationToken}`
transporter.sendMail({
to: email,
subject: 'Verify Account',
html: `Click <a href = '${url}'>here</a> to confirm your email.`
})
return res.status(201).send({
message: `Sent a verification email to ${email}`
});
} catch (err) {
return res.status(500).send(err);
}
}
当我发送请求时,显示如下
转到Google帐户,然后安全和Less secure app access
,并设置为ON
.重新启动您的应用程序,现在应该启动 运行 ;-)
请注意 - 在您的 .env
文件中,输入您的凭据,但不要输入任何 quotes