fix(mail): 发送失败时返回 SMTP 错误
- 发送邮件时若 `SMTP` 投递失败,直接返回 `502 Bad Gateway`。 - 新增测试覆盖投递失败场景,确认错误信息会透出。
This commit is contained in:
@@ -589,6 +589,32 @@ func TestCatchAllStoresUnregisteredMailForAdminOnly(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMailSendReturnsSMTPFailure(t *testing.T) {
|
||||||
|
a := newTestApp(t)
|
||||||
|
a.cfg.SMTPHost = "127.0.0.1"
|
||||||
|
a.cfg.SMTPPort = "1"
|
||||||
|
ts := httptest.NewServer(a.Router())
|
||||||
|
defer ts.Close()
|
||||||
|
admin := &testClient{t: t, server: ts}
|
||||||
|
|
||||||
|
var login map[string]any
|
||||||
|
if code := admin.do("POST", "/api/auth/login", map[string]string{"email": "admin@lanqin.local", "password": "ChangeMe123!"}, &login); code != http.StatusOK {
|
||||||
|
t.Fatalf("login code=%d body=%v", code, login)
|
||||||
|
}
|
||||||
|
payload := map[string]any{
|
||||||
|
"to": []string{"person@example.com"},
|
||||||
|
"subject": "smtp failure should surface",
|
||||||
|
"text": "hello",
|
||||||
|
}
|
||||||
|
var errBody map[string]any
|
||||||
|
if code := admin.do("POST", "/api/mail/send", payload, &errBody); code != http.StatusBadGateway {
|
||||||
|
t.Fatalf("smtp failure code=%d body=%v", code, errBody)
|
||||||
|
}
|
||||||
|
if got, _ := errBody["error"].(string); !strings.Contains(got, "smtp delivery failed") {
|
||||||
|
t.Fatalf("error=%q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAdminSMTPTestEndpoint(t *testing.T) {
|
func TestAdminSMTPTestEndpoint(t *testing.T) {
|
||||||
a := newTestApp(t)
|
a := newTestApp(t)
|
||||||
host, port, received := startFakeSMTP(t)
|
host, port, received := startFakeSMTP(t)
|
||||||
|
|||||||
@@ -330,7 +330,8 @@ func (a *App) handleMailSend(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
if a.cfg.SMTPHost != "" {
|
if a.cfg.SMTPHost != "" {
|
||||||
if err := a.sendSMTP(mb.Address, allRecipients, mimeBytes); err != nil {
|
if err := a.sendSMTP(mb.Address, allRecipients, mimeBytes); err != nil {
|
||||||
a.log.Warn("smtp delivery failed; keeping local sent copy", "error", err)
|
respondError(w, http.StatusBadGateway, "smtp delivery failed: "+err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user