Thursday 18 May 2017

Sending email Nodemailer in NodeJS results in Error: Invalid Login

I am trying to send an email using Nodemailer and a gmail account from a nodeJS server. I understand that there are many questions regarding how to do that on Stackoverflow.

One of the most helpful was the answer below. Which describes how to get access to Drive API (GMAIL API in my case) using Oauth playground tool.

How do I authorise an app (web or installed) without user intervention? (canonical ?)

So I followed these steps exactly (Plus allowing less securing apps) then in nodejs I am trying to send an email using Nodemailer as following:

createTransport

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    type: 'OAuth2',
    user: globals.admin_email,
    clientId: globals.client_id,
    clientSecret: globals.secret,
    refreshToken: globals.refresh_token,
    accessToken: globals.access_token,
  },
});

define email options

var mail_options = {
        from: globals.admin_email,
        to: receiver_email, // list of receivers
        subject: 'API key request',
        html: '<h3>A new API key request<h3> </br> <h5>From: ' + user_name + '</h5> </br>' +
        '<h5>Email: ' + user_email + '</h5> </br> <h5> Institute: ' + user_insititute + '</h5>' +
        '<p>You can generate keys by going to /gen_keys</p>',
    };

sendMail

transporter.sendMail(mail_options, function (err, info) {
        if (err) {
            console.log(err);
            return err;
        }
        console.log('Sent email: ' + info.response);
    })

However when I run the code I recieve the following ERROR

{ [Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials 43sm6162153wrx.26 - gsmtp]
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials 43sm6162153wrx.26 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' }

which is weird since I am not using basic authentication (username and password). I am using Oauth using credentials I created on developer console (OAuth client ID)

Any suggestions ?



via Abdel-Rahman Shoman

No comments:

Post a Comment