简体   繁体   English

与 Firebase 函数一起使用时,Nodemailer 无法正常工作

[英]Nodemailer not working when used with Firebase Functions

I'm trying to send myself an email when a new user account is made in my web app.当在我的 web 应用程序中创建新用户帐户时,我正在尝试向自己发送一个 email。 Here is the current code I'm deploying to Firebase Functions:这是我部署到 Firebase 函数的当前代码:

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

Running a similar version on my machine using the node index.js command works no problem.使用node index.js命令在我的机器上运行类似的版本没有问题。 The problem seems to be when it runs on Firebase Functions.问题似乎是当它在 Firebase 函数上运行时。

According to your current code, it seems to be working correctly, as you said, it only works fine in your machine using the node.js.根据您当前的代码,它似乎工作正常,正如您所说,它只能在使用 node.js 的机器上正常工作。 Probably the guide to follow to get the same result is the Nodemailer as a module for Node.js .获得相同结果的指南可能是 Nodemailer 作为Node.js的模块。

Now, if you would like to do it using Cloud Functions for Firebase, I can highly recommend you to follow the Send Email Using Firebase Functions & Nodemailer guide.现在,如果您想使用 Firebase 的 Cloud Functions 来执行此操作,我强烈建议您遵循使用 Firebase 函数和 Nodemailer 发送 Email指南。

Additional guide for Send Email with Firebase functions and Nodemailer . 使用 Firebase 功能和 Nodemailer 发送 Email 的附加指南。

For me, it was failing because it wasn't importing nodemailer as I did not install it in the actual functions folder.对我来说,它失败了,因为它没有导入 nodemailer,因为我没有将它安装在实际功能文件夹中。

If you are installing nodemailer or any package please make sure that you are installing it in the actual functions folder.如果您正在安装 nodemailer 或任何 package,请确保您将其安装在实际功能文件夹中。

You can confirm if it was installed if you check package.json and see that the dependencies have nodemailer in it.如果您检查 package.json 并看到依赖项中有 nodemailer,则可以确认它是否已安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:内部 firebase 函数,与 nodemailer 一起使用时 - Error:INTERNAL firebase functions ,when using it with nodemailer 在 firebase 云函数中为 nodemailer 创建自定义 email 模板? - Creating custom email template for nodemailer in firebase cloud functions? 运行 Firebase Cloud Functions 时使用哪个服务帐户? - Which service account is used when running Firebase Cloud Functions? 错误:使用 firebase 云函数时已超过期限 - Error: deadline-exceeded when working with firebase cloud functions Firebase 函数环境变量不起作用 - Firebase functions environment variables not working 当您修改许多其他函数使用的代码时,Firebase 如何更新一个可调用的 function - How does Firebase update one callable function when you modify a code used by many other functions Flutter - Firebase 区域不工作的可调用函数 - Flutter - Firebase callable functions with region not working 可以使用 Firebase Auth.auth() 函数检索 GoogleSignIn 用户吗? - Can Firebase Auth.auth() functions be used to retrieve a GoogleSignIn user? Jest useFakeTimers() 在与 firebase 模拟器一起使用时会阻塞 - Jest useFakeTimers() blocks when used with firebase emulators NodeJs function 在 Firebase 云函数中无法按预期工作 - NodeJs function not working as expected in Firebase Cloud functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM