# SMTP错误：必须先发出STARTTLS命令

**URL:** https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050
**Category:** Self-hosting
**Created:** [2021年二月24日 21:16 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050 "2021-02-24T21:16:33Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![blsrds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blsrds/32/211062_2.png) [@blsrds](https://meta.discourse.org/u/blsrds)
#### Post date: [2021年二月24日 21:16 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/1 "2021-02-24T21:16:33Z")

</div>

我正在尝试将 Discourse 2.7.0.beta4 与 MailerSend SMTP 服务进行配置。

运行 `./discourse-doctor` 后，我收到了以下错误：

> SMTP 错误：必须先发出 STARTTLS 命令

这是我当前 **app.yml** 中关于 SMTP 的配置：

```
  DISCOURSE_SMTP_ADDRESS: 'smtp.mailersend.net'
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: username@subdomain.domain.org
  DISCOURSE_SMTP_PASSWORD: mypasswordhere
 #DISCOURSE_SMTP_ENABLE_START_TLS: true # (可选，默认为 true)
 #DISCOURSE_SMTP_AUTHENTICATION: login
 #DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none

```

我已经尝试过取消注释并显式将 `DISCOURSE_SMTP_ENABLE_START_TLS` 设置为 true，但错误依然存在。对 `DISCOURSE_SMTP_AUTHENTICATION: login` 也做了同样的尝试。

每次修改 YML 文件后，我都使用以下命令重启系统：

`./launcher destroy app; ./launcher start app`

关于可能出现的问题，有什么建议吗？

提前感谢！

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2021年二月24日 21:42 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/2 "2021-02-24T21:42:51Z")

</div>

我最近对 discourse-setup 进行了一些更改，同时也修改了您正在使用的 rake 任务（我认为该更改尚未合并）。

如果您愿意授予我访问您服务器的权限，我可以帮忙查看。

---

<div class="post-metadata">

### Author: ![blsrds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blsrds/32/211062_2.png) [@blsrds](https://meta.discourse.org/u/blsrds)
#### Post date: [2021年二月24日 23:00 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/3 "2021-02-24T23:00:59Z")

</div>

正如我通过私信提到的，出于安全考虑，我无法提供服务器访问权限。但非常感谢 @pfaffman 的帮助以及你为解决此问题所做的努力。

让我补充一下这个问题的更多背景：一位前任管理员曾安装过 Discourse 并配置了 Mailgun SMTP 服务，但该服务已停止工作，而我无法访问该账户。

如我所说，我现在正尝试使用 [Mailersend](http://mailersend.com/) 进行配置。我阅读了此论坛中的主题 [1] 以及其他关于 STARTTLS 的帖子，但我不确定如何实现所需的更改。

我也尝试了以下设置，但错误依然存在：

> DISCOURSE\_SMTP\_OPENSSL\_VERIFY\_MODE: none

如果这与最近的某些更新有关，那么是否考虑回退版本会更好呢？

[1] [Can't send email with certificate issue - #3 by supermathie](https://meta.discourse.org/t/cant-send-email-with-certificate-issue/113207/3)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2021年三月2日 04:33 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/4 "2021-03-02T04:33:04Z")

</div>

你搞定这个了吗？

---

<div class="post-metadata">

### Author: ![blsrds](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blsrds/32/211062_2.png) [@blsrds](https://meta.discourse.org/u/blsrds)
#### Post date: [2021年三月8日 13:13 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/5 "2021-03-08T13:13:47Z")

</div>

不，我已经决定更换为另一家邮件服务提供商。现在使用 [Mailjet](https://www.mailjet.com/) 一切正常。

---

<div class="post-metadata">

### Author: ![dbrgn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dbrgn/32/240704_2.png) [@dbrgn](https://meta.discourse.org/u/dbrgn)
#### Post date: [2021年十一月22日 08:25 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/6 "2021-11-22T08:25:01Z")

</div>

我遇到了类似的问题。昨天我花了大约三个小时调试新 Discourse 实例的电子邮件发送，但都没有成功。我正尝试通过 Fastmail 使用 STARTTLS 在端口 587 上发送电子邮件。其他服务使用相同的凭据可以正常工作。

使用这些设置时，我没有收到“必须先发出 STARTTLS 命令”的错误响应：

```
DISCOURSE_SMTP_ADDRESS: 'smtp.fastmail.com'
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: 'myuser@fastmail.fm'
DISCOURSE_SMTP_PASSWORD: 'mypass'
DISCOURSE_SMTP_ENABLE_START_TLS: true

```

…然后运行 `./launcher rebuild app`，当我运行 `./discourse-doctor` 并发送电子邮件时，我会收到 `500 5.5.1 Invalid command` 错误响应。

今天我开始使用 tcpdump 跟踪通信，并注意到 Discourse 似乎并没有真正使用 STARTTLS。以下是我发送 Grafana 恢复电子邮件时发生的情况：

```
< 220 smtp.fastmail.com ESMTP ready
> EHLO 9b5ba1569f77
< 250-smtp.fastmail.com
< 250-PIPELINING
< 250-SIZE 71000000
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 STARTTLS
> STARTTLS
< ...[encrypted]

```

但是使用 Discourse 时，会发生这种情况：

```
< 220 smtp.fastmail.com ESMTP ready
> EHLO localhost
< 250-smtp.fastmail.com
< 250-PIPELINING
< 250-SIZE 71000000
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 STARTTLS
> AUTH PLAIN [redacted]
< 500 5.5.1 Invalid command

```

所以，看起来 Discourse 正在明文传输我的凭据，即使在设置中启用了 STARTTLS？这是一个 bug 吗？

我还注意到，当我运行 `./discourse-doctor` 时，“YML SETTINGS”摘要顶部列出了以下项目：

```
==================== YML SETTINGS ====================
DISCOURSE_HOSTNAME=forum.[redacted]
SMTP_ADDRESS=smtp.fastmail.com
DEVELOPER_EMAILS=sysadmin@[redacted]
SMTP_PASSWORD=[redacted]
SMTP_PORT=587
SMTP_USER_NAME=[redacted]@fastmail.fm
LETSENCRYPT_ACCOUNT_EMAIL=

```

然而，尽管在 `app.yml` 中设置了 `DISCOURSE_SMTP_ENABLE_START_TLS`，但没有提及它。不确定这个问题是否相关。

---

<div class="post-metadata">

### Author: ![dbrgn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dbrgn/32/240704_2.png) [@dbrgn](https://meta.discourse.org/u/dbrgn)
#### Post date: [2021年十一月22日 08:32 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/7 "2021-11-22T08:32:02Z")

</div>

哦，这很奇怪。我手动创建了一个用户帐户（通过 `rake admin:create`）然后登录，突然电子邮件通知就可以正常工作了。但是，通过 `discourse-doctor` 发送仍然失败。

也许 `discourse-doctor` 坏了？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2021年十一月22日 09:37 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/8 "2021-11-22T09:37:18Z")

</div>

抱歉。我知道那有多令人沮丧。

> [@dbrgn](#):
>
> 也许 `discourse-doctor` 坏了？

有可能。它会做一些事情来尝试调试问题所在，所以可能是它使用的逻辑在你的情况下是错误的。

还有一个 rake 任务，对你来说可能更好。

```
    rake emails:test[x@y.com]

```

你是否遵循了 [新 Discourse 安装上的电子邮件故障排除](https://meta.discourse.org/t/troubleshooting-email-on-a-new-discourse-install/16326)？

---

<div class="post-metadata">

### Author: ![dbrgn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dbrgn/32/240704_2.png) [@dbrgn](https://meta.discourse.org/u/dbrgn)
#### Post date: [2021年十一月22日 10:12 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/9 "2021-11-22T10:12:46Z")

</div>

> [@pfaffman](#):
>
> 还有一个 rake 任务，可能对你来说是更好的选择。

我不知道有这个命令，它确实看起来很有用！但结果是一样的：

```plaintext
root@app:/var/www/discourse# rake emails:test redacted@example.com
Testing sending to using smtp.fastmail.com:587, username:myuser@fastmail.fm with plain auth.
======================================== ERROR ========================================
                                    UNEXPECTED ERROR
500 5.5.1 Invalid command

====================================== SOLUTION =======================================
This is not a common error. No recommended solution exists!

Please report the exact error message above to https://meta.discourse.org/
(And a solution, if you find one!)
=======================================================================================

```

当我查看 tcpdump 时，我再次看到它在没有 STARTTLS 加密的情况下以明文发送 `AUTH PLAIN` 凭据。

> [@pfaffman](#):
>
> 你是否遵循了 [Troubleshooting email on a new Discourse install](https://meta.discourse.org/t/troubleshooting-email-on-a-new-discourse-install/16326)？

是的，我浏览了那个页面。

然而，根据 `tcpdump` 的结果，这在我看来像是诊断工具中的一个 bug，因为即使在 `app.yml` 中启用了 STARTTLS 设置，它也没有被使用。（Discourse 应用程序本身确实使用了 STARTTLS。我假设许多电子邮件提供商也允许未加密的电子邮件提交，所以这个问题只会出现在那些使用诊断工具 _并且_ 使用了不允许通过 SMTP 进行未加密提交的提供商的人身上。）

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2021年十一月23日 17:49 UTC](https://meta.discourse.org/t/smtp-error-must-issue-a-starttls-command-first/181050/10 "2021-11-23T17:49:25Z")

</div>

啊。看起来那个 rake 任务和 discourse-doctor 调用的是同一个。抱歉。

也许有人可以看看如何让那个 rake 任务更像实际过程，或者至少在它试图弄清楚发生了什么时不要放弃。一个好的第一步是说“嗯，XXX 似乎坏了，但我们会尝试一下……”
