因此,我创建了mern stack电子商务应用程序,使用nodejs作为服务器。我试图向新用户发送确认电子邮件,使用nodemailer和gmail作为主机。我试图启用两步验证并生成密码,但我仍然没有收到任何电子邮件,并且还收到标题中写的错误。有人能帮忙吗?
我创建的包含发送邮件功能的类:
import nodemailer from 'nodemailer';
export const sendEmail=async(subject,message,send_to,send_from,reply_to)=>{
var transporter=nodemailer.createTransport({
service:process.env.EMAIL_SERVICE,
auth:{
user:process.env.EMAIL_USER,
pass:process.env.EMAIL_PASS
},
tls:{
rejectUnauthorized:false,
}
});
const options={
from:send_from,
to:send_to,
replyTo:reply_to,
subject,
html:message,
}
transporter.sendMail(options,function(err,info){
if(err){
console.log(err)
}
else{
console.log(info)
}
})
}
export default sendEmail;
报名途径:
export const register=catchAsync(async(req,res,next)=>{
const{fullName,email,password,confirmPassword,type,Wishlist}=req.body;
if(!fullName||!email||!password||!confirmPassword||!type){
return next(new ErrorHandling("One or more of the fields is empty",400));
}
if(password!=confirmPassword){
return next(new ErrorHandling("Password fields does not equal",400));
}
if(!validateEmail(email)){
return next(new ErrorHandling("The email you entered is illegal",400));
}
if(await User.findOne({email})){
return next(new ErrorHandling("Email already exists",400));
}
try{
await sendEmail('Consumer account confirmation',`<h3>Welcome to Base herbal cosmetics</h3><p>Thank you for your registration</p><button>press here to join</button>`,process.env.EMAIL_USER,email,email)
}catch(err){
return next(new ErrorHandling(err.message,err.statusCode));
}
const newUser=await User.create({
fullName,
email,
password,
confirmPassword,
type,
Wishlist
})
res.status(201).json({
status:"success",
data:{
newUser
}
})
});
值得一提的是,用户确实添加到我的数据库和一切,但电子邮件工作正常。
1条答案
按热度按时间rpppsulh1#
从您的谷歌帐户设置,点击安全选项卡,转到两步验证并启用它(这是确定的,因为它已经启用)。现在,在两步验证下,向下滚动到应用程序密码,生成一个唯一的密码,并在代码中使用它,而不是登录电子邮件时使用的传统密码。
这是因为Google的安全性。传统登录时,输入密码后还有一个验证步骤;但由于NodeMailer只使用一个密码阶段,这就是为什么谷歌为那些可能想要访问他们的帐户的人提供了这个选项。