一些背景信息:Emails have stopped sending - end of file reached
大约一周前(2021 年 1 月 13 日),通过 Google 的 smtp-relay.gmail.com 服务器发送邮件开始失败(这是 Google Apps 用户被允许且预期的用途)。
Sidekiq 报告了这些失败,错误为 EOFError:
Jobs::HandledExceptionWrapper: Wrapped EOFError: end of file reached
/logs 也报告了失败的任务:
Job exception: end of file reached
其他帖子中提供了完整的回溯信息。
===================
调查表明,最新版本的 Discourse 安装使用 ‘EHLO localhost’ 连接到 SMTP 中继服务器,而 Google 大约在一周前开始拒绝此类请求。
来自生产实例的 tcpdump 输出:
0x0030: d10f f8e4 4548 4c4f 206c 6f63 616c 686f ....EHLO.localho
0x0040: 7374 0d0a st..
...
0x0030: de62 f0c3 3432 3120 342e 372e 3020 5472 .b..421.4.7.0.Tr
0x0040: 7920 6167 6169 6e20 6c61 7465 722c 2063 y.again.later,.c
0x0050: 6c6f 7369 6e67 2063 6f6e 6e65 6374 696f losing.connectio
0x0060: 6e2e 2028 4548 4c4f 2920 6a31 3673 6d34 n..(EHLO).j16sm4
0x0070: 3831 3932 3976 736d 2e31 202d 2067 736d 81929vsm.1.-.gsm
0x0080: 7470 0d0a tp..
使用 telnet 复现也得到相同结果:
root@conversation:~# telnet smtp-relay.gmail.com 587
Trying 74.125.137.28...
Connected to smtp-relay.gmail.com.
Escape character is '^]'.
220 smtp-relay.gmail.com ESMTP ls8sm507258pjb.6 - gsmtp
ehlo localhost.localdomain
421 4.7.0 Try again later, closing connection. (EHLO) ls8sm507258pjb.6 - gsmtp
Connection closed by foreign host.
然而,使用特定域名的 ehlo 则正常工作:
root@conversation:~# telnet smtp-relay.gmail.com 587
Trying 74.125.137.28...
Connected to smtp-relay.gmail.com.
Escape character is '^]'.
220 smtp-relay.gmail.com ESMTP p10sm668563uaw.3 - gsmtp
ehlo conversation.sevarg.net
250-smtp-relay.gmail.com at your service, [64.227.96.27]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
======
根据日志,我确定了需要修改的文件以测试修复方案(在 Docker 镜像中):
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb
将
DEFAULTS = {
:address => 'localhost',
:port => 25,
:domain => 'localhost.localdomain',
修改为
DEFAULTS = {
:address => 'conversation.sevarg.net',
:port => 25,
:domain => 'conversation.sevarg.net',
问题得以解决(在重启实例后)。现在 EHLO 使用的是域名字符串,邮件也能从我的实例正常发送了。
================
期望行为:发送邮件时,默认的 Discourse 安装应使用配置的域名作为与 SMTP 服务器初始连接时的 EHLO 参数。或者,应提供一个配置选项以覆盖发送的域名。如果存在该选项,我通过搜索未能找到。