Node Mailer Error

Sriram R picture Sriram R · Jan 12, 2018 · Viewed 9.9k times · Source

I am using nodemailer in my Firebase Cloud Functions to send a mail when a data is added to the realtime database.

Code:

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');

const gmailEmail = '[email protected]';
const gmailPassword = 'mypassword';

const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
  user: gmailEmail,
  password: gmailPassword
}
});

const APP_NAME = 'ABC In'

exports.salonCreatedAccount = functions.database.instance('abc-
in').ref('/abc/{def}').onCreate(event => {
const snapshot = event.data;
const val = snapshot.val()
console.log(val);

const email = val.email;
const displayname = val.name;

return sendconfirmationEmail(email, displayname);
});

function sendconfirmationEmail(email, displayName){
const mailOptions = {
  from: `${APP_NAME} <[email protected]>`,
  to: email
};

mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Some Text`;
return mailTransport.sendMail(mailOptions).then(() => {
  console.log(`New welcome mail sent to ${email}`);
});
}

I am getting this following error while executing. NOTE: I have made sure that the email and password is right and there's no mistake there.

Error:

Error: Missing credentials for "PLAIN"
at SMTPConnection._formatError (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:591:19)
at SMTPConnection.login (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:340:38)
at connection.connect (/user_code/node_modules/nodemailer/lib/smtp-transport/index.js:270:32)
at SMTPConnection.once (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:188:17)
at SMTPConnection.g (events.js:292:16)
at emitNone (events.js:86:13)
at SMTPConnection.emit (events.js:185:7)
at SMTPConnection._actionEHLO (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:1113:14)
at SMTPConnection._processResponse (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:747:20)
at SMTPConnection._onData (/user_code/node_modules/nodemailer/lib/smtp-connection/index.js:543:14)

How do I fix this?

Answer

Shahzaib Sheikh picture Shahzaib Sheikh · Nov 12, 2018

Rename password to pass in auth object. for e.g

const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword
  }
});

I had the same problem, and solved it like that.

found out by diving in the code of nodemailer where this message is being logged the code look like this.

  if (this._auth.user && this._auth.pass) { // this condition <---
    this._auth.credentials = {
      user: this._auth.user,
      pass: this._auth.pass
    };
  } else {
    return callback(this._formatError('Missing credentials for "' + this._authMethod + '"', 'EAUTH', false, 'API'));
  }

Hope this helps :)