Why nodemailer give me this error when I try to send an e-mail using gmail address? Error: Can’t set headers after they are sent

I am working on a Firebase project that uses Google Cloud Functions. I am some problem trying to implement a cloud function that uses nodemailer in order to send an e-mail.

I have something like this in my code:

import * as functions from 'firebase-functions';
import admin = require('firebase-admin');

import nodemailer = require('nodemailer');

admin.initializeApp();

  const transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: 'my-sender-mail@gmail.com',
        pass: 'my-password'
    }
  });

  const mailOptions = {
    from: `my-sender-mail@gmail.com`,
    to: `my-destination-mail@gmail.com`,
    subject: 'contact form message',
    text: 'TEST TEST TEST'
    /*
    html: `<h1>Order Confirmation</h1>
     <p> <b>Email: </b>TEST </p>`
     */
  };

  export const sendEmailNotification = functions.https.onRequest((request, response) => {
    console.log("sendEmailNotification START 2");

    transporter.sendMail(mailOptions, (error, data) => {
      if (error) {
          console.log(error);
          response.status(500).json({message: 'ERROR SENDING MAIL !!!'});
      }
      console.log("Sent!");
      response.status(200).json({message: 'NOTIFICATION MAIL SENT !!!'});
    });

  });

So as you can see I prepared the transporter object setting the gmail confifuration inserting user and password as authentication.

Then the maulOption object contains the e-mail to be sent.

Finnally I defined a GET function that called should send the e-mail.

The problem is that it is not working and I am obtaining the following error into the Firebase cloud function log:

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/worker/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/worker/node_modules/express/lib/response.js:267:15)
    at transporter.sendMail (/srv/lib/index.js:72:30)
    at transporter.send.args (/srv/node_modules/nodemailer/lib/mailer/index.js:226:21)
    at connection.login.err (/srv/node_modules/nodemailer/lib/smtp-transport/index.js:282:36)
    at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1513:20)
    at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:540:26) 

What it means? What is wrong? What am I missing? I am going crazy...how can I fix my code in order to correctly send this e-mail?

EDIT-1: After that I solved the header error I am obtaining this new error message:

{ 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 t12sm13191635ios.12 - gsmtp
    at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:774:19)
    at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1513:34)
    at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
    at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:932:20)
    at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:739:14)
    at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
  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 t12sm13191635ios.12 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' }


Read more here: https://stackoverflow.com/questions/64894188/why-nodemailer-give-me-this-error-when-i-try-to-send-an-e-mail-using-gmail-addre

Content Attribution

This content was originally published by AndreaNobili at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: