深化smtp文件的支持

This commit is contained in:
Yun
2025-11-21 14:01:43 +08:00
parent f2b8345e93
commit b3a87d0b54
16 changed files with 2075 additions and 200 deletions
+115 -84
View File
@@ -2,7 +2,6 @@ package aliyun
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -15,6 +14,12 @@ import (
"github.com/alibabacloud-go/tea/tea"
)
const (
MaxRecipients = 100
MaxRetries = 3
DefaultEndpoint = "dm.aliyuncs.com"
)
type Aliyun struct {
interfaces.DefaultEmail
client *dm20151123.Client
@@ -30,118 +35,70 @@ func NewAliyun() *Aliyun {
}
func (l *Aliyun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
for _, o := range opt {
o(&l.Options)
}
l.Options.Logger.Infof(ctx, "Aliyun:%+v", l.Options.Aliyun)
if l.Options.Aliyun == nil {
return nil, errors.New("not aliyun")
return nil, fmt.Errorf("Aliyun configuration is required")
}
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
// 验证配置
if err := l.validateConfig(); err != nil {
return nil, fmt.Errorf("invalid Aliyun config: %w", err)
}
// 安全日志输出
l.Options.Logger.Infof(ctx, "Aliyun configured - Endpoint:%s AccountName:%s",
l.Options.Aliyun.Endpoint, l.Options.Aliyun.AccountName)
config := &openapi.Config{
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
AccessKeySecret: tea.String(l.Options.Aliyun.AccessKey),
}
if l.Options.Aliyun.Endpoint == "" {
l.Options.Aliyun.Endpoint = "dm.aliyuncs.com"
}
// Endpoint 请参考 https://api.aliyun.com/product/Dm
if l.Options.Aliyun.Endpoint == "" {
l.Options.Aliyun.Endpoint = DefaultEndpoint
}
config.Endpoint = tea.String(l.Options.Aliyun.Endpoint)
result, err := dm20151123.NewClient(config)
client, err := dm20151123.NewClient(config)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create Aliyun client: %w", err)
}
return &Aliyun{
client: result,
}, nil
l.client = client
return l, nil
}
func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
if l.client == nil {
return errors.New("client no init")
}
if len(params.To) > 100 {
return errors.New("最多 100 个地址")
}
if l.Options.Aliyun.AccountName == "" {
return errors.New("AccountName 必填")
return fmt.Errorf("Aliyun client not initialized")
}
toAddress := strings.Join(params.To, ",")
singleSendMailRequest := &dm20151123.SingleSendMailRequest{}
singleSendMailRequest.AccountName = tea.String(l.Options.Aliyun.AccountName)
singleSendMailRequest.ToAddress = tea.String(toAddress) // 目标地址,多个 email 地址可以用逗号分隔,最多 100 个地址(支持邮件组)。
singleSendMailRequest.Subject = tea.String(params.Subject)
singleSendMailRequest.HtmlBody = tea.String(params.Body)
singleSendMailRequest.AddressType = tea.Int32(0) // 地址类型。取值:0:为随机账号1:为发信地址
if params.ReplyTo != "" {
singleSendMailRequest.ReplyToAddress = tea.Bool(false)
singleSendMailRequest.ReplyAddress = tea.String(params.ReplyTo)
} else {
singleSendMailRequest.ReplyToAddress = tea.Bool(true)
// 验证消息
if err := l.validateMessage(params); err != nil {
return fmt.Errorf("invalid message: %w", err)
}
runtime := &util.RuntimeOptions{}
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
resp, err := l.client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
by, _ := json.Marshal(resp)
fmt.Printf("resp:%+v err:%+v", string(by), err)
l.Options.Logger.Infof(ctx, "resp:%+v err:%+v", resp, err)
if err != nil {
return err
// 重试机制
var lastErr error
for i := 0; i < MaxRetries; i++ {
if i > 0 {
l.Options.Logger.Infof(ctx, "Retrying Aliyun email send, attempt %d/%d", i+1, MaxRetries)
time.Sleep(time.Duration(i) * time.Second)
}
return nil
}()
if tryErr != nil {
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
return tryErr
lastErr = l.sendEmail(ctx, params)
if lastErr == nil {
l.Options.Logger.Infof(ctx, "Aliyun email sent successfully to %v", params.To)
return nil
}
// var error = &tea.SDKError{}
// if _t, ok := tryErr.(*tea.SDKError); ok {
// error = _t
// } else {
// error.Message = tea.String(tryErr.Error())
// }
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// // 错误 message
// fmt.Println(tea.StringValue(error.Message))
// // 诊断地址
// var data interface{}
// d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
// d.Decode(&data)
// if m, ok := data.(map[string]interface{}); ok {
// recommend, _ := m["Recommend"]
// fmt.Println("recommend", recommend)
// }
// _, _err := util.AssertAsString(error.Message)
// if _err != nil {
// return _err
// }
l.Options.Logger.Errorf(ctx, "Aliyun send attempt %d failed: %v", i+1, lastErr)
}
return nil // 实现具体的 Aliyun 发送方法
// 如:return aliyunSDK.SendMail(params)
return fmt.Errorf("failed to send Aliyun email after %d attempts: %w", MaxRetries, lastErr)
}
// 同步状态
@@ -258,3 +215,77 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
}
return list, nextStart, nil
}
// 验证配置
func (l *Aliyun) validateConfig() error {
if l.Options.Aliyun.AccessId == "" {
return errors.New("AccessId is required")
}
if l.Options.Aliyun.AccessKey == "" {
return errors.New("AccessKey is required")
}
if l.Options.Aliyun.AccountName == "" {
return errors.New("AccountName is required")
}
return nil
}
// 验证消息
func (l *Aliyun) validateMessage(params interfaces.Message) error {
if len(params.To) == 0 {
return errors.New("at least one recipient is required")
}
if len(params.To) > MaxRecipients {
return fmt.Errorf("too many recipients: %d (max: %d)", len(params.To), MaxRecipients)
}
if params.Subject == "" {
return errors.New("subject is required")
}
if params.Body == "" {
return errors.New("body is required")
}
return nil
}
// 发送邮件核心逻辑
func (l *Aliyun) sendEmail(ctx context.Context, params interfaces.Message) error {
toAddress := strings.Join(params.To, ",")
request := &dm20151123.SingleSendMailRequest{
AccountName: tea.String(l.Options.Aliyun.AccountName),
ToAddress: tea.String(toAddress),
Subject: tea.String(params.Subject),
HtmlBody: tea.String(params.Body),
AddressType: tea.Int32(0), // 随机账号
}
// 设置回复地址
if params.ReplyTo != "" {
request.ReplyToAddress = tea.Bool(false)
request.ReplyAddress = tea.String(params.ReplyTo)
} else if l.Options.Aliyun.ReplyAddress != "" {
request.ReplyToAddress = tea.Bool(false)
request.ReplyAddress = tea.String(l.Options.Aliyun.ReplyAddress)
} else {
request.ReplyToAddress = tea.Bool(true)
}
// 设置发件人名称
if params.Form != "" {
request.FromAlias = tea.String(params.Form)
}
runtime := &util.RuntimeOptions{}
resp, err := l.client.SingleSendMailWithOptions(request, runtime)
if err != nil {
return fmt.Errorf("Aliyun API call failed: %w", err)
}
// 记录响应信息(不包含敏感数据)
if resp != nil && resp.Body != nil {
l.Options.Logger.Infof(ctx, "Aliyun email sent - RequestId:%s EnvId:%s",
tea.StringValue(resp.Body.RequestId), tea.StringValue(resp.Body.EnvId))
}
return nil
}