satya
satya

Reputation: 3560

Could not send the mail using Nodemailer and Node.js

I could not send mail using Node.js. I am getting the below error.

{
    "error": {
        "code": "EAUTH",
        "response": "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvp\n534-5.7.14 j_smRtzyG0TYeMSMFUYlhfCGtpt9DmDE4ZR140rMTKNkk0ZwgtIEQ6Kik51kVBedggHrBA\n534-5.7.14 3mM-ha-68KYhP-IPiHTVQkjUj6HlwdSciwJUgyRqIYKTxTtA9N96CmRzCRiPpIPMf6UGu3\n534-5.7.14 ismcOTMvxqQK2naujp_fgkbLjzwPMj47j5cOoaWdIkdwy4qmqMXnw8ib3sdkRfBeUgjVGB\n534-5.7.14 iiSXqql1vBLMkKnk_HNyGdeQu8OwI> Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14  Learn more at\n534 5.7.14  https://support.google.com/mail/answer/78754 k97sm25351307qkh.29 - gsmtp",
        "responseCode": 534,
        "command": "AUTH PLAIN"
    }
}

Here is my code:

    var nodemailer = require("nodemailer");
    var smtpTransport = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: '[email protected]',
            pass: '********'
        }
    });
    rand=crypto.randomBytes(20).toString('hex');
    host=req.get('host');
    link="http://"+req.get('host')+"/api/users/verify?id="+rand;

mailOptions={
              from: "[email protected]",
              to : email,
              subject : "Please confirm your Vodex account",
              html : "Hello,<br> Please Click on the link to verify your Account.<br><a href="+link+">Click here to verify</a>"
}
  smtpTransport.sendMail(mailOptions,function(error, response){
        if (error) {
                    var data={'error':error};
                    res.send(data);
        }else{
                    db.f_user_login.update({_id : docs._id },{$set:{verification_id:rand}},function(err,doc){
        if (!err) {
                    var edata=[{"email": docs.email,"dob": docs.dob,"created_date":docs.created_date ,"id": docs._id,"updated_date":docs.updated_date}];
                    var data={"statusCode": 200,"data":edata ,"message": "The user registered successfull and an email is sent to your registered email for account verification."};
                    res.send(data);
                }
            })
        }
    })

This code is working fine for localhost:8989 but now I have deployed the code in Heroku and my URL is like this https://node-fgdp.herokuapp.com/api/users/signup. Here I could not send the mail and getting those above error. I need to send the mail using nodemailer and Node.js.

Upvotes: 4

Views: 4104

Answers (1)

Kayvan Mazaheri
Kayvan Mazaheri

Reputation: 2597

As a security precaution, Google may require you to complete an additional step called Unlock Captcha when signing into a new device or application.

You might need to manually allow access to your Google account.

Login to your Google account using a browser, then visit the link below to unlock it: https://accounts.google.com/DisplayUnlockCaptcha

After clicking Allow button, you should see the message:

Account access enabled. Please try signing in to your Google account again from your new device or application.

Reference:
https://support.google.com/mail/answer/7126229?visit_id=1-636373008143011090-3861871695&rd=2#cantsignin

Upvotes: 12

Related Questions