Firebase 函数上显示“错误:在建立安全 TLS 连接之前客户端网络套接字已断开”

dou*_*mes 3 javascript node.js firebase nodemailer google-cloud-functions

我正在nodemailer为我的 React.js 项目使用 Firebase 函数(服务器端函数),但收到错误:Error: Client network socket disconnected before secure TLS connection was established...并导致一些电子邮件无法发送。这对于这个项目来说是一个大问题,因为客户端不能丢失从系统发送的电子邮件。为什么我会收到此错误以及如何补救?

Firebase 函数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)
    }
  });
Run Code Online (Sandbox Code Playgroud)

函数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
}

Run Code Online (Sandbox Code Playgroud)

完整错误: 在此输入图像描述

小智 5

就我而言,将 TLS 版本添加到选项后错误消失了nodemailer

let transporter = nodemailer.createTransport({
    host: 'smtp-server',
    port: 25,
    tls: {
      ciphers : 'SSLv3',
    },
  });
Run Code Online (Sandbox Code Playgroud)