Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat(email): support implicit TLS (SMTPS/465) for SMTP relay (MUL-2768) (#3340)
fengchangguo-star2cf8107fc8b6
diff --git a/.env.example b/.env.exampleindex ec178830f6..25ae5a2e0b 100644--- a/.env.example+++ b/.env.example@@ -89,11 +89,18 @@ [email protected]# Takes priority over Resend when SMTP_HOST is set.# Supports unauthenticated relay (leave SMTP_USERNAME empty) and authenticated SMTP.# Set SMTP_TLS_INSECURE=true only for private CA or self-signed certificates.+# SMTP_TLS controls the TLS mode:+# - unset / "starttls" (default): plaintext connect, upgrade via STARTTLS.+# - "implicit" (aliases: "smtps", "ssl"): TLS handshake on connect (SMTPS).+# Required by providers that only offer port 465 and do not advertise+# STARTTLS (e.g. Aliyun enterprise mail). Auto-enabled when SMTP_PORT=465+# and SMTP_TLS is unset.SMTP_HOST=SMTP_PORT=25SMTP_USERNAME=SMTP_PASSWORD=SMTP_TLS_INSECURE=false+SMTP_TLS=# Google OAuth# The web login page reads GOOGLE_CLIENT_ID from /api/config at runtime, sodiff --git a/SELF_HOSTING_ADVANCED.md b/SELF_HOSTING_ADVANCED.mdindex 473797ad60..d50cba36be 100644--- a/SELF_HOSTING_ADVANCED.md+++ b/SELF_HOSTING_ADVANCED.md@@ -44,9 +44,10 @@ Use this option when your deployment cannot reach the public internet or you alr| `SMTP_PORT` | SMTP port | `25` || `SMTP_USERNAME` | SMTP username (leave empty for unauthenticated relay) | - || `SMTP_PASSWORD` | SMTP password | - |+| `SMTP_TLS` | TLS mode. `implicit` (aliases `smtps`, `ssl`) forces SMTPS on connect; port `465` auto-enables it. Unset / `starttls` upgrades via STARTTLS | `starttls` || `SMTP_TLS_INSECURE` | Set `true` to skip TLS certificate verification (self-signed / private CA certs) | `false` |-STARTTLS is used automatically when advertised by the server. Port 465 (SMTPS / implicit TLS) is not currently supported - use ports 25 or 587 with STARTTLS.+STARTTLS is used automatically when advertised by the server. Port 465 (SMTPS / implicit TLS) is supported and auto-enables implicit TLS; set `SMTP_TLS=implicit` (aliases `smtps`, `ssl`) to force it on a non-standard port.> **Note:** If neither Resend nor SMTP is configured, generated verification codes are printed to backend logs — copy them from there to log in. A fixed local testing code (e.g. `888888`) is **opt-in only**: set `MULTICA_DEV_VERIFICATION_CODE=888888` in `.env` and keep `APP_ENV` non-production. The Docker self-host stack pins `APP_ENV=production`, so the shortcut is ignored there. **Never enable a fixed code on a publicly reachable instance.**diff --git a/apps/docs/content/docs/auth-setup.mdx b/apps/docs/content/docs/auth-setup.mdxindex bea151dabc..020b047966 100644--- a/apps/docs/content/docs/auth-setup.mdx+++ b/apps/docs/content/docs/auth-setup.mdx@@ -37,7 +37,7 @@ The SMTP path supports the three relay modes most on-premise mail servers (notab|---|---|---|---|| Anonymous internal relay | `25` | none — submission is trusted by IP / subnet | none on the wire (internal segment only) || Authenticated submission | `587` | `SMTP_USERNAME` + `SMTP_PASSWORD` | STARTTLS, upgraded automatically |-| Implicit TLS (SMTPS) | `465` | — | **not supported yet** — use port 25 or 587 |+| Implicit TLS (SMTPS) | `465` | optional (`SMTP_USERNAME` + `SMTP_PASSWORD`) | TLS handshake on connect — auto-enabled on port `465`, or force on a non-standard port with `SMTP_TLS=implicit` |**Anonymous Exchange relay on port 25** — the typical "internal SMTP relay" / Exchange anonymous receive connector that accepts mail from a trusted subnet without credentials:@@ -61,7 +61,18 @@ SMTP_TLS_INSECURE=false # set true only for self-signed / private CA```-At startup the server prints which provider it picked — for example `EmailService: SMTP relay exchange.internal.example.com:25 [email protected]` (or `Resend API` / `DEV mode`). The password is never logged. If you don't see the SMTP line after restart, `SMTP_HOST` didn't reach the process — check the container env (`docker compose -f docker-compose.selfhost.yml exec backend env | grep SMTP`).+**Implicit TLS (SMTPS) on port 465** — for providers that only offer SMTPS and don't advertise STARTTLS (e.g. Aliyun / Tencent enterprise mail). Port `465` auto-enables implicit TLS; `SMTP_TLS=implicit` (aliases: `smtps`, `ssl`) forces it on a non-standard SMTPS port:++```bash+SMTP_HOST=smtp.qiye.aliyun.com+SMTP_PORT=465 # implicit TLS auto-enabled on 465+SMTP_PASSWORD=...+SMTP_TLS=implicit # optional on 465; required on a non-standard SMTPS port+```++At startup the server prints which provider it picked, including the negotiated TLS mode — for example `EmailService: SMTP relay exchange.internal.example.com:25 (starttls) [email protected]` or `… smtp.qiye.aliyun.com:465 (implicit-tls) from=…` (or `Resend API` / `DEV mode`). The password is never logged. If you don't see the SMTP line after restart, `SMTP_HOST` didn't reach the process — check the container env (`docker compose -f docker-compose.selfhost.yml exec backend env | grep SMTP`).**What happens if you set neither**: the server doesn't error, but **every email that should have been sent is written to the server's stdout only**. Handy for local development (copy the code from the logs); in production it's a black hole.diff --git a/apps/docs/content/docs/auth-setup.zh.mdx b/apps/docs/content/docs/auth-setup.zh.mdxindex 0a5598c12f..79c157e6e7 100644--- a/apps/docs/content/docs/auth-setup.zh.mdx+++ b/apps/docs/content/docs/auth-setup.zh.mdx@@ -37,7 +37,7 @@ SMTP 路径覆盖大多数本地邮件服务器(特别是 Microsoft Exchange|---|---|---|---|| 匿名内部 relay | `25` | 无 —— 按 IP / 子网信任 | 链路上无 TLS(仅限内网段) || 认证提交(submission) | `587` | `SMTP_USERNAME` + `SMTP_PASSWORD` | STARTTLS,自动升级 |-| 隐式 TLS(SMTPS) | `465` | —— | **暂不支持** —— 请用 25 或 587 |+| 隐式 TLS(SMTPS) | `465` | 可选(`SMTP_USERNAME` + `SMTP_PASSWORD`) | 连上即 TLS 握手 —— 端口 `465` 自动启用;非标准端口设 `SMTP_TLS=implicit` 强制开启 |**匿名 Exchange relay,端口 25** —— 经典的 "internal SMTP relay" / Exchange 匿名 receive connector,按可信子网放行,不要求凭据:@@ -61,7 +61,18 @@ SMTP_TLS_INSECURE=false # 仅在私有 CA / 自签证书时改成 true```-启动时 server 会打印当前选择的 provider,比如 `EmailService: SMTP relay exchange.internal.example.com:25 [email protected]`(或 `Resend API` / `DEV mode`),密码不会出现在日志里。重启后没看到 SMTP 这行,说明 `SMTP_HOST` 没进到进程,确认下容器环境(`docker compose -f docker-compose.selfhost.yml exec backend env | grep SMTP`)。+**隐式 TLS(SMTPS),端口 465** —— 适用于只提供 SMTPS、不广告 STARTTLS 的服务商(例如阿里云 / 腾讯企业邮箱)。端口 `465` 会自动启用隐式 TLS;非标准 SMTPS 端口设置 `SMTP_TLS=implicit`(别名:`smtps`、`ssl`)即可强制开启:++```bash+SMTP_HOST=smtp.qiye.aliyun.com+SMTP_PORT=465 # 465 自动启用隐式 TLS+SMTP_PASSWORD=...+SMTP_TLS=implicit # 465 上可省略;在非标准 SMTPS 端口上必填+```++启动时 server 会打印当前选择的 provider 和协商出的 TLS 模式,比如 `EmailService: SMTP relay exchange.internal.example.com:25 (starttls) [email protected]` 或 `… smtp.qiye.aliyun.com:465 (implicit-tls) from=…`(或 `Resend API` / `DEV mode`),密码不会出现在日志里。重启后没看到 SMTP 这行,说明 `SMTP_HOST` 没进到进程,确认下容器环境(`docker compose -f docker-compose.selfhost.yml exec backend env | grep SMTP`)。**两种都不配**:server 不报错,但所有本该发出去的邮件**只打到 server 的 stdout**。本地开发方便(你从日志抄验证码),生产环境等于黑洞。diff --git a/apps/docs/content/docs/environment-variables.mdx b/apps/docs/content/docs/environment-variables.mdxindex 882df9c9b2..6af9bb78ac 100644--- a/apps/docs/content/docs/environment-variables.mdx+++ b/apps/docs/content/docs/environment-variables.mdx@@ -49,9 +49,10 @@ Multica supports two delivery backends — [Resend](https://resend.com/) for clo| Variable | Default | Description ||---|---|---|| `SMTP_HOST` | empty | SMTP relay hostname. Setting this activates SMTP mode and overrides Resend |-| `SMTP_PORT` | `25` | SMTP port. Use `587` for STARTTLS submission; **port 465 (SMTPS / implicit TLS) is not supported** |+| `SMTP_PORT` | `25` | SMTP port. Use `587` for STARTTLS submission, or `465` for SMTPS (implicit TLS, auto-enabled) || `SMTP_USERNAME` | empty | SMTP username. Leave empty for unauthenticated relay || `SMTP_PASSWORD` | empty | SMTP password |+| `SMTP_TLS` | `starttls` | TLS mode. `implicit` (aliases `smtps`, `ssl`) forces an immediate TLS handshake on connect (SMTPS); port `465` auto-enables it. Unset / `starttls` upgrades via STARTTLS after connect || `SMTP_TLS_INSECURE` | `false` | Set `true` to skip TLS certificate verification (private CA / self-signed only) |STARTTLS is upgraded automatically when the server advertises it. The dial timeout is 10s and the whole SMTP session has a 30s deadline, so a black-holed relay can't hang the auth handler.diff --git a/apps/docs/content/docs/environment-variables.zh.mdx b/apps/docs/content/docs/environment-variables.zh.mdxindex 6bebec89a0..e576fe5d0f 100644--- a/apps/docs/content/docs/environment-variables.zh.mdx+++ b/apps/docs/content/docs/environment-variables.zh.mdx@@ -49,9 +49,10 @@ Multica 支持两种邮件发送通道——[Resend](https://resend.com/) 适合| 环境变量 | 默认值 | 说明 ||---|---|---|| `SMTP_HOST` | 空 | SMTP relay 主机名。设置后即启用 SMTP 模式并覆盖 Resend |-| `SMTP_PORT` | `25` | SMTP 端口。STARTTLS 提交端口用 `587`;**暂不支持 465(SMTPS / 隐式 TLS)** |+| `SMTP_PORT` | `25` | SMTP 端口。STARTTLS 提交端口用 `587`;SMTPS(隐式 TLS,自动启用)用 `465` || `SMTP_USERNAME` | 空 | SMTP 用户名。留空表示未认证 relay || `SMTP_PASSWORD` | 空 | SMTP 密码 |+| `SMTP_TLS` | `starttls` | TLS 模式。`implicit`(别名 `smtps`、`ssl`)在连接时立即进行 TLS 握手(SMTPS);`465` 端口会自动启用。未设置 / `starttls` 则在连接后通过 STARTTLS 升级 || `SMTP_TLS_INSECURE` | `false` | 设为 `true` 跳过 TLS 证书校验(仅限私有 CA / 自签证书)|服务端 advertise STARTTLS 时会自动升级。dial 超时 10s,整个 SMTP 会话有 30s deadline,避免 relay 黑洞把 auth handler 挂死。diff --git a/apps/docs/content/docs/self-host-quickstart.mdx b/apps/docs/content/docs/self-host-quickstart.mdxindex 9af35eac7f..69b5275bde 100644--- a/apps/docs/content/docs/self-host-quickstart.mdx+++ b/apps/docs/content/docs/self-host-quickstart.mdx@@ -82,7 +82,7 @@ Two delivery backends are supported — pick whichever fits your network:**Option B — SMTP relay (internal networks / on-premise):**-Use this when the deployment can't reach `api.resend.com`, or you already have an internal mail relay (Microsoft Exchange, Postfix, on-prem SendGrid, etc.). `SMTP_HOST` takes priority over Resend when both are set, so verification and invite mail stays on the internal relay. Port 465 (SMTPS / implicit TLS) is not currently supported — use 25 or 587.+Use this when the deployment can't reach `api.resend.com`, or you already have an internal mail relay (Microsoft Exchange, Postfix, on-prem SendGrid, etc.). `SMTP_HOST` takes priority over Resend when both are set, so verification and invite mail stays on the internal relay. STARTTLS is upgraded automatically when advertised; port `465` (SMTPS / implicit TLS) auto-enables an immediate TLS handshake, and `SMTP_TLS=implicit` (aliases: `smtps`, `ssl`) forces it on a non-standard SMTPS port.For **anonymous Exchange internal relay (port 25)** — the host is trusted by IP and submits without credentials:@@ -106,7 +106,18 @@ SMTP_TLS_INSECURE=false # set true only for private CA / self-signed```-Then restart: `docker compose -f docker-compose.selfhost.yml restart backend`. On restart, the backend prints which provider it picked (`EmailService: SMTP relay …` / `Resend API` / `DEV mode`) — credentials are never logged, so this line is safe to share when asking for help.+For **implicit TLS / SMTPS (port 465)** — providers like Aliyun / Tencent enterprise mail that don't advertise STARTTLS. Port `465` auto-enables implicit TLS, so `SMTP_TLS` is optional here:++```bash+SMTP_HOST=smtp.qiye.aliyun.com+SMTP_PORT=465+SMTP_PASSWORD=...+SMTP_TLS=implicit # optional on 465; required on a non-standard SMTPS port+```++Then restart: `docker compose -f docker-compose.selfhost.yml restart backend`. On restart, the backend prints which provider it picked and the negotiated TLS mode (`EmailService: SMTP relay <host>:<port> (starttls|implicit-tls) from=…` / `Resend API` / `DEV mode`) — credentials are never logged, so this line is safe to share when asking for help.For more auth configuration (OAuth, signup allowlist) and the full SMTP variable reference, see [Auth setup](/auth-setup) and [Environment variables → Email](/environment-variables#email-configuration).diff --git a/apps/docs/content/docs/self-host-quickstart.zh.mdx b/apps/docs/content/docs/self-host-quickstart.zh.mdxindex cfad51ec5e..6976389058 100644--- a/apps/docs/content/docs/self-host-quickstart.zh.mdx+++ b/apps/docs/content/docs/self-host-quickstart.zh.mdx@@ -81,7 +81,7 @@ make selfhost**Option B — SMTP relay(内网/自部署):**-适合内网无法访问 `api.resend.com`,或已经有内部邮件中继(Microsoft Exchange、Postfix、自部署 SendGrid 等)的场景。同时设置时 `SMTP_HOST` 优先级高于 Resend,验证码和邀请邮件不会走外部 provider。**暂不支持** 465(SMTPS / 隐式 TLS),请使用 25 或 587。+适合内网无法访问 `api.resend.com`,或已经有内部邮件中继(Microsoft Exchange、Postfix、自部署 SendGrid 等)的场景。同时设置时 `SMTP_HOST` 优先级高于 Resend,验证码和邀请邮件不会走外部 provider。服务端 advertise STARTTLS 时会自动升级;端口 `465`(SMTPS / 隐式 TLS)会自动启用连接即握手的 TLS 模式,非标准 SMTPS 端口用 `SMTP_TLS=implicit`(别名:`smtps`、`ssl`)强制开启。**匿名 Exchange 内部 relay(端口 25)** —— 主机按 IP 被信任,不需要凭据:@@ -105,7 +105,18 @@ SMTP_TLS_INSECURE=false # 仅在私有 CA / 自签证书时改成 true```-之后重启:`docker compose -f docker-compose.selfhost.yml restart backend`。重启时 backend 会打印当前选择的 provider(`EmailService: SMTP relay …` / `Resend API` / `DEV mode`),密码不会被记录,所以这行截图给同事是安全的。+**隐式 TLS / SMTPS(端口 465)** —— 适用于阿里云 / 腾讯企业邮箱等不广告 STARTTLS 的服务商。端口 `465` 自动启用隐式 TLS,因此这里 `SMTP_TLS` 可省略:++```bash+SMTP_HOST=smtp.qiye.aliyun.com+SMTP_PORT=465+SMTP_PASSWORD=...+SMTP_TLS=implicit # 465 上可省略;非标准 SMTPS 端口上必填+```++之后重启:`docker compose -f docker-compose.selfhost.yml restart backend`。重启时 backend 会打印当前选择的 provider 和协商出的 TLS 模式(`EmailService: SMTP relay <host>:<port> (starttls|implicit-tls) from=…` / `Resend API` / `DEV mode`),密码不会被记录,所以这行截图给同事是安全的。更多 auth 配置(OAuth、注册白名单)以及完整的 SMTP 变量说明见 [登录与注册配置](/auth-setup) 和 [环境变量](/environment-variables)。diff --git a/docker-compose.selfhost.yml b/docker-compose.selfhost.ymlindex 27d2f38f26..4f7ca80881 100644--- a/docker-compose.selfhost.yml+++ b/docker-compose.selfhost.yml@@ -56,6 +56,7 @@ services:SMTP_PORT: ${SMTP_PORT:-25}SMTP_USERNAME: ${SMTP_USERNAME:-}SMTP_PASSWORD: ${SMTP_PASSWORD:-}+ SMTP_TLS: ${SMTP_TLS:-}SMTP_TLS_INSECURE: ${SMTP_TLS_INSECURE:-false}GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-}GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET:-}diff --git a/server/internal/service/email.go b/server/internal/service/email.goindex 1ba6a89ce6..d7270d17d3 100644--- a/server/internal/service/email.go+++ b/server/internal/service/email.go@@ -30,6 +30,7 @@ type EmailService struct {smtpUsername stringsmtpPassword stringsmtpTLSInsecure bool+ smtpTLSImplicit bool}func NewEmailService() *EmailService {@@ -48,6 +49,19 @@ func NewEmailService() *EmailService {smtpPassword := os.Getenv("SMTP_PASSWORD")smtpTLSInsecure := os.Getenv("SMTP_TLS_INSECURE") == "true"+ // SMTP_TLS=implicit forces an immediate TLS handshake on connect (SMTPS).+ // Required by providers like Aliyun enterprise mail that only offer port 465+ // SSL and do not advertise STARTTLS. Default (empty / "starttls") preserves+ // the prior STARTTLS-upgrade behavior.+ smtpTLSMode := strings.ToLower(strings.TrimSpace(os.Getenv("SMTP_TLS")))+ smtpTLSImplicit := smtpTLSMode == "implicit" || smtpTLSMode == "smtps" || smtpTLSMode == "ssl"+ if smtpTLSMode == "" && smtpPort == "465" {+ smtpTLSImplicit = true+ }+ if smtpTLSMode != "" && !smtpTLSImplicit && smtpTLSMode != "starttls" {+ fmt.Printf("EmailService: SMTP_TLS=%q not recognized, falling back to starttls\n", smtpTLSMode)+ }+var client *resend.Clientif apiKey != "" {client = resend.NewClient(apiKey)@@ -55,7 +69,11 @@ func NewEmailService() *EmailService {switch {case smtpHost != "":- fmt.Printf("EmailService: SMTP relay %s:%s from=%s\n", smtpHost, smtpPort, from)+ tlsLabel := "starttls"+ if smtpTLSImplicit {+ tlsLabel = "implicit-tls"+ }+ fmt.Printf("EmailService: SMTP relay %s:%s (%s) from=%s\n", smtpHost, smtpPort, tlsLabel, from)case client != nil:fmt.Printf("EmailService: Resend API from=%s\n", from)default:@@ -70,6 +88,7 @@ func NewEmailService() *EmailService {smtpUsername: smtpUsername,smtpPassword: smtpPassword,smtpTLSInsecure: smtpTLSInsecure,+ smtpTLSImplicit: smtpTLSImplicit,}}@@ -80,9 +99,21 @@ func NewEmailService() *EmailService {func (s *EmailService) sendSMTP(to, subject, htmlBody string) error {addr := net.JoinHostPort(s.smtpHost, s.smtpPort)+ tlsCfg := &tls.Config{+ ServerName: s.smtpHost,+ InsecureSkipVerify: s.smtpTLSInsecure, //nolint:gosec // opt-in via SMTP_TLS_INSECURE=true+ }+// Bounded dial + whole-session deadline: prevents a blackholed SMTP server// from hanging the auth handler (or a background goroutine) indefinitely.- conn, err := net.DialTimeout("tcp", addr, 10*time.Second)+ var conn net.Conn+ var err error+ if s.smtpTLSImplicit {+ dialer := &net.Dialer{Timeout: 10 * time.Second}+ conn, err = tls.DialWithDialer(dialer, "tcp", addr, tlsCfg)+ } else {+ conn, err = net.DialTimeout("tcp", addr, 10*time.Second)+ }if err != nil {return fmt.Errorf("smtp dial %s: %w", addr, err)}@@ -98,14 +129,13 @@ func (s *EmailService) sendSMTP(to, subject, htmlBody string) error {}defer c.Close()- // STARTTLS if advertised — refreshes the extension list for 8BITMIME check below.- if ok, _ := c.Extension("STARTTLS"); ok {- tlsCfg := &tls.Config{- ServerName: s.smtpHost,- InsecureSkipVerify: s.smtpTLSInsecure, //nolint:gosec // opt-in via SMTP_TLS_INSECURE=true- }- if err = c.StartTLS(tlsCfg); err != nil {- return fmt.Errorf("smtp starttls: %w", err)+ // STARTTLS upgrade only makes sense when the underlying connection is still+ // plaintext. Skip when we already dialed with implicit TLS.+ if !s.smtpTLSImplicit {+ if ok, _ := c.Extension("STARTTLS"); ok {+ if err = c.StartTLS(tlsCfg); err != nil {+ return fmt.Errorf("smtp starttls: %w", err)+ }}}diff --git a/server/internal/service/email_test.go b/server/internal/service/email_test.goindex 529b93b889..2e68a58490 100644--- a/server/internal/service/email_test.go+++ b/server/internal/service/email_test.go@@ -35,6 +35,41 @@ func TestSanitizeSubjectField(t *testing.T) {}}+func TestNewEmailService_TLSMode(t *testing.T) {+ tests := []struct {+ name string+ smtpTLS string+ smtpPort string+ wantImplicit bool+ }{+ {"unset on 465 auto-enables implicit", "", "465", true},+ {"unset on 587 stays starttls", "", "587", false},+ {"unset default port stays starttls", "", "", false},+ {"explicit implicit on 587 forces SMTPS", "implicit", "587", true},+ {"smtps alias", "smtps", "587", true},+ {"ssl alias", "ssl", "587", true},+ {"explicit starttls on 465 overrides auto-detect", "starttls", "465", false},+ {"case-insensitive", "IMPLICIT", "587", true},+ {"trims whitespace", " implicit ", "587", true},+ {"unknown value falls back to starttls", "tls", "465", false},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Isolate from any ambient mail config so only SMTP_TLS/SMTP_PORT drive the result.+ t.Setenv("RESEND_API_KEY", "")+ t.Setenv("SMTP_HOST", "smtp.example.com")+ t.Setenv("SMTP_PORT", tt.smtpPort)+ t.Setenv("SMTP_TLS", tt.smtpTLS)++ s := NewEmailService()+ if s.smtpTLSImplicit != tt.wantImplicit {+ t.Errorf("SMTP_TLS=%q SMTP_PORT=%q: smtpTLSImplicit = %v, want %v",+ tt.smtpTLS, tt.smtpPort, s.smtpTLSImplicit, tt.wantImplicit)+ }+ })+ }+}+func TestBuildInvitationParams_EscapesHTMLInBody(t *testing.T) {tests := []struct {name string