更新
This commit is contained in:
+108
-61
@@ -1,90 +1,137 @@
|
||||
// Package aws 提供 Amazon SES 邮件发送通道。
|
||||
package aws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"code.yun.ink/pkg/mailx/interfaces"
|
||||
mailx "code.yun.ink/pkg/mailx"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/ses"
|
||||
"github.com/aws/aws-sdk-go/service/ses/sesiface"
|
||||
)
|
||||
|
||||
// 不支持变更发信人(必须配置好)
|
||||
// defaultTimeout 单次 API 调用的默认超时
|
||||
const defaultTimeout = 30 * time.Second
|
||||
|
||||
// Config AWS SES 通道配置
|
||||
type Config struct {
|
||||
AccessKeyID string // AccessKey ID
|
||||
AccessKeySecret string // AccessKey Secret
|
||||
Region string // Region,默认 ap-northeast-1
|
||||
Sender string // 默认发件人(必填,AWS 需要预先验证发件地址)
|
||||
Timeout time.Duration // 单次 API 调用的超时,默认 30s;0 表示交给 ctx 控制
|
||||
}
|
||||
|
||||
// Aws AWS SES 发送通道
|
||||
type Aws struct {
|
||||
interfaces.DefaultEmail
|
||||
// params *interfaces.EmailConfigDataAws
|
||||
cfg Config
|
||||
|
||||
initOnce sync.Once
|
||||
svc sesiface.SESAPI
|
||||
initErr error
|
||||
}
|
||||
|
||||
func NewAws() *Aws {
|
||||
aws := &Aws{}
|
||||
aws.Options = interfaces.DefaultOptions()
|
||||
aws.EmailType = interfaces.EmailTypeAws
|
||||
return aws
|
||||
// New 创建 AWS 通道
|
||||
func New(cfg Config) *Aws {
|
||||
if cfg.Region == "" {
|
||||
cfg.Region = "ap-northeast-1"
|
||||
}
|
||||
return &Aws{cfg: cfg}
|
||||
}
|
||||
|
||||
func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
||||
// Name 返回通道名称
|
||||
func (a *Aws) Name() string { return "aws" }
|
||||
|
||||
for _, o := range opt {
|
||||
o(&l.Options)
|
||||
// Send 发送一封邮件
|
||||
func (a *Aws) Send(ctx context.Context, msg *mailx.Message) error {
|
||||
logger := mailx.LoggerFromContext(ctx)
|
||||
|
||||
sender := msg.From
|
||||
if sender == "" {
|
||||
sender = a.cfg.Sender
|
||||
}
|
||||
if sender == "" {
|
||||
return fmt.Errorf("%w: aws sender is required", mailx.ErrInvalidConfig)
|
||||
}
|
||||
if len(msg.Inline) > 0 {
|
||||
return fmt.Errorf("%w: aws SendEmail does not support inline images; use raw message mode", mailx.ErrInvalidConfig)
|
||||
}
|
||||
|
||||
l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws)
|
||||
if l.Options.Aws == nil {
|
||||
return nil, errors.New("not aws")
|
||||
svc, err := a.client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Options.Aws.Region == "" {
|
||||
l.Options.Aws.Region = "ap-northeast-1"
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l *Aws) Send(ctx context.Context, params interfaces.Message) error {
|
||||
if l.Options.Aws == nil {
|
||||
return errors.New("not init")
|
||||
}
|
||||
// 配置AWS认证信息
|
||||
config := aws.Config{
|
||||
Region: aws.String(l.Options.Aws.Region), // 设置你的AWS区域
|
||||
Credentials: credentials.NewStaticCredentials(l.Options.Aws.AccessId, l.Options.Aws.AccessSecret, ""),
|
||||
}
|
||||
|
||||
// 创建AWS会话
|
||||
sess := session.Must(session.NewSession(&config))
|
||||
|
||||
// 创建SES客户端
|
||||
svc := ses.New(sess)
|
||||
|
||||
toAddress := []*string{}
|
||||
for _, val := range params.To {
|
||||
toAddress = append(toAddress, aws.String(val))
|
||||
}
|
||||
|
||||
// 使用SES服务发送邮件
|
||||
_, err := svc.SendEmail(&ses.SendEmailInput{
|
||||
input := &ses.SendEmailInput{
|
||||
Source: aws.String(sender),
|
||||
Destination: &ses.Destination{
|
||||
ToAddresses: toAddress,
|
||||
ToAddresses: aws.StringSlice(msg.To),
|
||||
CcAddresses: aws.StringSlice(msg.Cc),
|
||||
BccAddresses: aws.StringSlice(msg.Bcc),
|
||||
},
|
||||
Message: &ses.Message{
|
||||
Body: &ses.Body{
|
||||
Html: &ses.Content{
|
||||
Data: aws.String(params.Body),
|
||||
Charset: aws.String("UTF-8"),
|
||||
},
|
||||
},
|
||||
Subject: &ses.Content{
|
||||
Data: aws.String(params.Subject),
|
||||
Charset: aws.String("UTF-8"),
|
||||
},
|
||||
Subject: &ses.Content{Data: aws.String(msg.Subject), Charset: aws.String("UTF-8")},
|
||||
Body: awsBody(msg),
|
||||
},
|
||||
Source: aws.String(l.Options.Aws.Sender),
|
||||
})
|
||||
}
|
||||
if msg.ReplyTo != "" {
|
||||
input.ReplyToAddresses = aws.StringSlice([]string{msg.ReplyTo})
|
||||
}
|
||||
|
||||
// svc.SendRawEmail()
|
||||
ctx, cancel := a.withTimeout(ctx)
|
||||
defer cancel()
|
||||
if _, err := svc.SendEmailWithContext(ctx, input); err != nil {
|
||||
logger.Errorf(ctx, "mailx/aws: send to %v failed: %v", msg.To, err)
|
||||
return fmt.Errorf("%w: %v", mailx.ErrSendFailed, err)
|
||||
}
|
||||
logger.Infof(ctx, "mailx/aws: sent to %v subject=%q", msg.To, msg.Subject)
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
// client 惰性初始化并复用 SES 客户端(线程安全)。
|
||||
// 若已通过测试或其他方式注入 svc,则直接返回注入的客户端。
|
||||
func (a *Aws) client() (sesiface.SESAPI, error) {
|
||||
if a.svc != nil {
|
||||
return a.svc, a.initErr
|
||||
}
|
||||
a.initOnce.Do(func() {
|
||||
sess, err := session.NewSession(&aws.Config{
|
||||
Region: aws.String(a.cfg.Region),
|
||||
Credentials: credentials.NewStaticCredentials(a.cfg.AccessKeyID, a.cfg.AccessKeySecret, ""),
|
||||
})
|
||||
if err != nil {
|
||||
a.initErr = fmt.Errorf("%w: create aws session: %v", mailx.ErrInvalidConfig, err)
|
||||
return
|
||||
}
|
||||
a.svc = ses.New(sess)
|
||||
})
|
||||
return a.svc, a.initErr
|
||||
}
|
||||
|
||||
// withTimeout 叠加配置超时到 ctx(已有更早 deadline 时保持不变)
|
||||
func (a *Aws) withTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if a.cfg.Timeout <= 0 {
|
||||
return ctx, func() {}
|
||||
}
|
||||
if dl, ok := ctx.Deadline(); ok && time.Until(dl) <= a.cfg.Timeout {
|
||||
return ctx, func() {}
|
||||
}
|
||||
return context.WithTimeout(ctx, a.cfg.Timeout)
|
||||
}
|
||||
|
||||
// awsBody 构造 SES Body:优先使用 Html,其次 Text;两者都有时同时提供
|
||||
func awsBody(msg *mailx.Message) *ses.Body {
|
||||
body := &ses.Body{}
|
||||
if msg.Body != "" {
|
||||
body.Html = &ses.Content{Data: aws.String(msg.Body), Charset: aws.String("UTF-8")}
|
||||
}
|
||||
if msg.TextBody != "" {
|
||||
body.Text = &ses.Content{Data: aws.String(msg.TextBody), Charset: aws.String("UTF-8")}
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user