8

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 = 'myemail.in@gmail.com';
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} <abc.in@gmail.com>`,
  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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sriram R
  • 2,109
  • 3
  • 23
  • 40

5 Answers5

6

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

Shahzaib Sheikh
  • 647
  • 1
  • 9
  • 21
2

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: 'somerealemail@gmail.com',
    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.. :)

verNsíon1
  • 53
  • 7
  • WRT the 2FactorAuth note a useful feature is the app password https://support.google.com/accounts/answer/185833?hl=en – carruthd May 23 '18 at 17:11
1

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="senderEmail@gmail.com" 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

RamiroIsBack
  • 260
  • 1
  • 3
  • 15
0

According to the official docs, GMail may disable basic auth in some special cases.

To prevent having login issues you should either use OAuth2 or use another delivery provider and preferably a dedicated one.

Read more here: https://nodemailer.com/usage/using-gmail/

Daniel Diekmeier
  • 3,401
  • 18
  • 33
0

I had this same error and could not find any solution online, until now.

My solution for this exact error "Missing credentials for "PLAIN"":

  • Go to Authentication in Firebase Console
  • Click on Sign-In Method
  • Select Google
  • Make sure it is enabled

That solved my error. (Same code as you)

Anders Ekman
  • 323
  • 5
  • 17