保存外部邮箱验证码投递修复进度
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 02:03:18 +08:00
1 parent 2a3d3b420e
commit c79f1035af
7 files changed
+227 -26

No files matched your search

+13 -4
View File
@@ -22,9 +22,11 @@ const (
)
var (
ErrInvalidSMTPConfig = errors.New("invalid SMTP configuration")
ErrUnsupportedSMTP = errors.New("unsupported SMTP security mode")
ErrSMTPRelayUnavailable = errors.New("SMTP relay is unavailable")
ErrInvalidSMTPConfig = errors.New("invalid SMTP configuration")
ErrUnsupportedSMTP = errors.New("unsupported SMTP security mode")
ErrSMTPRelayUnavailable = errors.New("SMTP relay is unavailable")
ErrProjectMailUnavailable = errors.New("project mail delivery is unavailable")
ErrMailDeliveryFailed = errors.New("verification mail delivery failed")
)
type SMTPSettings struct {
@@ -178,6 +180,12 @@ func sendSMTPMessage(settings SMTPSettings, recipient string, message []byte) er
}
func buildCodeMessage(from, recipient string, code string, purpose Purpose) []byte {
title, text := CodeMessage(purpose, code)
return []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n%s\r\n", from, recipient, mime.QEncoding.Encode("UTF-8", title), text))
}
// CodeMessage 返回验证码邮件的统一标题和正文,供中继与项目自身投递复用。
func CodeMessage(purpose Purpose, code string) (string, string) {
title := "Smail 安全验证码"
purposeText := "完成本次安全验证"
switch purpose {
@@ -190,7 +198,8 @@ func buildCodeMessage(from, recipient string, code string, purpose Purpose) []by
case PurposeExternalEmailChange:
purposeText = "完成外部邮箱变更验证"
}
return []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n您好,您正在%s。验证码:%s\r\n验证码有效期为十分钟,请勿向他人泄露。\r\n", from, recipient, mime.QEncoding.Encode("UTF-8", title), purposeText, code))
text := fmt.Sprintf("您好,您正在%s。验证码:%s\r\n验证码有效期为十分钟,请勿向他人泄露。", purposeText, code)
return title, text
}
func clearBytes(data []byte) {
@@ -40,6 +40,16 @@ func TestSMTPDeliveryKeepsCodeOutOfChallenge(t *testing.T) {
clearBytes(message)
}
func TestCodeMessageDescribesExternalEmailBinding(t *testing.T) {
title, text := CodeMessage(PurposeExternalEmailBind, "12345678")
if title != "Smail 安全验证码" {
t.Fatalf("title = %q", title)
}
if !strings.Contains(text, "外部邮箱绑定验证") || !strings.Contains(text, "12345678") {
t.Fatalf("unexpected verification message: %q", text)
}
}
func TestSMTPModesAreValidated(t *testing.T) {
base := SMTPSettings{Host: "localhost", Port: 25, Security: SecurityPlain, From: "no-reply@example.com"}
if _, err := NewSMTPDelivery(base); err != nil {