Clouddie
Clouddie

Reputation: 103

Nodemail to send mail for firebase app with service account

I get the following message when trying to send mail with Nodemailer :

    '535-5.7.8 Username and Password not accepted. 
    Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials y42sm13399804wrc.51 - gsmtp',
  responseCode: 535,
  command: 'AUTH XOAUTH2' }

I am using a service account from a firebase project, and have granted access to the GMail API. But the nodemailer docs for 2LO is really scarce so I wonder if anyone could help me find if I use the correct credentials ?

user (functions.config().gmail.user) : [email protected]

-

function sendContactMail(contactName, contactEmail, contactDate, contactText) {


  // Create transport
  let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: functions.config().gmail.user,
        serviceClient: functions.config().gmail.client_id,
        privateKey: functions.config().gmail.private_key
        //accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
        //expires: 1484314697598
    }
  });

  // Mail Options
  let mailOptions = {
    from: `"${APP_NAME}" <${SENDER}>`,
    replyTo: `${contactEmail}`,
    to: '[email protected]',
    subject: `Nouveau contact photo de ${contactName}`,
    html: `Nom : ${contactName}<br/>
           Email : ${contactEmail}<br/>
           Date du mariage : ${contactDate}<br/>
           Message : ${contactText}`,
    disableFileAccess: true,
    disableUrlAccess:true
  };

  // Send Mail
  return transporter.sendMail(mailOptions);
}

Upvotes: 1

Views: 1048

Answers (2)

Carlos Casallas
Carlos Casallas

Reputation: 324

Has your account two steps authentication enabled? In that case generate an app password on https://myaccount.google.com/apppasswords and use it instead of your normal password:

const mailTransport = nodemailer.createTransport(
    `smtps://[email protected]:[email protected]`);

Upvotes: 1

Panup Pong
Panup Pong

Reputation: 1891

I have Cloud Function with this code and it's work fine. Try this

mailTransport = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: ${email},
    pass: ${password}
  }
});

Upvotes: 0

Related Questions