NodeMailer 发送电子邮件时出错。仅在生产中

问题描述 投票:0回答:3

我使用以下代码创建一个用于发送电子邮件的 SMTPtransporter。该代码在我的计算机上运行得非常好。使用 Yarn 版本“1.22.17”。

import * as nodemailer from 'nodemailer';
import * as SMTPTransport from "nodemailer/lib/smtp-transport";

const poolOptions = {
      pool: true, 
      maxConnections: 1, 
      maxMessages: 5
}

const smtpOptions = {
host: 'smtp.gmail.com',
  port: 587,
  secure: false,
  auth: {
    user: SMTP_USER,
    pass: SMTP_PASSWORD
  },
  tls:{
        ciphers: 'SSLv3',
        rejectUnauthorized: false
      }


}


const nodemailerOptions: SMTPTransport.Options = {
      ...poolOptions,
      ...smtpOptions
    }

const transport = nodemailer.createTransport(nodemailerOptions);

发送电子邮件功能:

export function sendMail(
  to: string, subject: string, text: string, html: string) {
  const mailOptions = {
    from: 'Bobo <[email protected]>', to, subject, text, html
  };

  return new Promise((resolve, reject) => {
    transport.sendMail(mailOptions, (error, info) => {
      if (error) {
        console.error(
          `Failed to send email to ${to} with body [${text}]`, error);
        reject(new Error('Email sending failed'));
      } else {
        console.log(
          `Email sent to ${to} with subject [${subject}]`, info.response);
        resolve();
      }
    });
  });
}

同时在服务器中,每次我尝试发送电子邮件时都会收到以下错误:

{ Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
  errno: 'ECONNRESET',
  code: 'ECONNECTION',
  syscall: 'read',
  command: 'CONN' }

它与部署在 ubuntu 服务器中的相同应用程序具有相同的 Yarn 版本。 如果有人可以帮助我,我将非常感激。

注意:: 对于服务器上的部署,我使用 Nginx 将端口 80 上的所有请求转发到 3000(应用程序运行)。并且 SMTP 端口已开放(25,587)

javascript node.js typescript nodemailer
3个回答
0
投票

这似乎是某种与 TLS 节点库相关的问题,已经有一个问题解决了这个问题,你可以在这里找到它


0
投票
问题是由于托管公司对网络的限制造成的。我将 smtp 主机调整为允许流量的主机。


0
投票
Amigo,我们无法解决内部问题,请参阅 vi,配置主机、端口和安全错误。

host: "smtp.gmail.com", port: 465, secure: true,
a porta que o gmail usa é a 465。
o 安全 é 真实。

Desculpe por não saber mais informações a respeito, mas pode ser um bom ponto de partida para começar a Solutionr o Problema.

© www.soinside.com 2019 - 2024. All rights reserved.