Search code examples
node.jsfirebasegoogle-cloud-functionsnodemailer

Problem on sending email with firebase functions


I have created a shop application with android and firebase. What I am developing now is a trigger to send an email to the customer when the invoice of the order is created on the storage bucket.

I have tested that all the code related to the trigger is correct, but I am having problems with the sendMail function. Concretely the Firebase Functions console is catching Error: Unexpected socket close.

This is my code:

function sendEmail(user, email, order, type){
  console.log("Sending Email...");
  if (type == "confirmacionPedido"){
    return transport.sendMail({
      from: "COMPANY <xxxxxxx@gmail.com>",
      to: email,
      subject: "Confirmación pedido " + order,
      html: `
            <h1> Estiamdo ${user}, </h1>
            <p> Hemos recibido su pedido correctamente. Le mantendremos infromado de su estado. </p>
            <p> Gracias por confiar en nosotros </p>
          `
    })
    .then(r => r)
    .catch(e => {
      console.log(e);
    });
  }
}

I would be very grateful if someone could help me.


Solution

  • I have followed the solution exposed on the link that has passed @John Brookfields.

    For mails to be sent from smtp.gmail.com make sure you have followed these 2 steps:

    1. In your google account make sure you have 2-Step Verification enabled.

    2. Then go to https://security.google.com/settings/security/apppasswords. Click Select app and choose Other (custom name) from the dropdown and click Generate. You will get a 16 digit code, this code should be used as password in email configuration and user remains as your email. Also, make sure {secure: true}

    Here is my configuration:

    transport: {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: '<myemail>',
            pass: '<16 digit code generated in step 2 above>',
        },
    }