3

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 ?

Edit: Some errors were dure to wrongly set scope. I checked the authorized scope and it is https://mail.google.com/ So this is probably is not the issue here

Community
  • 1
  • 1
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
  • Since no one have provided an answer so far, thought I would share a workaround that might help .. this article explains how `crypto` can be used in normal authentication to encrypt the password .. http://thisdavej.com/node-js-sending-email-notifications-using-nodemailer-and-gmail/#comment-1494 – Abdelrahman Shoman May 21 '17 at 08:23
  • I actually have the same error ;( Its a long time ago since you asek this question. Did you manage to get this to work? – JiiB Jan 25 '19 at 23:05

0 Answers0