2026-04-05 10:36:22 +08:00
|
|
|
package email
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/ssdomei232/goodBaby/handler/db"
|
2026-07-26 22:19:41 +08:00
|
|
|
"github.com/ssdomei232/goodBaby/internal/retry"
|
2026-04-05 10:36:22 +08:00
|
|
|
"github.com/ssdomei232/goodBaby/model"
|
|
|
|
|
"github.com/wneessen/go-mail"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
// GetEmailAccountFromRule 从 Rule 中获取 EmailAccount 配置
|
2026-04-05 10:36:22 +08:00
|
|
|
func GetEmailAccountFromRule(rule *model.Rule) (*EmailAccountConfig, error) {
|
|
|
|
|
var emailAccountConfig EmailAccountConfig
|
2026-07-26 22:19:41 +08:00
|
|
|
if err := db.LoadAccountConfig(rule.AccountID, &emailAccountConfig); err != nil {
|
2026-04-05 10:36:22 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &emailAccountConfig, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
// GetEmailRuleFromRule 从 Rule 中获取 EmailRule 配置
|
2026-04-05 10:36:22 +08:00
|
|
|
func GetEmailRuleFromRule(rule *model.Rule) (*EmailRule, error) {
|
|
|
|
|
var emailRule EmailRule
|
2026-07-26 22:19:41 +08:00
|
|
|
if err := json.Unmarshal([]byte(rule.ConfigJson), &emailRule); err != nil {
|
2026-04-05 10:36:22 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &emailRule, nil
|
|
|
|
|
}
|
2026-07-26 22:19:41 +08:00
|
|
|
|
|
|
|
|
// newSMTPClient 按账号配置里的加密方式创建 SMTP 客户端
|
|
|
|
|
func newSMTPClient(cfg *EmailAccountConfig) (*mail.Client, error) {
|
|
|
|
|
options := []mail.Option{
|
|
|
|
|
mail.WithPort(cfg.Port),
|
|
|
|
|
mail.WithUsername(cfg.Username),
|
|
|
|
|
mail.WithPassword(cfg.Password),
|
|
|
|
|
mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch cfg.SecurityOrDefault() {
|
|
|
|
|
case SecuritySSL:
|
|
|
|
|
options = append(options, mail.WithSSL())
|
|
|
|
|
case SecuritySTARTTLS:
|
|
|
|
|
options = append(options, mail.WithTLSPolicy(mail.TLSMandatory))
|
|
|
|
|
case SecurityNone:
|
|
|
|
|
options = append(options, mail.WithTLSPolicy(mail.NoTLS))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := mail.NewClient(cfg.SMTPServer, options...)
|
2026-04-05 10:36:22 +08:00
|
|
|
if err != nil {
|
2026-07-26 22:19:41 +08:00
|
|
|
return nil, fmt.Errorf("创建邮件客户端失败: %w", err)
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
2026-07-26 22:19:41 +08:00
|
|
|
return client, nil
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
func buildMessage(cfg *EmailAccountConfig, address, title, body string) (*mail.Msg, error) {
|
2026-04-05 10:36:22 +08:00
|
|
|
message := mail.NewMsg()
|
2026-07-26 22:19:41 +08:00
|
|
|
if err := message.From(cfg.FromOrDefault()); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("设置发件人失败: %w", err)
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
|
|
|
|
if err := message.To(address); err != nil {
|
2026-07-26 22:19:41 +08:00
|
|
|
return nil, fmt.Errorf("设置收件人失败: %w", err)
|
|
|
|
|
}
|
|
|
|
|
message.Subject(title)
|
|
|
|
|
message.SetBodyString(mail.TypeTextPlain, body)
|
|
|
|
|
return message, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendMailMsgWithRetry(ctx context.Context, cfg *EmailAccountConfig, rule *EmailRule, address string) error {
|
|
|
|
|
return retry.Do(ctx, func() error {
|
|
|
|
|
return sendMailMsg(ctx, cfg, rule, address)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendMailMsg(ctx context.Context, cfg *EmailAccountConfig, rule *EmailRule, address string) error {
|
|
|
|
|
client, err := newSMTPClient(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
message, err := buildMessage(cfg, address, rule.Title, rule.Msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
if err := client.DialAndSendWithContext(ctx, message); err != nil {
|
|
|
|
|
return fmt.Errorf("发送邮件失败: %w", err)
|
|
|
|
|
}
|
2026-04-05 10:36:22 +08:00
|
|
|
return nil
|
|
|
|
|
}
|