mitsu
mitsu

Reputation: 430

Email doesn't get sent with Gmail smtp using Nodejs

I'm trying to send an email (Gmail) smtp using nodejs but I'm getting the error:

My email and password is correct I'm using the nodemailer for sending the mail. This is my code :

    //send email to confirme gain
    var smtpTransport = nodemailer.createTransport({
        service : 'Gmail',
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        auth : {
            user : config.supportemail,
            pass : config.gmailPSW
        }
    });
    var mailOptions = {
        to : gain.emailAccount,
        from : config.supportemail,
        subject : "FATBOAR : Vous avez gagné ! ",
        text : "Félicitations ! \n \n Votre compte : "+gain.emailAccount+" a bien gagné : "+gain.libelleGain+" grace à votre ticket de caisse N°: "+gain.numTicket+". \n \n Présentez-vous à notre équipe Fatboar avec ce mail ou avec l'application Fatboar pour récupérer votre gain. \n \n Nous vous remercions pour votre visite et espérons vous revoir très prochainement pour plus de cadeau. \n \n P.S : Vous avez participé automatiquement au tirage au sort pour gagner une voiture Range Rover Evoque. \n \n Bonne chance et à bientôt ! \n \n L'équipe FATBOAR"
    }
    smtpTransport.sendMail(mailOptions,function(err){
        if(err){
            res.status(400);
            resultats = {
                "success": false,
                "message": 'Error :5 '+ err,
                "result": ''
            }
            res.json(resultats);
        }
        else{
            res.status(200);
            resultats = {
                "success": true,
                "message": "SUCCESS",
                "result": gain
            }
            res.json(resultats);
        }
    });

And this is my .env file :

#GMAIL
SECRETKEY="xxxx......="
GMAILPSW="XXXXX"
SUPPORTEMAIL="[email protected]"

I allowed access to less secured app, and activated two-step validation. With all that it gives nothing.

Upvotes: 2

Views: 285

Answers (2)

George Alexandru
George Alexandru

Reputation: 13

At first, you need to generate App password in Gmail. go in email->security and generate it. It should look like: abjsdwqrjdsdxwfx

If you want, set it with the gmailPWD "Gmail Password" in your environment variables file .env and load them with require('dotenv').config();

In this example, I am using Mailgen to generate response HTML Template, you should install and import the package on top: const Mailgen = require('mailgen');

let transporter = nodemailer.createTransport({
        service:"Gmail",
        secure:true,
        auth:{
            user: process.env.supportemail,
            pass: process.env.gmailPSW
        }
    });

const emailSend= async(userEmail,user) => {
    try{
        let mailGenerator =  new Mailgen({
            theme:"default",
            product:{
                name:"Name",
                link:`${process.env.YOUR LINK CLIENT }`
            }
        });

const email = {
            body:{
                name:userEmail,
                intro: 'Text',
                action:{
                    instructions: 'Text',
                    button:{
                        color:'#1a73e8',
                        text: 'Valideaza-ti contul.',
                        link: `${process.env.SITE_DOMAIN}` //link for redirect 
                    }
                    
                },
                outro: 'if u need help....'
            }
        }
        let emailBody = mailGenerator.generate(email);
        let message = {
            from: process.env.EMAIL,
            to:userEmail,
            subject:"welcome",
            html: emailBody
        };

        // sending the email
        await transporter.sendMail(message);
        return true;
    } catch(error){
        throw error;
    }
} // export the module
module.exports = {
    emailSend,
}

Upvotes: 1

larabee
larabee

Reputation: 156

if you have activated the two factor authentication please check this guide:

https://galleryserverpro.com/use-gmail-as-your-smtp-server-even-when-using-2-factor-authentication-2-step-verification/

Maybe you need to use the "google app password" instead.

More official informations are here: https://support.google.com/mail/answer/185833?hl=en

Maybe thats help :)

Upvotes: 1

Related Questions