# Discourse-setup 在 SMTP 用户名中使用 / 时失败

**URL:** <https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514>\
**Category:** Bug\
**Tags:** pr-welcome\
**Created:** [2023年七月22日 16:23 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514 "2023-07-22T16:23:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年七月22日 16:23 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/1 "2023-07-22T16:23:44Z")

</div>

继续讨论 [将独立容器移至单独的 Web 和数据容器](https://meta.discourse.org/t/move-from-standalone-container-to-separate-web-and-data-containers/29413)：

## Bug 描述

运行 `discourse-setup` 时，如果 SMTP 用户名包含 `/`，脚本将失败：

> sed: -e 表达式 #1, 字符 74: 对于 ‘s’ 未知选项  
> DISCOURSE\_SMTP\_USER\_NAME 更改失败。

## 预期行为

应接受包含 `/` 的 SMTP 用户名。

根据 [RFC3696](https://datatracker.ietf.org/doc/html/rfc3696#section-3)，`/` 是 SMTP 用户名的有效字符：

> 未加引号时，本地部分可以由字母、数字或任何特殊字符组成
> 
> ! # $ % & ’ \* + - / = ? ^ \_ ` . { | } ~

## 修正路径

脚本在 `sed` 命令处失败，该命令默认使用 `/` 作为分隔符。  
由于冒号 (`:`) 不是电子邮件地址 `local-part` 的可接受字符，因此可以代替 `/` 使用它，而不会产生其他字符（如逗号或分号）可能带来的副作用（除了 `sed` 模式中的冒号必须转义 - 请注意，使用 `]` 可以消除转义冒号的需要）：

- 第 589 行：  
`smtp_domain=$(echo $notification_email | sed -e \"s/.*@//\")` 应改为  
`smtp_domain=$(echo $notification_email | sed -e \"s:.*@::\")`
- 第 648 行：`DISCOURSE_DEVELOPER_EMAILS` 使用 `,` 分隔电子邮件地址，因此也可以安全地使用 `:`
- 第 675 行是我的 Bug 来源：  
`sed -i -e \"s/^ #\\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog\" $web_file` 可改为  
`sed -i -e \"s:^ #\\?DISCOURSE_SMTP_USER_NAME\\:.*: DISCOURSE_SMTP_USER_NAME\\: $smtp_user_name:w $changelog\" $web_file` 或  
`sed -i -e \"s] #\\?DISCOURSE_SMTP_USER_NAME:.*] DISCOURSE_SMTP_USER_NAME: $smtp_user_name]w $changelog\" $web_file`
- 第 684 行（`DISCOURSE_NOTIFICATION_EMAIL`）同上

---

<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:** [2023年七月22日 20:21 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/2 "2023-07-22T20:21:55Z")

</div>

我无法控制那段代码，但我写它已经很久了。这是一个很好的解决方案。我应该想到的。我猜想一个 PR（拉取请求）会受到欢迎。

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年七月22日 20:38 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/3 "2023-07-22T20:38:49Z")

</div>

很遗憾，我无法访问我的 Github 帐户了。他们要求我提供一些代码，但我不再使用关联的电子邮件，因此我已停止使用该平台，只用于克隆公共代码。

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2023年七月24日 02:40 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/4 "2023-07-24T02:40:31Z")

</div>

感谢报告，添加 #pr-welcome 以便有人能够提交补丁。

你也可以在这里发布一个 .diff，然后让社区成员来获得功劳 🙂

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年七月24日 14:12 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/5 "2023-07-24T14:12:21Z")

</div>

这是一个补丁：

修复 sed 命令，以允许符合 RFC3696 的电子邮件地址

[discourse-setup.fix-272514.patch.gz](https://meta.discourse.org/uploads/short-url/rCrNhJTV1WisbJMDTGzXsGnh2Z8.gz) (601 字节)

```diff
diff --git a/discourse-setup b/discourse-setup
index 0daafcb..121982e 100755
--- a/discourse-setup
+++ b/discourse-setup
@@ -586,7 +586,7 @@ ask_user_for_config() {
     fi

     # set smtp_domain default value here rather than use Rails default of localhost
- smtp_domain=$(echo $notification_email | sed -e "s/.*@//")
+ smtp_domain=$(echo $notification_email | sed -e "s].*@]]")

     if [! -z $letsencrypt_account_email]
     then
@@ -645,7 +645,7 @@ ask_user_for_config() {
     update_ok="n"
   fi

- sed -i -e "s/^ DISCOURSE_DEVELOPER_EMAILS:.*/ DISCOURSE_DEVELOPER_EMAILS: \'$developer_emails\'/w $changelog" $web_file
+ sed -i -e "s]^ DISCOURSE_DEVELOPER_EMAILS:.*] DISCOURSE_DEVELOPER_EMAILS: \'$developer_emails\']w $changelog" $web_file
   if [-s $changelog]
   then
     rm $changelog
@@ -672,7 +672,7 @@ ask_user_for_config() {
     update_ok="n"
   fi

- sed -i -e "s/^ #\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog" $web_file
+ sed -i -e "s]^ #\?DISCOURSE_SMTP_USER_NAME:.*] DISCOURSE_SMTP_USER_NAME: $smtp_user_name]w $changelog" $web_file
   if [-s $changelog]
   then
     rm $changelog
@@ -681,7 +681,7 @@ ask_user_for_config() {
     update_ok="n"
   fi

- sed -i -e "s/^ #\?DISCOURSE_NOTIFICATION_EMAIL:.*/ DISCOURSE_NOTIFICATION_EMAIL: $notification_email/w $changelog" $web_file
+ sed -i -e "s]^ #\?DISCOURSE_NOTIFICATION_EMAIL:.*] DISCOURSE_NOTIFICATION_EMAIL: $notification_email]w $changelog" $web_file
   if [-s $changelog]
   then
     rm $changelog
@@ -745,7 +745,7 @@ ask_user_for_config() {
   then
     echo "Enabling Let's Encrypt"
   fi
- sed -i -e "s/^ #\?LETSENCRYPT_ACCOUNT_EMAIL:.*/ LETSENCRYPT_ACCOUNT_EMAIL: $letsencrypt_account_email/w $changelog" $web_file
+ sed -i -e "s]^ #\?LETSENCRYPT_ACCOUNT_EMAIL:.*] LETSENCRYPT_ACCOUNT_EMAIL: $letsencrypt_account_email]w $changelog" $web_file
   if [-s $changelog]
   then
     rm $changelog

```

---

<div class="post-metadata">

**Author:** ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)\
**Post date:** [2023年七月24日 16:29 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/6 "2023-07-24T16:29:29Z")

</div>

> [@hellekin](#):
>
> `$letsencrypt_account_email]/w`

您是想在那儿留下斜杠吗？

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年七月24日 16:49 UTC](https://meta.discourse.org/t/discourse-setup-fails-on-smtp-username-with-a/272514/7 "2023-07-24T16:49:52Z")

</div>

感谢您提出这个很好的建议！我已经更新了补丁。
