Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  136] [ 7]  / answers: 1 / hits: 19029  / 4 Years ago, wed, november 18, 2020, 12:00:00

I am using nodemailer with my Firebase Functions (server-side functions) for my React.js project and am getting the error: Error: Client network socket disconnected before secure TLS connection was established... and causing some emails to not be sent out. This is a big issue for this project, as the client can't be missing emails sent from the system. Why am I getting this error and how might I remedy it?


Firebase Function index.js:


"use strict";
import functions = require('firebase-functions');
import admin = require("firebase-admin");
import nodemailer = require('nodemailer');
import { DocumentSnapshot } from 'firebase-functions/lib/providers/firestore';
import { Change, EventContext } from 'firebase-functions';
import { Status } from './common';

admin.initializeApp(functions.config().firebase);

export const onUserCreated = functions.firestore.document('users/{userId}')
.onCreate(async (snap: { data: () => any; }) => {
console.log("Client create heard! Starting inner...")
const newValue = snap.data();

try {
console.log("Started try{}...")

// Template it
const htmlEmail =
`
<div>
<h2>client Sign Up</h2>
`
// Config it
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: functions.config().email.user,
pass: functions.config().email.password
}
})
console.log("transporter = " + transporter)

// Pack it
const mailOptions = {
from: `[email protected]`,
to: '[email protected]',
replyTo: `${newValue.email}`,
subject: `user sign up`,
text: `A new user sign up with the email of ${newValue.email}.`,
html: htmlEmail
}

// Send it
transporter.sendMail(mailOptions, (err: any) => {
if(err) {
console.error(err);
} else {
console.log("Successfully sent mail with sendMail()!");
}
})
} catch (error) {
console.error(error)
}
});

Functions package.json:


{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"@types/nodemailer": "^6.4.0",
"firebase-admin": "^9.4.1",
"firebase-functions": "^3.11.0",
"nodemailer": "^6.4.16"
},
"devDependencies": {
"firebase-functions-test": "^0.2.3",
"tslint": "^5.12.0",
"typescript": "^3.8.0"
},
"private": true
}


Full error:
enter


More From » node.js

 Answers
75

You should return a promise to avoid unexpected Fuctions behaviour like early termination.


Checking Nodemailer API documentation


I can see that transporter.sendMail returns a promise. So just returning this promise should fix your issue:


export const onUserCreated = functions.firestore.document('users/{userId}')
.onCreate(async (snap: { data: () => any; }) => {

....
return transporter.sendMail(mailOptions)

}

[#50537] Friday, November 6, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;