排查新Discourse安装中的电子邮件问题

You just installed Discourse via the install guide, but email doesn’t seem to work. Unfortunately this means you can’t log in as an admin to finalize the install. :cry: Let’s troubleshootize!

Try the doctor :woman_health_worker:

If you run ./discourse-doctor it will check several ways that your mail configuration might be broken, and offer advice. Try that first.

Did you enter email settings correctly?

The simplest way is to run ./discourse-setup again. Did you enter everything correctly? But wait! If your password has anything other than numbers and letters, you might be better off editing your app.yml with nano or your favorite editor.

You can also double check the settings in your containers/app.yml file. A valid email section looks like this:

DISCOURSE_DEVELOPER_EMAILS: 'name@example.com'
DISCOURSE_SMTP_ADDRESS: smtp.mailgun.org
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: postmaster@discourse.example.com
DISCOURSE_SMTP_PASSWORD: aUd34cdWKCu6CTjfoH7ykk

Closely examine all values for correctness. Note that:

  • it all aligns
  • no leading # characters
  • single quotes around the developer email field
  • password does not include ", ', %, ] or other special characters

If you had any errors in your app.yml and made changes, you MUST rebuild the container for those changes to take effect!

cd /var/discourse/
./launcher rebuild app

Well, you don’t always need to rebuild

Doing a rebuild will often fix things that seem broken, but it takes a while. There are times when a full rebuild is not necessary; the above is usually the best advice, but If you change just SMTP settings, you can do just this to apply them without doing a full rebuild:

cd /var/discourse
./launcher destroy app
./launcher start app

Are your SMTP connections being blocked?

To confirm that your server can indeed contact the email server, issue this command:

telnet smtp.mailgun.org 587

If you can’t connect this way, you’re almost certainly blocked. (And if you do get connected, the escape character for SMTP is ctrl+], then use quit to exit telnet.)

If this happens, first try port 2525, and if that fails, contact your cloud provider support and confirm that your email connections are not being blocked.

What do the Discourse logs say?

From the command line, issue this command:

cd /var/discourse
tail shared/standalone/log/rails/production.log

This will show the last few lines of the log. Look for anything mail related. If you need to view the fuller logs, try

less shared/standalone/log/rails/production.log

To page through the complete log, press space or type GG to jump to the end. Look closely for any email related messages or press /, type email, and hit enter to search.

What do your email provider logs say?

Assuming there are no errors in the Discourse logs, or your Discourse mail configuration, the emails probably went out. The question, is what did your email provider do with them?

Most email providers have a log viewing function. Check the logs for your email domain and see what happened with the incoming emails.

Did you properly set up DKIM and SPF records for your domain?

You must enter those crucial DNS records for DKIM and SPF, otherwise your emails may arrive only sporadically, if at all.

Is the email domain correct?

The default email from address is based on the install domain plus subdomain, so if your URL is discourse.example.com it will be:

noreply@discourse.example.com

But if your mail provider is expecting:

noreply@example.com

… you may have problems! To get around this, edit and uncomment this exec line in app.yml

## If you want to set the 'From' email address for your first registration, uncomment and change:
#- exec: rails r "SiteSetting.notification_email='noreply@example.com'"
## After getting the first signup email, re-comment the line. It only needs to run once.

You’ll need to issue a rebuild after uncommenting the above line and setting the from email address as required.

You can also change this from the command line, if needed:

./launcher enter app
rails r "SiteSetting.notification_email = 'discourse@yoursite.com'"
exit

If using Mailgun – have you activated your domain and provided credit card info?

If you are using Mailgun, after you enter your DKIM and SPF records, you must visit https://mailgun.com/app/domains/YOUR.DISCOURSE.DOMAIN.com and click the “Check DNS Records Now” button. At the top of that page you should see “State ACTIVE” (in a calming green). If it says “State Unverified” (in a scary warning-yellow) Mailgun will not accept mail.

Mailgun now requires a credit card in order to deliver mail (other than to you). If your mailgun logs have a message about “free accounts,” this is your problem.

Other mail services have similar requirements.

Are you using an IP address as the mail domain?

This does not work in our experience. You must use a domain name when sending email, not an IP address like 192.168.1.1.

If you really want to go on with an IP address, try mail settings similar to these:

DISCOURSE_SMTP_ADDRESS: 172.17.0.1         # e.g. use internal docker IP here
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: "YOUR-SMTP-USER-NAME"
DISCOURSE_SMTP_PASSWORD: "YOUR-SMTP-PASSWORD"
DISCOURSE_SMTP_ENABLE_START_TLS: true     # (optional, default true)
DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none
DISCOURSE_SMTP_DOMAIN: example.com

Need to log in without receiving a registration email?

We don’t recommend this, because your email is still broken, and you have a broken Discourse until email is working. But if you absolutely must log in as admin with email broken, here’s what to do:

cd /var/discourse
./launcher enter app
rake admin:create

And answer the prompts. It takes a few seconds before they appear. When it asks for the password, you will not be able to see what you type. That is why it makes you type it twice.

Email smtp port selection (Using 465?)

The ability to be able to AUTH using ‘telnet’ is extremely important in your first steps of email troubleshooting.

Port 465 (SMTP over SSL) is largely deprecated in favor of STARTTLS on 25. You may need to try alternate ports such as port 2525 or port 587 (Mail Submission) when things do not seem to work as expected.

Command Line SMTP tests for experienced sysadmins

If you’re comfortable with the command line, these might help diagnose network or certificate problems. If these do not seem “easy-to-follow” then you should please ignore this section.

See also Test SMTP Authentication and StartTLS - Sysadmins of the North.

Office 365 Tweaks

If you’re using Office 365, be sure to include these (the first line is what you are likely missing):

DISCOURSE_SMTP_AUTHENTICATION: login
DISCOURSE_SMTP_ENABLE_START_TLS: true
DISCOURSE_SMTP_PORT: 587

and set the correct value for DISCOURSE_SMTP_NOTIFICATION_EMAIL (which is likely different from your forum hostname).

TLS and SSL issues

By default, Discourse uses STARTTLS to encrypt its connection to the email server. Some email servers (increasingly rare nowadays) don’t support this or aren’t configured to use it, so it can be disabled by adding this line:

DISCOURSE_SMTP_ENABLE_START_TLS: false    #default: true

Other email servers might support STARTTLS, but use a self-signed certificate. This is uncommon and can be enabled with:

DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none #default: peer

Email still doesn’t work! What next?

Anything else I forgot here? Feel free to edit this.


Debug issues with first connection to smtp server from inside the Discourse container

1. Enter your container:

./launcher enter app

2. Check dns resolving for your smtp server name via getent hosts:

(dig, nslookup, ping etc. are not installed inside the container.)

getent hosts your.smtp.server

On success, it will look like this or will be blank on failure.

# IPv4
123.123.123.123 your.smtp.server

# IPv6
2001:db8:0:0:0:ff00:42:8329 your.smtp.server

3. Try to open a connection to your smtp server via openssl:

(telnet, nc etc. are not installed inside the container.)

Fiddle with some different settings until you succeed with a connection.

openssl s_client -connect your.smtp.server:465
openssl s_client -connect your.smtp.server:587 -starttls smtp

# IPv4
openssl s_client -connect 172.17.0.123:465
openssl s_client -connect 172.17.0.123:587 -starttls smtp

# IPv6
openssl s_client -6 -connect "[2001:db8:0:0:0:ff00:42:8329]:465"
openssl s_client -6 -connect "[2001:db8:0:0:0:ff00:42:8329]:587" -starttls smtp

See: How to check SMTP connection → Step 3: Checking SMTP Connection Over TLS Using Openssl

4. Use your found working connection settings with Discourse.

:rocket:

Bonus: show Discourse IP from inside docker container

( ifconfig , ip etc. are not installed inside the container.)

hostname -I

Result like:

172.17.0.2

Last edited by @grayphilo 2025-03-04T18:51:41Z

Check documentPerform check on document:
57 个赞

这是最新信息吗?对我来说不起作用;更改 SMTP 端口后,我不得不重建应用程序。

2 个赞

如果 discourse-doctor 提示连接到端口 587 失败,但 openssl s_client -connect your.smtp.server:587 -starttls smtp 工作正常,请尝试以下方法,两个命令的耗时应该相同:

time openssl s_client -starttls smtp -connect your.smtp.server:587 </dev/null > /dev/null

docker run --rm discourse/base:2.0.20231023-1945 bash -c 'time openssl s_client -starttls smtp -connect your.smtp.server:587 </dev/null' > /dev/null

如果 Docker 版本耗时远长于前者,则可能是您的 /etc/docker/daemon.json 文件配置错误。您可以尝试将 Google 的 DNS 服务器放在首位:

{
  "dns": ["8.8.8.8", "ww.xx.yy.zz", "ww.xx.yy.za"]
}

端口 2525 可用于 Mailjet。
587 端口失败。

2 个赞

我编辑了 OP 以建议使用端口 2525。是您的托管服务阻止了该端口。因此,许多邮件服务也支持 2525。

3 个赞

嘿,只是想就此添加一条注释;

Mailgun 现在要求提供信用卡才能发送邮件(除了发送给自己)。如果您的 Mailgun 日志中出现关于“免费账户”的消息,这就是您的问题。

我本周(2024 年 7 月)注册了,到目前为止,在不添加信用卡的情况下,使用基础免费套餐,它仍然有效。根据我在旧论坛帖子中看到的内容,似乎他们一直在摇摆不定地制定这项政策,以及他们的免费套餐使用情况和限制,也许吧。

1 个赞

哇。这太疯狂了,和我记忆中的情况完全不同。

人们很难弄清楚如何更改为按需付费计划,而不是注册一些相当昂贵的月度计划。

你是否发送给过除了你自己以外的用户?

1 个赞

是的,我已发送给用户,并且可以正常工作。唯一的问题是,出于某种原因,AOL 电子邮件地址正在阻止我的电子邮件,但我不认为这是 MailGun 的错。我和你一样惊讶 :slight_smile:

更新:似乎有些电子邮件被阻止的原因是,用于发送 MailGun 免费电子邮件的 IP 是共享的,因此它已被 AOL、Yahoo Mail 等一些电子邮件平台报告为“垃圾邮件”。似乎除了 Gmail 之外,所有人都看到了退回或被拒绝的电子邮件递送。

1 个赞

能否请您解释一下如何检查 containers/app.yml 文件中的设置?我们这些新手在没有明确指导的情况下不知道如何做这些事情。哈哈

如果你不知道如何使用像 nano 这样的工具,那么请再次运行 discourse-setup。在它保存更改后,你可以按 control-c 然后

./launcher destroy app;./launcher start app
1 个赞

好的,但我该如何检查我的 containers/app.yml 文件中的设置,以便查看电子邮件部分并验证数据是否正确?

如果你不喜欢我的答案,你可以谷歌搜索“nano”。

可以说,OP 应该说些关于 nano 的话,不过,正如我所说,如果你不知道它是什么,那么只需再次运行 discourse-setup 即可,因为它会读取文件中的值,而且你不会搞乱格式。

我现在明白了你的意思。当你运行 destroy 和 start 命令后,它会在完成后显示我需要的数据。抱歉!:slight_smile:

1 个赞

我运行了 doctor,并收到了一个名为 SMTPAuthenticationError 的错误。doctor 说这不是一个常见错误,他们也没有任何修复建议。如果发生这种情况,请务必仔细检查您的 SMTP 用户名和密码,因为 Discourse 设置过程不会告诉您是否错误,它只会不起作用(不发送电子邮件),让您束手无策。我做的一些有帮助的事情是使用 Ubuntu SSH 登录到我的服务器,而不是使用 LISH(因为我使用的是 Linode),因为 LISH 非常有错误且不支持复制粘贴。然后我重新进行了设置过程,这次复制粘贴了所有内容,而不是输入 100 个字符的密码,哈哈。总之,我希望这对其他新手有所帮助!

您的用户名或密码无效。

不确定我为什么没能解决这个问题,但这个错误本身很容易理解。

可能是您复制粘贴错误。也可能是其中包含需要转义的字符。

我使用 Brevo 作为我的通知电子邮件发送者,但由于出现错误,所有发送的通知都被拒绝了。我在 Brevo 中发现一条消息,指出“由于您使用的发件人无效,已拒绝发送。请验证您的发件人或对您的域名进行身份验证”。因此,我的论坛完全无法运行。我想知道如何解决这个问题——我需要什么样的发件人?非常感谢!!!

发件人地址可以设置为您用于发送邮件的邮件子域名,例如 mail@domain_address。

要对子域名 + 发件人进行身份验证,有几个步骤,他们在此处有一个指南:

嗨,Discourse 的朋友们!

我花了好几天时间来设置端口为 465 的电子邮件参数,但解决方案既不在论坛的帖子中,也不在我阅读过的任何地方(我真的仔细找过了)。

当然,这取决于您的邮件服务器接受什么。在我的例子中,只有 465 通过 TLS

app.yml 中需要添加的两个配置行是:

DISCOURSE_SMTP_FORCE_TLS: true
DISCOURSE_SMTP_ENABLE_START_TLS: false
一些细节

默认设置在使用 discourse-doctor 测试电子邮件时导致了 Net::ReadTimeout 错误。在容器内使用 curl 等工具发送测试电子邮件工作正常,正如这篇帖子中所述,这让我找到了解决方案的一半:Cannot send email - problem with port 465 - #10 by schungx

我只能在查看 app.yml 内容并修改此参数后才了解第二个设置。我感觉大多数程序(例如 Thunderbird)在选择端口 465 时会隐式设置正确的协议,那么 Discourse 应该这样做吗?这似乎非常标准,正如这里所强调的:

链接 到完整帖子)

因此,我强烈建议更新本指南中关于端口 465 的部分,或者让 discourse-setup 自动选择最佳设置。

2 个赞

我通常不评论,但这确实有帮助!
谢谢,Discourse 的维护者绝对应该将此设置包含在默认配置中,我的意思是设置他们的软件很费力,但我没有什么可批评的,除了在如此大的项目中,有些信息无法快速获得,需要有人“深入研究”。
好的,对我来说没问题!

帖子已合并到现有主题:在 discourse-setup 的 SMTP 密码中使用 %