2025-10-02 16:16:33 +00:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"backend/pkg/notification/config"
|
|
|
|
|
|
"backend/pkg/notification/domain/repository"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
2025-11-04 09:47:36 +00:00
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
2025-10-02 16:16:33 +00:00
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ses"
|
2025-11-04 09:47:36 +00:00
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
2025-10-02 16:16:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// AwsEmailDeliveryParam 傳送參數配置
|
|
|
|
|
|
type AwsEmailDeliveryParam struct {
|
|
|
|
|
|
Conf *config.AmazonSesSettings
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type AwsEmailDeliveryRepository struct {
|
2025-10-22 13:40:31 +00:00
|
|
|
|
Client *ses.Client
|
|
|
|
|
|
Timeout int // 超時時間(秒),預設 30
|
2025-10-02 16:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func MustAwsSesMailRepository(param AwsEmailDeliveryParam) repository.MailRepository {
|
|
|
|
|
|
// 手動指定 AWS 配置,不使用默認配置
|
|
|
|
|
|
cfg := aws.Config{
|
|
|
|
|
|
Region: param.Conf.Region, // 自定義的 AWS 區域
|
|
|
|
|
|
Credentials: credentials.NewStaticCredentialsProvider(
|
|
|
|
|
|
param.Conf.AccessKey, // AWS Access Key
|
|
|
|
|
|
param.Conf.SecretKey, // AWS Secret Key
|
|
|
|
|
|
"",
|
|
|
|
|
|
),
|
|
|
|
|
|
}
|
|
|
|
|
|
// 創建 SES 客戶端
|
|
|
|
|
|
sesClient := ses.NewFromConfig(cfg)
|
|
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
|
// 設置默認超時時間
|
|
|
|
|
|
timeout := 30
|
|
|
|
|
|
if param.Conf.PoolSize > 0 {
|
|
|
|
|
|
timeout = param.Conf.PoolSize // 可以復用這個配置項,或新增專門的 Timeout 配置
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 16:16:33 +00:00
|
|
|
|
return &AwsEmailDeliveryRepository{
|
2025-10-22 13:40:31 +00:00
|
|
|
|
Client: sesClient,
|
|
|
|
|
|
Timeout: timeout,
|
2025-10-02 16:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
|
func (repo *AwsEmailDeliveryRepository) SendMail(ctx context.Context, req repository.MailReq) error {
|
|
|
|
|
|
// 檢查 context 是否已取消
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 設置郵件參數
|
|
|
|
|
|
to := make([]string, 0, len(req.To))
|
|
|
|
|
|
to = append(to, req.To...)
|
2025-10-02 16:16:33 +00:00
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
|
input := &ses.SendEmailInput{
|
|
|
|
|
|
Destination: &types.Destination{
|
|
|
|
|
|
ToAddresses: to,
|
|
|
|
|
|
},
|
|
|
|
|
|
Message: &types.Message{
|
|
|
|
|
|
Body: &types.Body{
|
|
|
|
|
|
Html: &types.Content{
|
2025-10-02 16:16:33 +00:00
|
|
|
|
Charset: aws.String("UTF-8"),
|
2025-10-22 13:40:31 +00:00
|
|
|
|
Data: aws.String(req.Body),
|
2025-10-02 16:16:33 +00:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-10-22 13:40:31 +00:00
|
|
|
|
Subject: &types.Content{
|
|
|
|
|
|
Charset: aws.String("UTF-8"),
|
|
|
|
|
|
Data: aws.String(req.Subject),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
Source: aws.String(req.From),
|
|
|
|
|
|
}
|
2025-10-02 16:16:33 +00:00
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
|
// 發送郵件(直接使用傳入的 context,不創建新的 context)
|
|
|
|
|
|
_, err := repo.Client.SendEmail(ctx, input)
|
2025-10-02 16:16:33 +00:00
|
|
|
|
if err != nil {
|
2025-11-04 09:47:36 +00:00
|
|
|
|
return err
|
2025-10-02 16:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|