【问题标题】:Nodemailer not working when used with Firebase Functions与 Firebase 函数一起使用时,Nodemailer 无法正常工作
【发布时间】:2022-01-23 22:44:35
【问题描述】:

当在我的网络应用中创建新用户帐户时,我正尝试向自己发送一封电子邮件。这是我正在部署到 Firebase Functions 的当前代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");

admin.initializeApp();

require("dotenv").config();

const {
    SENDER_EMAIL,
    SENDER_PASSWORD
} = process.env;

exports.sendEmailNotification = functions.firestore.document("users/{userId}").onCreate(async (snapshot, context) => {
    const data = snapshot.data();

    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: "smtp.gmail.com",
        port: 465,
        secure: true,
        auth: {
            user: SENDER_EMAIL,
            pass: SENDER_PASSWORD,
        },
    });

    // send mail with defined transport object
    let info = await transporter.sendMail({
        from: `"MY_APP_NAME" <${SENDER_EMAIL}>`,
        to: "MY_PERSONAL_GMAIL_ACCOUNT",
        subject: `A New User Has Joined MY_APP_NAME!`,
        text: `A new user has joined MY_APP_NAME. Name: ${data.name}, email: ${data.email}`
    });
})

使用node index.js 命令在我的机器上运行类似的版本没有问题。问题似乎出在它在 Firebase Functions 上运行时。

【问题讨论】:

    标签: javascript firebase google-cloud-functions nodemailer


    【解决方案1】:

    根据您当前的代码,它似乎工作正常,正如您所说,它只能在使用 node.js 的机器上正常工作。 获得相同结果的指南可能是 Nodemailer 作为Node.js 的模块。

    现在,如果您想使用 Cloud Functions for Firebase 执行此操作,我强烈建议您遵循 Send Email Using Firebase Functions & Nodemailer 指南。

    Send Email with Firebase functions and Nodemailer 的附加指南。

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 1970-01-01
      • 2014-05-30
      • 2020-10-03
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      相关资源
      最近更新 更多