[英]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.