smtp 发件前验证配置

This commit is contained in:
ryan
2026-06-09 09:01:14 +08:00
parent 2ae55da52e
commit 7a62a78fad
+23
View File
@@ -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)