保存邮件超时修复工作进度,准备接入可选登录验证

This commit is contained in:
xuans committed 2026-09-18 20:34:48 +08:00
1 parent b33126e11d
commit 9706e52a29
4 files changed
+108 -18

No files matched your search

+12 -1
View File
@@ -18,7 +18,11 @@ var errNullMX = errors.New("收件域名声明不接收邮件")
type mxLookup func(stdcontext.Context, string) ([]*net.MX, error)
func resolveMX(domain string, lookup mxLookup) ([]string, error) {
ctx, cancel := stdcontext.WithTimeout(stdcontext.Background(), 10*time.Second)
return resolveMXContext(stdcontext.Background(), domain, lookup)
}
func resolveMXContext(parent stdcontext.Context, domain string, lookup mxLookup) ([]string, error) {
ctx, cancel := stdcontext.WithTimeout(parent, 10*time.Second)
defer cancel()
records, err := lookup(ctx, domain)
if err != nil {
@@ -60,9 +64,16 @@ func stopFallback(err error) bool {
// deliverMX 只在确认本次未接收时切换安全通道,DATA 不确定结果不进入下一条路由。
func deliverMX(hosts []string, attempt func(host, port string) error) error {
return deliverMXContext(stdcontext.Background(), hosts, attempt)
}
func deliverMXContext(ctx stdcontext.Context, hosts []string, attempt func(host, port string) error) error {
var lastErr error
for _, host := range hosts {
for _, port := range []string{"25", "587", "465"} {
if err := ctx.Err(); err != nil {
return err
}
lastErr = attempt(host, port)
if lastErr == nil || stopFallback(lastErr) {
return lastErr
+10 -5
View File
@@ -1,6 +1,7 @@
package send
import (
stdcontext "context"
"crypto/tls"
"errors"
"fmt"
@@ -152,6 +153,10 @@ func parseRecipientAddress(user *parsemail.User) (string, string, error) {
}
func doSend(ctx *context.Context, fromDomain string, data []byte, to []*parsemail.User, from string, triggers ...string) (sendErr error, recipientErrors map[string]error) {
deliveryContext := stdcontext.Background()
if ctx != nil && ctx.Context != nil {
deliveryContext = ctx.Context
}
var accepted atomic.Int64
trigger := "邮件发送"
if len(triggers) > 0 {
@@ -243,7 +248,7 @@ func doSend(ctx *context.Context, fromDomain string, data []byte, to []*parsemai
}()
if domain == consts.TEST_DOMAIN {
err := smtp.SendMailUnsafe("localhost", "127.0.0.1:25", nil, from, fromDomain, buildAddress(tos), data)
err := smtp.SendMailUnsafeContext(deliveryContext, "localhost", "127.0.0.1:25", nil, from, fromDomain, buildAddress(tos), data)
if err != nil {
recordFailure(err)
} else {
@@ -252,17 +257,17 @@ func doSend(ctx *context.Context, fromDomain string, data []byte, to []*parsemai
return
}
hosts, err := resolveMX(domain, net.DefaultResolver.LookupMX)
hosts, err := resolveMXContext(deliveryContext, domain, net.DefaultResolver.LookupMX)
if err != nil {
recordFailure(err)
return
}
err = deliverMX(hosts, func(host, port string) error {
err = deliverMXContext(deliveryContext, hosts, func(host, port string) error {
address := net.JoinHostPort(host, port)
if port == "465" {
return smtp.SendMailWithTls(host, address, nil, from, fromDomain, buildAddress(tos), data)
return smtp.SendMailWithTLSContext(deliveryContext, host, address, nil, from, fromDomain, buildAddress(tos), data)
}
return smtp.SendMail(host, address, nil, from, fromDomain, buildAddress(tos), data)
return smtp.SendMailContext(deliveryContext, host, address, nil, from, fromDomain, buildAddress(tos), data)
})
if err != nil {
recordFailure(err)