Home > Software engineering >  Nodemailer error: verification email doesn't works
Nodemailer error: verification email doesn't works

Time:10-16

I am writing this code to sign up the user. I used Nodemailer to send a verification email to activate the account. The post request works well but I did not receive a verification email and here is my code:

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);
    }
}

WHEN I SEND THE REQUEST, SHOWS ME LIKE THIS

enter image description here

CodePudding user response:

Go to Google account, than Security and Less secure app access, and set to ON. Restart Your app and Now should up and running ;-) Note That - In Your .env file, enter Your credentials without any quotes

  • Related