From 7a62a78fadcd60d8fc8a2a7acfa9be1bffb3e7ba Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 9 Jun 2026 09:01:14 +0800 Subject: [PATCH] =?UTF-8?q?smtp=20=E5=8F=91=E4=BB=B6=E5=89=8D=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/apps/user/controllers.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/apps/user/controllers.go b/internal/apps/user/controllers.go index 18b92176..0d3287ec 100644 --- a/internal/apps/user/controllers.go +++ b/internal/apps/user/controllers.go @@ -20,6 +20,7 @@ import ( "context" "crypto/rand" "encoding/json" + "errors" "fmt" "math/big" "net/http" @@ -72,6 +73,23 @@ func isEmailRegisterVerificationEnabled() bool { return enabled } +func isSMTPConfigured(ctx context.Context) bool { + var sc model.SystemConfig + var host, port, username string + + if err := sc.GetByKey(ctx, model.ConfigKeySMTPHost); err == nil { + host = sc.Value + } + if err := sc.GetByKey(ctx, model.ConfigKeySMTPPort); err == nil { + port = sc.Value + } + if err := sc.GetByKey(ctx, model.ConfigKeySMTPUsername); err == nil { + username = sc.Value + } + + return host != "" && port != "" && username != "" +} + func generateVerificationCode() string { n, _ := rand.Int(rand.Reader, big.NewInt(900000)) return fmt.Sprintf("%06d", n.Int64()+100000) @@ -86,6 +104,11 @@ func getEmailCooldownKey(email string) string { } func sendEmailVerificationCode(ctx context.Context, email, scene, templateName string) error { + // 校验 SMTP 配置是否完整 + if !isSMTPConfigured(ctx) { + return errors.New(errSMTPConfigIncomplete) + } + code := generateVerificationCode() codeKey := getEmailCodeKey(scene, email) cooldownKey := getEmailCooldownKey(email)