@@ -231,11 +231,27 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
231231 if req .DefaultBalance < 0 {
232232 req .DefaultBalance = 0
233233 }
234+ req .SMTPHost = strings .TrimSpace (req .SMTPHost )
235+ req .SMTPUsername = strings .TrimSpace (req .SMTPUsername )
236+ req .SMTPPassword = strings .TrimSpace (req .SMTPPassword )
237+ req .SMTPFrom = strings .TrimSpace (req .SMTPFrom )
238+ req .SMTPFromName = strings .TrimSpace (req .SMTPFromName )
234239 if req .SMTPPort <= 0 {
235240 req .SMTPPort = 587
236241 }
237242 req .DefaultSubscriptions = normalizeDefaultSubscriptions (req .DefaultSubscriptions )
238243
244+ // SMTP 配置保护:如果请求中 smtp_host 为空但数据库中已有配置,则保留已有 SMTP 配置
245+ // 防止前端加载设置失败时空表单覆盖已保存的 SMTP 配置
246+ if req .SMTPHost == "" && previousSettings .SMTPHost != "" {
247+ req .SMTPHost = previousSettings .SMTPHost
248+ req .SMTPPort = previousSettings .SMTPPort
249+ req .SMTPUsername = previousSettings .SMTPUsername
250+ req .SMTPFrom = previousSettings .SMTPFrom
251+ req .SMTPFromName = previousSettings .SMTPFromName
252+ req .SMTPUseTLS = previousSettings .SMTPUseTLS
253+ }
254+
239255 // Turnstile 参数验证
240256 if req .TurnstileEnabled {
241257 // 检查必填字段
@@ -828,7 +844,7 @@ func equalDefaultSubscriptions(a, b []service.DefaultSubscriptionSetting) bool {
828844
829845// TestSMTPRequest 测试SMTP连接请求
830846type TestSMTPRequest struct {
831- SMTPHost string `json:"smtp_host" binding:"required" `
847+ SMTPHost string `json:"smtp_host"`
832848 SMTPPort int `json:"smtp_port"`
833849 SMTPUsername string `json:"smtp_username"`
834850 SMTPPassword string `json:"smtp_password"`
@@ -844,18 +860,35 @@ func (h *SettingHandler) TestSMTPConnection(c *gin.Context) {
844860 return
845861 }
846862
847- if req .SMTPPort <= 0 {
848- req .SMTPPort = 587
863+ req .SMTPHost = strings .TrimSpace (req .SMTPHost )
864+ req .SMTPUsername = strings .TrimSpace (req .SMTPUsername )
865+
866+ var savedConfig * service.SMTPConfig
867+ if cfg , err := h .emailService .GetSMTPConfig (c .Request .Context ()); err == nil && cfg != nil {
868+ savedConfig = cfg
849869 }
850870
851- // 如果未提供密码,从数据库获取已保存的密码
852- password := req .SMTPPassword
853- if password == "" {
854- savedConfig , err := h .emailService .GetSMTPConfig (c .Request .Context ())
855- if err == nil && savedConfig != nil {
856- password = savedConfig .Password
871+ if req .SMTPHost == "" && savedConfig != nil {
872+ req .SMTPHost = savedConfig .Host
873+ }
874+ if req .SMTPPort <= 0 {
875+ if savedConfig != nil && savedConfig .Port > 0 {
876+ req .SMTPPort = savedConfig .Port
877+ } else {
878+ req .SMTPPort = 587
857879 }
858880 }
881+ if req .SMTPUsername == "" && savedConfig != nil {
882+ req .SMTPUsername = savedConfig .Username
883+ }
884+ password := strings .TrimSpace (req .SMTPPassword )
885+ if password == "" && savedConfig != nil {
886+ password = savedConfig .Password
887+ }
888+ if req .SMTPHost == "" {
889+ response .BadRequest (c , "SMTP host is required" )
890+ return
891+ }
859892
860893 config := & service.SMTPConfig {
861894 Host : req .SMTPHost ,
@@ -877,7 +910,7 @@ func (h *SettingHandler) TestSMTPConnection(c *gin.Context) {
877910// SendTestEmailRequest 发送测试邮件请求
878911type SendTestEmailRequest struct {
879912 Email string `json:"email" binding:"required,email"`
880- SMTPHost string `json:"smtp_host" binding:"required" `
913+ SMTPHost string `json:"smtp_host"`
881914 SMTPPort int `json:"smtp_port"`
882915 SMTPUsername string `json:"smtp_username"`
883916 SMTPPassword string `json:"smtp_password"`
@@ -895,18 +928,43 @@ func (h *SettingHandler) SendTestEmail(c *gin.Context) {
895928 return
896929 }
897930
898- if req .SMTPPort <= 0 {
899- req .SMTPPort = 587
931+ req .SMTPHost = strings .TrimSpace (req .SMTPHost )
932+ req .SMTPUsername = strings .TrimSpace (req .SMTPUsername )
933+ req .SMTPFrom = strings .TrimSpace (req .SMTPFrom )
934+ req .SMTPFromName = strings .TrimSpace (req .SMTPFromName )
935+
936+ var savedConfig * service.SMTPConfig
937+ if cfg , err := h .emailService .GetSMTPConfig (c .Request .Context ()); err == nil && cfg != nil {
938+ savedConfig = cfg
900939 }
901940
902- // 如果未提供密码,从数据库获取已保存的密码
903- password := req .SMTPPassword
904- if password == "" {
905- savedConfig , err := h .emailService .GetSMTPConfig (c .Request .Context ())
906- if err == nil && savedConfig != nil {
907- password = savedConfig .Password
941+ if req .SMTPHost == "" && savedConfig != nil {
942+ req .SMTPHost = savedConfig .Host
943+ }
944+ if req .SMTPPort <= 0 {
945+ if savedConfig != nil && savedConfig .Port > 0 {
946+ req .SMTPPort = savedConfig .Port
947+ } else {
948+ req .SMTPPort = 587
908949 }
909950 }
951+ if req .SMTPUsername == "" && savedConfig != nil {
952+ req .SMTPUsername = savedConfig .Username
953+ }
954+ password := strings .TrimSpace (req .SMTPPassword )
955+ if password == "" && savedConfig != nil {
956+ password = savedConfig .Password
957+ }
958+ if req .SMTPFrom == "" && savedConfig != nil {
959+ req .SMTPFrom = savedConfig .From
960+ }
961+ if req .SMTPFromName == "" && savedConfig != nil {
962+ req .SMTPFromName = savedConfig .FromName
963+ }
964+ if req .SMTPHost == "" {
965+ response .BadRequest (c , "SMTP host is required" )
966+ return
967+ }
910968
911969 config := & service.SMTPConfig {
912970 Host : req .SMTPHost ,
0 commit comments