2026-04-05 10:36:22 +08:00
|
|
|
package email
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-26 22:19:41 +08:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2026-04-05 10:36:22 +08:00
|
|
|
|
|
|
|
|
"github.com/ssdomei232/goodBaby/model"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
// SendMail 向规则配置里的所有地址发送邮件。
|
|
|
|
|
//
|
|
|
|
|
// 每个地址单独重试,互不阻塞;全部失败/部分失败都会汇总成错误返回,
|
|
|
|
|
// 由 runner 记录到执行日志中。
|
|
|
|
|
func SendMail(ctx context.Context, rule *model.Rule) error {
|
2026-04-05 10:36:22 +08:00
|
|
|
emailRule, err := GetEmailRuleFromRule(rule)
|
|
|
|
|
if err != nil {
|
2026-07-26 22:19:41 +08:00
|
|
|
return fmt.Errorf("获取邮件规则配置失败: %w", err)
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
accountConfig, err := GetEmailAccountFromRule(rule)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("获取邮件账户配置失败: %w", err)
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
2026-07-26 22:19:41 +08:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
fails []string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for _, destination := range emailRule.Destinations {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(address string) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := sendMailMsgWithRetry(ctx, accountConfig, emailRule, address); err != nil {
|
|
|
|
|
mu.Lock()
|
|
|
|
|
fails = append(fails, fmt.Sprintf("%s: %v", address, err))
|
|
|
|
|
mu.Unlock()
|
|
|
|
|
}
|
|
|
|
|
}(destination)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
if len(fails) > 0 {
|
|
|
|
|
return fmt.Errorf("%d/%d 封邮件发送失败: %s",
|
|
|
|
|
len(fails), len(emailRule.Destinations), strings.Join(fails, "; "))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|