Search code examples
javascriptfirebasegoogle-cloud-functionsnodemailer

nodemailer: Connection closed unexpectedly


I deployed a function to firebase and when I test the function to send an email I get the following error:

Error: Connection closed unexpectedly
    at SMTPConnection._onClose (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:824:34)
    at TLSSocket.SMTPConnection._onSocketClose (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:191:42)
    at Object.onceWrapper (events.js:520:26)
    at TLSSocket.emit (events.js:412:35)
    at TLSSocket.emit (domain.js:537:15)
    at net.js:686:12
    at TCP.done (_tls_wrap.js:564:7)
    at TCP.callbackTrampoline (internal/async_hooks.js:130:17) 

This is the function I use to send the email:

const transporter = nodemailer.createTransport({
  host: "host",
  port: 465,
  secure: true,
  auth: {
    user: "email",
    pass: "pass",
  },
});

exports.sendMailUser =
  functions.firestore.document("collection/Users").onUpdate(async (snapshot, context) => {
    const data = snapshot.after.data();
    const dataReceived = data.users;
    const name = dataReceived[dataReceived.length - 1].name;
    const email = dataReceived[dataReceived.length - 1].email;
    const password = dataReceived[dataReceived.length - 1].password;
    const body = "As dori crearea unui cont pe portalul e-Radauti cu urmatoarele date: <br>" + name + "<br>" + email + "<br>" + "password" + password;
    const mailOptions = {
      from: "Adaugare User pe portal-ul e-Radauti",
      to: "mail",
      subject: "Adaugare user pe portalul e-Radauti",
      bcc: "bcc",
      html: body,
    };
    console.log(mailOptions);
    await transporter.sendMail(mailOptions);
    return null;
  });

Edit: I've updated the code with @Dharmaraj suggestion and also I have the Blaze(Pay as you go) plan

enter image description here


Solution

  • I've solved this by creating a new transporter and I've added it inside the function:

    exports.sendMailUser =
      functions.firestore.document("collection/Users").onUpdate(async (snapshot, context) => {
        const transporter2 = nodemailer.createTransport({
          host: host,
          port: 465,
          debug: true,
          secure: true, 
          auth: {
            user: user,
            pass: pass,
          },
        });
        var data = snapshot.after.data();
        var dataReceived = data.users;
        var body = "As dori crearea unui cont pe portalul e-Radauti cu urmatoarele date: <br>" + dataReceived[dataReceived.length - 1].name + "<br>" + dataReceived[dataReceived.length - 1].email + "<br>" + "password:" + dataReceived[dataReceived.length - 1].password;
        var mailOptions = {
          from: "Adaugare User pe portal-ul e-Radauti",
          to: to,
          subject: "Adaugare user pe portalul e-Radauti",
          bcc: bcc,
          html: body,
        };
        //console.log(mailOptions);
        await transporter2.sendMail(mailOptions);
        return null;
      });