Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Mailer Error

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?

like image 642
Sriram R Avatar asked Jan 12 '18 13:01

Sriram R


3 Answers

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 :)

like image 57
Shahzaib Sheikh Avatar answered Sep 25 '22 01:09

Shahzaib Sheikh


Solved same error from a local nodejs test app, by what
@Zaheen replied to: Nodemailer with Gmail and NodeJS , part of his reply is copied here:

var smtpTransport = require('nodemailer-smtp-transport');

var transporter = nodemailer.createTransport(smtpTransport({
  service: 'gmail',
  host: 'smtp.gmail.com',
  auth: {
    user: '[email protected]',
    pass: 'realpasswordforaboveaccount'
  }
}));

(BTW Accounts with 2FactorAuth cannot enable less secure apps).
I used nodemailer-smpt-transport, xoauth2, and added host to auth{}.


Haven't yet done it with Firebase, that's next ... hopefully.. :)

like image 43
verNsíon1 Avatar answered Sep 21 '22 01:09

verNsíon1


Using nodemailer, you have to providean email and password of the account you are sending the email from, Firebase stores that for you so you don't have to write it directly on your code. For that to happen you have to configure the gmail.email and gmail.password Google Cloud environment variables.

In your console, within the path you store your functions /yourProject/functions$ set your variables this way:

firebase functions:config:set gmail.email="[email protected]" gmail.password="yourPassword"

and then you can use it this way:

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

I hope this is helpful for you all , it what was causing the 'missing credentialas plain error' in my case

like image 30
RamiroIsBack Avatar answered Sep 24 '22 01:09

RamiroIsBack