“错误:在建立安全TLS连接之前,客户端网络套接字已断开连接"在Firebase功能上 [英] "Error: Client network socket disconnected before secure TLS connection was established" on Firebase Function

查看:295
本文介绍了“错误:在建立安全TLS连接之前,客户端网络套接字已断开连接"在Firebase功能上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 nodemailer 与我的Firebase函数(服务器端函数)一起用于我的React.js项目,并收到错误消息:错误:客户端网络套接字在安全TLS连接建立之前已断开连接已建立... ,并导致 一些 电子邮件无法发送出去.对于这个项目来说,这是一个大问题,因为客户端不会丢失从系统发送的电子邮件.为什么会出现此错误,我该如何解决?

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: `email123@gmail.com`,
            to: 'email123@gmail.com',
            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)
    }
  });

功能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
}

完整错误:

推荐答案

您应该返回一个承诺,以避免诸如提前终止之类的意外功能行为.

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

检查 Nodemailer API文档

我可以看到 transporter.sendMail 返回了诺言.因此,只要兑现这一承诺就可以解决您的问题:

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)

}

这篇关于“错误:在建立安全TLS连接之前,客户端网络套接字已断开连接"在Firebase功能上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆