I'm trying to send myself an email when a new user account is made in my web app. Here is the current code I'm deploying to 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}`
});
})
Running a similar version on my machine using the node index.js command works no problem. The problem seems to be when it runs on Firebase Functions.
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. Probably the guide to follow to get the same result is the Nodemailer as a module for 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.
Additional guide for Send Email with Firebase functions and Nodemailer.