首页 > 解决方案 > 使用 firebase 功能发送电子邮件时出现问题

问题描述

我用 android 和 firebase 创建了一个商店应用程序。我现在正在开发的是在存储桶上创建订单发票时向客户发送电子邮件的触发器。

我已经测试了与触发器相关的所有代码都是正确的,但是我遇到了sendMail函数问题。具体来说,Firebase Functions 控制台很受欢迎Error: Unexpected socket close

这是我的代码:

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);
    });
  }
}

如果有人可以帮助我,我将不胜感激。

标签: node.jsfirebasegoogle-cloud-functionsnodemailer

解决方案


我遵循了通过@John Brookfields的链接上公开的解决方案。

对于要从​​ smtp.gmail.com 发送的邮件,请确保您已遵循以下 2 个步骤:

  1. 在您的 Google 帐户中,确保您启用了两步验证。

  2. 然后转到https://security.google.com/settings/security/apppasswords。单击选择应用程序并从下拉列表中选择其他(自定义名称),然后单击生成。您将获得一个 16 位代码,此代码应用作电子邮件配置中的密码,并且用户仍然是您的电子邮件。另外,确保 {secure: true}

这是我的配置:

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

推荐阅读