【问题标题】:Nodemailer 6.1.1 not working with NodeJs >=12Nodemailer 6.1.1 不适用于 NodeJs >=12
【发布时间】:2020-11-30 01:29:10
【问题描述】:

当我将我的 nodejs 版本更新为 12 时,我遇到了 nodemailer 的行为。

现在,当我尝试发送电子邮件时,我得到:

DEBUG Creating transport: nodemailer (6.1.1; +https://nodemailer.com/; SMTP/6.1.1[client:6.1.1])
DEBUG Sending mail using SMTP/6.1.1[client:6.1.1]
DEBUG [9mzLKQAwcwQ] Resolved mail.mycompany.com as xxx.xxx.xxx.xxx [cache miss]
INFO  [9mzLKQAwcwQ] Connection established to xxx.xxx.xxx.xxx:587
DEBUG [9mzLKQAwcwQ] S: 220 mail.mycompany.com ESMTP
DEBUG [9mzLKQAwcwQ] C: EHLO [127.0.0.1]
DEBUG [9mzLKQAwcwQ] S: 250-mail.mycompany.com
DEBUG [9mzLKQAwcwQ] S: 250-STARTTLS
DEBUG [9mzLKQAwcwQ] S: 250-PIPELINING
DEBUG [9mzLKQAwcwQ] S: 250-8BITMIME
DEBUG [9mzLKQAwcwQ] S: 250-SIZE 23068672
DEBUG [9mzLKQAwcwQ] S: 250 AUTH LOGIN PLAIN CRAM-MD5
DEBUG [9mzLKQAwcwQ] C: STARTTLS
DEBUG [9mzLKQAwcwQ] S: 220 ready for tls
ERROR [9mzLKQAwcwQ] 139673645745984:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:../deps/openssl/openssl/ssl/statem/statem_lib.c:1922:
ERROR [9mzLKQAwcwQ]
DEBUG [9mzLKQAwcwQ] Closing connection to the server using "end"
ERROR Send Error: 139673645745984:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:../deps/openssl/openssl/ssl/statem/statem_lib.c:1922:
ERROR
[Error: 139673645745984:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:../deps/openssl/openssl/ssl/statem/statem_lib.c:1922:
] {
  library: 'SSL routines',
  function: 'ssl_choose_client_version',
  reason: 'unsupported protocol',
  code: 'ESOCKET',
  command: 'CONN'
}
INFO  [9mzLKQAwcwQ] Connection closed
INFO  [9mzLKQAwcwQ] Connection closed

这是我的 SMTP 传输配置:

nodemailer.createTransport({
    host: 'mail.mycompany.com',
    port: 587,
    debug: true,
    logger: true,
    tls: {
      secure: false,
      ignoreTLS: true,
      rejectUnauthorized: false
    },
    auth: {
      user: 'user',
      pass: 'pass'
    }
})

如果我更改为 v11 的 最新的 nodejs 版本:11.15.0,一切都会恢复正常。只有v>12 会发生这种情况,因为我尝试使用12.1.012.2.0 并得到与上述相同的错误。

有人尝试相同的行为?有什么建议吗?

注意:我已将 nodemailer 更新到最新版本 6.1.1,正如我在 Q 标题中所说的那样。

【问题讨论】:

  • 在使用带有名为 Axios 的不同包的自签名证书时遇到相同的行为。您使用的是自签名证书吗?

标签: javascript node.js nodemailer


【解决方案1】:

如果您的邮件服务器只支持 TLS 1.0,只需添加:

tls: { secureProtocol: "TLSv1_method" }

【讨论】:

  • 这对我有用,但我选择了tls: { minVersion: 'TLSv1' } 以支持更新的 TLS 版本。
【解决方案2】:

更新:

我终于找到了解决方法 - 很确定您遇到了同样的问题。从节点 12 开始失败的原因是节点版本 12 最终禁用了 TLS 1.0 支持。不幸的是,我们的测试邮件服务器仅支持 TLS 1.0,因此没有有效的握手选项导致 nodemailer 在握手期间失败。

这也可能与版本 11.4 中的更改有关,其中 TLS.Min 版本已修改为 TLS 版本 1.2 - 请参阅 https://nodejs.org/api/tls.html#tls_tls_default_min_version

无论如何,我的修复包括让我的电子邮件服务器停止使用 TLS 1.0 并开始使用 TLS 1.2 或 TLS 1.3。

我在服务器上添加了一堆注册表项,并在邮件设置中强制执行 TLS。我的服务器是 Windows 2008...

1.) 以“管理员”身份打开“RegEdit”(以管理员身份运行)

2.) 导航到以下注册表项

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\

3.) 添加/更新以下每个子键,最终 DWORD 值如图所示:

SSL 2.0\Client   DisabledByDefault : 1
SSL 2.0\Server   DisabledByDefault : 1
SSL 2.0\Server   Enabled : 0

SSL 3.0\Client   DisabledByDefault : 1
SSL 3.0\Server   DisabledByDefault : 1
SSL 3.0\Server   Enabled : 0

TLS 1.0\Server   DisabledByDefault : 0
TLS 1.0\Server   Enabled : 1

TLS 1.2\Client   DisabledByDefault : 0
TLS 1.2\Server   DisabledByDefault : 0
TLS 1.2\Server   Enabled : 1

TLS 1.3\Client   DisabledByDefault : 0
TLS 1.3\Server   DisabledByDefault : 0
TLS 1.3\Server   Enabled : 1

最终应该看起来像这样:https://support.solarwinds.com/SuccessCenter/s/article/Enable-TLS-1-2-on-Windows-Server-2008

此外,Next 链接在服务器上添加了对大于 1.0 的 TLS 的客户端支持(不需要修复连接到服务器的客户端,但可能也希望将其用于出站连接):

https://support.microsoft.com/en-us/help/3140245/update-to-enable-tls-1-1-and-tls-1-2-as-default-secure-protocols-in-wi

最后,重新启动您的电子邮件服务器,以便启用 TLS,您可以通过检查 TLS 连接来检查注册表设置是否已解决问题 - 我使用了此链接:https://www.checktls.com/TestReceiver

在我的代码中,我碰巧将我的传输器 tls 部分更新为以下内容,但后来我使用原始设置进行了测试,一切正常 - 所以问题 100% 与不支持除 TLS 1.0 以外的任何内容的电子邮件服务器设置有关...

const transporter = nodemailer.createTransport({
  host: 'email.ourhost.com',
  port: 25,
  secure: false,
  auth: {
    user: 'test',
    pass: 'pass'
  },
        tls: {
          rejectUnauthorized: false,
          ignoreTLS: false,
          requireTLS: true
        }
      });

希望对您有所帮助...

【讨论】:

  • 好的,我赞成你对研究的回答,但我还没有测试过。我的邮件服务器在 linux 上,所以我需要看看我需要改变什么。稍后我会写信告诉你!
【解决方案3】:

在 npm 版本 6.9.0、节点版本 12.2.0 和 nodemailer 6.1.1 中存在相同的问题... 碰巧也使用电子邮件的功能测试在升级到节点 12.2.0 时开始失败。注意:这些测试一直运行良好,直到新版本的 Node...

这是我们收到的错误(从 mocha 测试结果中提取):

Uncaught AssertionError: expected Object {
  library: 'SSL routines',
  function: 'ssl_choose_client_version',
  reason: 'unsupported protocol',
  code: 'ESOCKET',
  command: 'CONN'
} to be ''

这是我们的传输配置:

const transporter = nodemailer.createTransport({
  host: 'email.ourhost.com',
  port: 25,
  secure: false,
  auth: {
    user: 'test',
    pass: 'pass'
  },
        tls: {
          rejectUnauthorized: false
        }
      });

除“降级”节点作为解决方案之外的任何有用建议将不胜感激!

【讨论】:

  • 是的,我们希望一些nodemailer的大师能看到这个:)
【解决方案4】:

您可以使用选项--tls-min-v1.0 启动node>=12。 它对我有用。

【讨论】:

  • 对不起,但这对我不起作用。你的tls 对象配置是什么?
  • 根本没有 tls 选项。只需托管、端口、安全和身份验证。与 aruba 意大利供应商合作良好。
  • 好的,我明白了,但对我来说,正如我之前所说,它不起作用,包括没有tls :(
【解决方案5】:

我无法使用命令行参数--tls-min-v1 更改此设置。另一种选择是在运行时为所有请求设置最低 TLS 版本。这可以通过将tls.DEFAULT_MIN_VERSION 设置为TLSv1 之类的其他值来完成。我只是在我的所有 require 声明之后添加了这个:

tls.DEFAULT_MIN_VERSION = 'TLSv1';

node.js 文档提供了有关此参数的有效选项的更多信息。 https://nodejs.org/docs/latest/api/tls.html#tls_tls_default_min_version

【讨论】:

  • 另外,记住你也必须先导入 tls...所以const tls = require('tls'); require('tls').DEFAULT_MIN_VERSOIN = 'TLSv1';
【解决方案6】:
var transporter = nodemailer.createTransport({
    host: "your host address",
    secure: false,
    port: 25,
    auth: {
      user: "your username",
      pass: "your password",
    },
    tls: {
      rejectUnauthorized: false,
      minVersion: 'TLSv1'
    },
  });

【讨论】:

  • 您好,感谢您的回答,但您需要在代码中提供一些解释。也许其他人正在尝试这个,并且需要它。
【解决方案7】:

对于Firebase/Cloud Functions for Firebase 环境中的Node.js 12,似乎是nodemailer 版本6.4.0 及更高版本上的问题。

代码如下:

const mailTransport = nodemailer.createTransport({
  // Make sure the environmental variables have proper typings.
  host: 'MYHOST',
  port: PORT_NUMBER,
  auth: {
    user: 'MY_USER@gmail.com',
    pass: 'MY_PASSWORD',
  },
});
...
exports.myFunction = functions.firestore
  .document(
    'mails/{somethingHere}'
  )
  .onCreate(async (snap: any, context: any) => {
    if (snap.data() === null) return null;
  ...
  return mailTransport
      .sendMail(mailOptions)
      .then((info: string) => {
        console.log('Info: ', info);
      })
      .catch((error: string) => {
        return console.log('Error: ', error); // This log will be shown in Firebase Functions logs.
      });
    ...

环境:

"firebase": "^8.1.1",
"firebase-admin": "^9.4.1",
"firebase-functions": "^3.11.0",
"nodemailer": "^6.4.11",

注意:我已将 GMAIL 用于我的 SMTP 设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2021-05-24
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多