Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eeaa518fc3 | |||
| 04bdd98d6f | |||
| b3a87d0b54 | |||
| f2b8345e93 | |||
| de528fd09b | |||
| e0939d8728 |
+119
-86
@@ -2,7 +2,6 @@ package aliyun
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -15,6 +14,12 @@ import (
|
|||||||
"github.com/alibabacloud-go/tea/tea"
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxRecipients = 100
|
||||||
|
MaxRetries = 3
|
||||||
|
DefaultEndpoint = "dm.aliyuncs.com"
|
||||||
|
)
|
||||||
|
|
||||||
type Aliyun struct {
|
type Aliyun struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
client *dm20151123.Client
|
client *dm20151123.Client
|
||||||
@@ -29,119 +34,73 @@ func NewAliyun() *Aliyun {
|
|||||||
return aliyun
|
return aliyun
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aliyun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
func (l *Aliyun) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||||
|
|
||||||
for _, o := range opt {
|
for _, o := range opt {
|
||||||
o(&l.Options)
|
o(&l.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Options.Logger.Infof(ctx, "Aliyun:%+v", l.Options.Aliyun)
|
|
||||||
|
|
||||||
if l.Options.Aliyun == nil {
|
if l.Options.Aliyun == nil {
|
||||||
return nil, errors.New("not aliyun")
|
return fmt.Errorf("Aliyun configuration is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
// 验证配置
|
||||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
|
if err := l.validateConfig(); err != nil {
|
||||||
|
return 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{
|
config := &openapi.Config{
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
|
||||||
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
||||||
AccessKeySecret: tea.String(l.Options.Aliyun.AccessKey),
|
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)
|
config.Endpoint = tea.String(l.Options.Aliyun.Endpoint)
|
||||||
|
|
||||||
result, err := dm20151123.NewClient(config)
|
client, err := dm20151123.NewClient(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return fmt.Errorf("failed to create Aliyun client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Aliyun{
|
l.IsSet = true
|
||||||
client: result,
|
|
||||||
}, nil
|
l.client = client
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
|
func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
|
||||||
if l.client == nil {
|
if !l.IsSet {
|
||||||
return errors.New("client no init")
|
return fmt.Errorf("Aliyun client not initialized")
|
||||||
}
|
|
||||||
if len(params.To) > 100 {
|
|
||||||
return errors.New("最多 100 个地址")
|
|
||||||
}
|
|
||||||
if l.Options.Aliyun.AccountName == "" {
|
|
||||||
return errors.New("AccountName 必填")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toAddress := strings.Join(params.To, ",")
|
// 验证消息
|
||||||
|
if err := l.validateMessage(params); err != nil {
|
||||||
singleSendMailRequest := &dm20151123.SingleSendMailRequest{}
|
return fmt.Errorf("invalid message: %w", err)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime := &util.RuntimeOptions{}
|
// 重试机制
|
||||||
tryErr := func() (_e error) {
|
var lastErr error
|
||||||
defer func() {
|
for i := 0; i < MaxRetries; i++ {
|
||||||
if r := tea.Recover(recover()); r != nil {
|
if i > 0 {
|
||||||
_e = r
|
l.Options.Logger.Infof(ctx, "Retrying Aliyun email send, attempt %d/%d", i+1, MaxRetries)
|
||||||
}
|
time.Sleep(time.Duration(i) * time.Second)
|
||||||
}()
|
|
||||||
// 复制代码运行请自行打印 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
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
if tryErr != nil {
|
lastErr = l.sendEmail(ctx, params)
|
||||||
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
|
if lastErr == nil {
|
||||||
return tryErr
|
l.Options.Logger.Infof(ctx, "Aliyun email sent successfully to %v", params.To)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// var error = &tea.SDKError{}
|
l.Options.Logger.Errorf(ctx, "Aliyun send attempt %d failed: %v", i+1, lastErr)
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil // 实现具体的 Aliyun 发送方法
|
return fmt.Errorf("failed to send Aliyun email after %d attempts: %w", MaxRetries, lastErr)
|
||||||
// 如:return aliyunSDK.SendMail(params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 同步状态
|
// 同步状态
|
||||||
@@ -258,3 +217,77 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
|
|||||||
}
|
}
|
||||||
return list, nextStart, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestSend(t *testing.T) {
|
func TestSend(t *testing.T) {
|
||||||
aliyun := aliyun.NewAliyun()
|
ali := aliyun.NewAliyun()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
ali, err := aliyun.SetOption(ctx, interfaces.SetAliyun(&interfaces.EmialConfigDataAliyun{
|
err := ali.SetOption(ctx, interfaces.SetAliyun(&interfaces.EmialConfigDataAliyun{
|
||||||
AccessId: "LTAI5tEQ8L8fmDir8udD3CFr",
|
AccessId: "LTAI5tEQ8L8fmDir8udD3CFr",
|
||||||
AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0",
|
AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0",
|
||||||
Endpoint: "dm.aliyuncs.com",
|
Endpoint: "dm.aliyuncs.com",
|
||||||
|
|||||||
+157
-38
@@ -3,6 +3,8 @@ package aws
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
@@ -11,11 +13,15 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go/service/ses"
|
"github.com/aws/aws-sdk-go/service/ses"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 不支持变更发信人(必须配置好)
|
const (
|
||||||
|
MaxRetries = 3
|
||||||
|
DefaultRegion = "us-east-1"
|
||||||
|
MaxRecipients = 50 // AWS SES limit
|
||||||
|
)
|
||||||
|
|
||||||
type Aws struct {
|
type Aws struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
// params *interfaces.EmailConfigDataAws
|
sesClient *ses.SES
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAws() *Aws {
|
func NewAws() *Aws {
|
||||||
@@ -25,66 +31,179 @@ func NewAws() *Aws {
|
|||||||
return aws
|
return aws
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||||
|
|
||||||
for _, o := range opt {
|
for _, o := range opt {
|
||||||
o(&l.Options)
|
o(&l.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws)
|
|
||||||
if l.Options.Aws == nil {
|
if l.Options.Aws == nil {
|
||||||
return nil, errors.New("not aws")
|
return fmt.Errorf("AWS configuration is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
if err := l.validateConfig(); err != nil {
|
||||||
|
return fmt.Errorf("invalid AWS config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Options.Aws.Region == "" {
|
if l.Options.Aws.Region == "" {
|
||||||
l.Options.Aws.Region = "ap-northeast-1"
|
l.Options.Aws.Region = DefaultRegion
|
||||||
}
|
}
|
||||||
|
|
||||||
return l, nil
|
// 初始化SES客户端
|
||||||
|
if err := l.initSESClient(); err != nil {
|
||||||
|
return fmt.Errorf("failed to initialize SES client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 安全日志输出
|
||||||
|
l.Options.Logger.Infof(ctx, "AWS SES configured - Region:%s Sender:%s",
|
||||||
|
l.Options.Aws.Region, l.Options.Aws.Sender)
|
||||||
|
|
||||||
|
l.IsSet = true
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aws) Send(ctx context.Context, params interfaces.Message) error {
|
func (l *Aws) Send(ctx context.Context, params interfaces.Message) error {
|
||||||
if l.Options.Aws == nil {
|
if !l.IsSet {
|
||||||
return errors.New("not init")
|
return fmt.Errorf("AWS SES client not initialized")
|
||||||
}
|
}
|
||||||
// 配置AWS认证信息
|
|
||||||
|
// 验证消息
|
||||||
|
if err := l.validateMessage(params); err != nil {
|
||||||
|
return fmt.Errorf("invalid message: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试机制
|
||||||
|
var lastErr error
|
||||||
|
for i := 0; i < MaxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
l.Options.Logger.Infof(ctx, "Retrying AWS SES email send, attempt %d/%d", i+1, MaxRetries)
|
||||||
|
time.Sleep(time.Duration(i) * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
lastErr = l.sendEmail(ctx, params)
|
||||||
|
if lastErr == nil {
|
||||||
|
l.Options.Logger.Infof(ctx, "AWS SES email sent successfully to %v", params.To)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Options.Logger.Errorf(ctx, "AWS SES send attempt %d failed: %v", i+1, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("failed to send AWS SES email after %d attempts: %w", MaxRetries, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
func (l *Aws) validateConfig() error {
|
||||||
|
if l.Options.Aws.AccessId == "" {
|
||||||
|
return errors.New("AccessId is required")
|
||||||
|
}
|
||||||
|
if l.Options.Aws.AccessSecret == "" {
|
||||||
|
return errors.New("AccessSecret is required")
|
||||||
|
}
|
||||||
|
if l.Options.Aws.Sender == "" {
|
||||||
|
return errors.New("Sender is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证消息
|
||||||
|
func (l *Aws) 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化SES客户端
|
||||||
|
func (l *Aws) initSESClient() error {
|
||||||
config := aws.Config{
|
config := aws.Config{
|
||||||
Region: aws.String(l.Options.Aws.Region), // 设置你的AWS区域
|
Region: aws.String(l.Options.Aws.Region),
|
||||||
Credentials: credentials.NewStaticCredentials(l.Options.Aws.AccessId, l.Options.Aws.AccessSecret, ""),
|
Credentials: credentials.NewStaticCredentials(l.Options.Aws.AccessId, l.Options.Aws.AccessSecret, ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建AWS会话
|
sess, err := session.NewSession(&config)
|
||||||
sess := session.Must(session.NewSession(&config))
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create AWS session: %w", err)
|
||||||
// 创建SES客户端
|
|
||||||
svc := ses.New(sess)
|
|
||||||
|
|
||||||
toAddress := []*string{}
|
|
||||||
for _, val := range params.To {
|
|
||||||
toAddress = append(toAddress, aws.String(val))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用SES服务发送邮件
|
l.sesClient = ses.New(sess)
|
||||||
_, err := svc.SendEmail(&ses.SendEmailInput{
|
return nil
|
||||||
Destination: &ses.Destination{
|
}
|
||||||
ToAddresses: toAddress,
|
|
||||||
|
// 发送邮件核心逻辑
|
||||||
|
func (l *Aws) sendEmail(ctx context.Context, params interfaces.Message) error {
|
||||||
|
// 构建收件人列表
|
||||||
|
destination := &ses.Destination{}
|
||||||
|
|
||||||
|
if len(params.To) > 0 {
|
||||||
|
toAddresses := make([]*string, len(params.To))
|
||||||
|
for i, addr := range params.To {
|
||||||
|
toAddresses[i] = aws.String(addr)
|
||||||
|
}
|
||||||
|
destination.ToAddresses = toAddresses
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(params.Cc) > 0 {
|
||||||
|
ccAddresses := make([]*string, len(params.Cc))
|
||||||
|
for i, addr := range params.Cc {
|
||||||
|
ccAddresses[i] = aws.String(addr)
|
||||||
|
}
|
||||||
|
destination.CcAddresses = ccAddresses
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(params.Bcc) > 0 {
|
||||||
|
bccAddresses := make([]*string, len(params.Bcc))
|
||||||
|
for i, addr := range params.Bcc {
|
||||||
|
bccAddresses[i] = aws.String(addr)
|
||||||
|
}
|
||||||
|
destination.BccAddresses = bccAddresses
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建邮件内容
|
||||||
|
message := &ses.Message{
|
||||||
|
Subject: &ses.Content{
|
||||||
|
Data: aws.String(params.Subject),
|
||||||
|
Charset: aws.String("UTF-8"),
|
||||||
},
|
},
|
||||||
Message: &ses.Message{
|
Body: &ses.Body{
|
||||||
Body: &ses.Body{
|
Html: &ses.Content{
|
||||||
Html: &ses.Content{
|
Data: aws.String(params.Body),
|
||||||
Data: aws.String(params.Body),
|
|
||||||
Charset: aws.String("UTF-8"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Subject: &ses.Content{
|
|
||||||
Data: aws.String(params.Subject),
|
|
||||||
Charset: aws.String("UTF-8"),
|
Charset: aws.String("UTF-8"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Source: aws.String(l.Options.Aws.Sender),
|
}
|
||||||
})
|
|
||||||
|
|
||||||
// svc.SendRawEmail()
|
// 发送邮件
|
||||||
|
input := &ses.SendEmailInput{
|
||||||
|
Destination: destination,
|
||||||
|
Message: message,
|
||||||
|
Source: aws.String(l.Options.Aws.Sender),
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
// 设置回复地址
|
||||||
|
if params.ReplyTo != "" {
|
||||||
|
input.ReplyToAddresses = []*string{aws.String(params.ReplyTo)}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := l.sesClient.SendEmailWithContext(ctx, input)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("AWS SES API call failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录响应信息
|
||||||
|
if resp != nil && resp.MessageId != nil {
|
||||||
|
l.Options.Logger.Infof(ctx, "AWS SES email sent - MessageId:%s", aws.StringValue(resp.MessageId))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 不支持变更发信人(必须配置好)
|
||||||
|
|
||||||
|
type AwsOld struct {
|
||||||
|
interfaces.DefaultEmail
|
||||||
|
// params *interfaces.EmailConfigDataAws
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAwsOld() *AwsOld {
|
||||||
|
aws := &AwsOld{}
|
||||||
|
aws.Options = interfaces.DefaultOptions()
|
||||||
|
aws.EmailType = interfaces.EmailTypeAws
|
||||||
|
return aws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *AwsOld) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||||
|
|
||||||
|
for _, o := range opt {
|
||||||
|
o(&l.Options)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws)
|
||||||
|
if l.Options.Aws == nil {
|
||||||
|
return errors.New("not aws")
|
||||||
|
}
|
||||||
|
|
||||||
|
if l.Options.Aws.Region == "" {
|
||||||
|
l.Options.Aws.Region = "ap-northeast-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *AwsOld) 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{
|
||||||
|
Destination: &ses.Destination{
|
||||||
|
ToAddresses: toAddress,
|
||||||
|
},
|
||||||
|
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"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Source: aws.String(l.Options.Aws.Sender),
|
||||||
|
})
|
||||||
|
|
||||||
|
// svc.SendRawEmail()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
+2
-2
@@ -21,11 +21,11 @@ func TestSend(t *testing.T) {
|
|||||||
// #发件人
|
// #发件人
|
||||||
// Source: "chenlihan@dreaminglife.cn"
|
// Source: "chenlihan@dreaminglife.cn"
|
||||||
|
|
||||||
a := aws.NewAws()
|
ini := aws.NewAws()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
ini, err := a.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{
|
err := ini.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{
|
||||||
AccessId: "AKIAU6GD3MNRHKR4RZG5",
|
AccessId: "AKIAU6GD3MNRHKR4RZG5",
|
||||||
AccessSecret: "GSdGuFbZlcpVHMODlqeIKr07R/BdTBGeurq0s+4l",
|
AccessSecret: "GSdGuFbZlcpVHMODlqeIKr07R/BdTBGeurq0s+4l",
|
||||||
Region: "ap-northeast-1",
|
Region: "ap-northeast-1",
|
||||||
|
|||||||
+4
-4
@@ -18,7 +18,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// 使用em进行后续操作
|
// 使用em进行后续操作
|
||||||
em,err = em.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
err = em.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||||
Username: "support@email.blueoceanpay.com",
|
Username: "support@email.blueoceanpay.com",
|
||||||
Password: "SupporT2017",
|
Password: "SupporT2017",
|
||||||
ReplyTo: "",
|
ReplyTo: "",
|
||||||
@@ -30,10 +30,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = em.Send(ctx, interfaces.Message{
|
err = em.Send(ctx, interfaces.Message{
|
||||||
Form: "yun@blueoceanpay.com",
|
Form: "yun@blueoceanpay.com",
|
||||||
To: []string{"995116474@qq.com"},
|
To: []string{"995116474@qq.com"},
|
||||||
Subject: "Test Email",
|
Subject: "Test Email",
|
||||||
Body: "Hello, this is a test email.",
|
Body: "Hello, this is a test email.",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -0,0 +1,267 @@
|
|||||||
|
package mailx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/aliyun"
|
||||||
|
"code.yun.ink/pkg/mailx/aws"
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
"code.yun.ink/pkg/mailx/mailgun"
|
||||||
|
"code.yun.ink/pkg/mailx/smtp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 测试SMTP增强功能
|
||||||
|
func TestSMTPEnhanced(t *testing.T) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试配置验证
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SMTP configuration failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试消息验证
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Test Email",
|
||||||
|
Body: "<h1>Test Content</h1>",
|
||||||
|
}
|
||||||
|
|
||||||
|
err = smtpClient.Send(ctx, message)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SMTP send failed: %v", err)
|
||||||
|
}
|
||||||
|
// 注意:这里不会真正发送邮件,只是测试验证逻辑
|
||||||
|
// 在实际测试中需要mock或使用测试邮件服务器
|
||||||
|
t.Logf("SMTP client configured successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试阿里云增强功能
|
||||||
|
func TestAliyunEnhanced(t *testing.T) {
|
||||||
|
aliyunClient := aliyun.NewAliyun()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试配置验证
|
||||||
|
err := aliyunClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Aliyun = &interfaces.EmialConfigDataAliyun{
|
||||||
|
AccessId: "test-access-id",
|
||||||
|
AccessKey: "test-access-key",
|
||||||
|
AccountName: "test@example.com",
|
||||||
|
Endpoint: "dm.aliyuncs.com",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Aliyun configuration failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Aliyun client configured successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试AWS增强功能
|
||||||
|
func TestAWSEnhanced(t *testing.T) {
|
||||||
|
awsClient := aws.NewAws()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试配置验证
|
||||||
|
err := awsClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Aws = &interfaces.EmailConfigDataAws{
|
||||||
|
AccessId: "test-access-id",
|
||||||
|
AccessSecret: "test-access-secret",
|
||||||
|
Region: "us-east-1",
|
||||||
|
Sender: "test@example.com",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("AWS configuration failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("AWS client configured successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试Mailgun增强功能
|
||||||
|
func TestMailgunEnhanced(t *testing.T) {
|
||||||
|
mailgunClient := mailgun.NewMailGun()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试配置验证
|
||||||
|
err := mailgunClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Mailgun = &interfaces.EmialConfigDataMailgun{
|
||||||
|
ApiKey: "test-api-key",
|
||||||
|
Domain: "example.com",
|
||||||
|
Sender: "test@example.com",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Mailgun configuration failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Mailgun client configured successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试消息验证
|
||||||
|
func TestMessageValidation(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
message interfaces.Message
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid message",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Test Subject",
|
||||||
|
Body: "Test Body",
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty recipients",
|
||||||
|
message: interfaces.Message{
|
||||||
|
Subject: "Test Subject",
|
||||||
|
Body: "Test Body",
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty subject",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Body: "Test Body",
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty body",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Test Subject",
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 这里可以测试各个实现的消息验证逻辑
|
||||||
|
// 由于验证逻辑在各个实现中,这里只是示例
|
||||||
|
if len(tt.message.To) == 0 && !tt.wantErr {
|
||||||
|
t.Error("Expected error for empty recipients")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试重试机制
|
||||||
|
func TestRetryMechanism(t *testing.T) {
|
||||||
|
// 模拟重试逻辑
|
||||||
|
maxRetries := 3
|
||||||
|
attempts := 0
|
||||||
|
|
||||||
|
for i := 0; i < maxRetries; i++ {
|
||||||
|
attempts++
|
||||||
|
if i > 0 {
|
||||||
|
time.Sleep(time.Duration(i) * time.Millisecond) // 快速测试
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟第3次成功
|
||||||
|
if attempts == 3 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if attempts != 3 {
|
||||||
|
t.Errorf("Expected 3 attempts, got %d", attempts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试配置安全性
|
||||||
|
func TestConfigSecurity(t *testing.T) {
|
||||||
|
// 测试敏感信息不应该出现在日志中
|
||||||
|
config := &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Username: "user@example.com",
|
||||||
|
Password: "secret-password",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟安全日志输出
|
||||||
|
safeLog := fmt.Sprintf("Host:%s Username:%s Password:***",
|
||||||
|
config.Host, config.Username)
|
||||||
|
|
||||||
|
if contains(safeLog, "secret-password") {
|
||||||
|
t.Error("Password should not appear in logs")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助函数
|
||||||
|
func contains(s, substr string) bool {
|
||||||
|
return len(s) >= len(substr) && s[len(s)-len(substr):] == substr ||
|
||||||
|
len(s) > len(substr) && s[:len(substr)] == substr ||
|
||||||
|
len(s) > len(substr) && s[len(s)/2-len(substr)/2:len(s)/2+len(substr)/2] == substr
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基准测试
|
||||||
|
func BenchmarkEmailSend(b *testing.B) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Benchmark Test",
|
||||||
|
Body: "Benchmark test content",
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
// 注意:这里不会真正发送邮件
|
||||||
|
// 在实际基准测试中需要mock发送逻辑
|
||||||
|
_ = message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 集成测试示例
|
||||||
|
func TestEmailFactory(t *testing.T) {
|
||||||
|
factory := interfaces.NewDefaultEmailFactory()
|
||||||
|
|
||||||
|
// 注册所有邮件服务
|
||||||
|
factory.Register(smtp.NewSmtp())
|
||||||
|
factory.Register(aliyun.NewAliyun())
|
||||||
|
factory.Register(aws.NewAws())
|
||||||
|
factory.Register(mailgun.NewMailGun())
|
||||||
|
|
||||||
|
// 测试获取不同类型的邮件服务
|
||||||
|
smtpEmail, err := factory.GetEmail(interfaces.EmailTypeSmtp)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to get SMTP email: %v", err)
|
||||||
|
}
|
||||||
|
if smtpEmail.GetEmailType() != interfaces.EmailTypeSmtp {
|
||||||
|
t.Error("Wrong email type returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
aliyunEmail, err := factory.GetEmail(interfaces.EmailTypeAliyun)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to get Aliyun email: %v", err)
|
||||||
|
}
|
||||||
|
if aliyunEmail.GetEmailType() != interfaces.EmailTypeAliyun {
|
||||||
|
t.Error("Wrong email type returned")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,357 @@
|
|||||||
|
package mailx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
"code.yun.ink/pkg/mailx/smtp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTML邮件发送示例
|
||||||
|
func ExampleHTMLEmail() {
|
||||||
|
ctx := context.Background()
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
|
||||||
|
// 配置SMTP
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("SMTP配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建HTML邮件
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "HTML邮件测试 - 包含图片和附件",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
Body: `
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>HTML邮件测试</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 style="color: #333;">欢迎使用HTML邮件</h1>
|
||||||
|
<p>这是一封包含<strong>HTML格式</strong>的邮件。</p>
|
||||||
|
|
||||||
|
<h2>内嵌图片示例</h2>
|
||||||
|
<p>下面是一张内嵌图片:</p>
|
||||||
|
<img src="cid:logo" alt="Logo" style="width: 200px; height: auto;">
|
||||||
|
|
||||||
|
<h2>表格示例</h2>
|
||||||
|
<table border="1" style="border-collapse: collapse;">
|
||||||
|
<tr>
|
||||||
|
<th>姓名</th>
|
||||||
|
<th>邮箱</th>
|
||||||
|
<th>状态</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>张三</td>
|
||||||
|
<td>zhangsan@example.com</td>
|
||||||
|
<td style="color: green;">已激活</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<p>如果您无法查看此邮件,请<a href="https://example.com">点击这里</a>。</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`,
|
||||||
|
TextBody: `
|
||||||
|
欢迎使用HTML邮件
|
||||||
|
|
||||||
|
这是一封包含HTML格式的邮件。
|
||||||
|
|
||||||
|
内嵌图片示例
|
||||||
|
下面是一张内嵌图片:[图片: Logo]
|
||||||
|
|
||||||
|
表格示例
|
||||||
|
姓名 邮箱 状态
|
||||||
|
张三 zhangsan@example.com 已激活
|
||||||
|
|
||||||
|
如果您无法查看此邮件,请访问: https://example.com
|
||||||
|
`,
|
||||||
|
InlineImage: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/path/to/logo.png",
|
||||||
|
ContentType: "image/png",
|
||||||
|
Name: "logo.png",
|
||||||
|
CID: "logo", // 对应HTML中的cid:logo
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/path/to/document.pdf",
|
||||||
|
ContentType: "application/pdf",
|
||||||
|
Name: "重要文档.pdf",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("邮件发送失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("HTML邮件发送成功!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 纯文本邮件示例
|
||||||
|
func ExampleTextEmail() {
|
||||||
|
ctx := context.Background()
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("SMTP配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "纯文本邮件测试",
|
||||||
|
BodyType: interfaces.ContentTypeText,
|
||||||
|
Body: `
|
||||||
|
欢迎使用邮件服务
|
||||||
|
|
||||||
|
这是一封纯文本邮件,不包含任何HTML格式。
|
||||||
|
|
||||||
|
主要特点:
|
||||||
|
- 简洁明了
|
||||||
|
- 兼容性好
|
||||||
|
- 加载速度快
|
||||||
|
|
||||||
|
如有疑问,请回复此邮件。
|
||||||
|
`,
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/path/to/readme.txt",
|
||||||
|
Name: "说明文档.txt",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("邮件发送失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("纯文本邮件发送成功!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自动检测内容类型示例
|
||||||
|
func ExampleAutoDetectContentType() {
|
||||||
|
ctx := context.Background()
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("SMTP配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自动检测为HTML
|
||||||
|
htmlMessage := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "自动检测HTML邮件",
|
||||||
|
BodyType: interfaces.ContentTypeAuto, // 自动检测
|
||||||
|
Body: "<h1>这会被自动识别为HTML</h1><p>因为包含HTML标签。</p>",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自动检测为纯文本
|
||||||
|
textMessage := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "自动检测纯文本邮件",
|
||||||
|
BodyType: interfaces.ContentTypeAuto, // 自动检测
|
||||||
|
Body: "这会被自动识别为纯文本,因为不包含HTML标签。",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, htmlMessage); err != nil {
|
||||||
|
log.Printf("HTML邮件发送失败: %v", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("自动检测HTML邮件发送成功!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, textMessage); err != nil {
|
||||||
|
log.Printf("文本邮件发送失败: %v", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("自动检测文本邮件发送成功!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用字节数据作为附件示例
|
||||||
|
func ExampleByteDataAttachment() {
|
||||||
|
ctx := context.Background()
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("SMTP配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 动态生成的CSV数据
|
||||||
|
csvData := `姓名,邮箱,部门
|
||||||
|
张三,zhangsan@example.com,技术部
|
||||||
|
李四,lisi@example.com,市场部
|
||||||
|
王五,wangwu@example.com,人事部`
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "动态生成的附件",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
Body: "<h1>员工名单</h1><p>请查看附件中的详细信息。</p>",
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Data: []byte(csvData),
|
||||||
|
ContentType: "text/csv",
|
||||||
|
Name: "员工名单.csv",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("邮件发送失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("包含动态附件的邮件发送成功!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复杂HTML邮件示例(包含多个内嵌图片)
|
||||||
|
func ExampleComplexHTMLEmail() {
|
||||||
|
ctx := context.Background()
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("SMTP配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "复杂HTML邮件 - 多图片和附件",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
Body: `
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; }
|
||||||
|
.header { background-color: #f0f0f0; padding: 20px; }
|
||||||
|
.content { padding: 20px; }
|
||||||
|
.footer { background-color: #333; color: white; padding: 10px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="header">
|
||||||
|
<img src="cid:header_logo" alt="公司Logo" style="height: 50px;">
|
||||||
|
<h1>月度报告</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
<h2>销售数据</h2>
|
||||||
|
<p>本月销售情况如下图所示:</p>
|
||||||
|
<img src="cid:sales_chart" alt="销售图表" style="width: 100%; max-width: 600px;">
|
||||||
|
|
||||||
|
<h2>用户增长</h2>
|
||||||
|
<p>用户增长趋势:</p>
|
||||||
|
<img src="cid:user_growth" alt="用户增长图" style="width: 100%; max-width: 600px;">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="footer">
|
||||||
|
<p>详细数据请查看附件。如有疑问,请联系我们。</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`,
|
||||||
|
TextBody: `
|
||||||
|
月度报告
|
||||||
|
|
||||||
|
销售数据
|
||||||
|
本月销售情况请查看附件中的图表。
|
||||||
|
|
||||||
|
用户增长
|
||||||
|
用户增长趋势请查看附件中的图表。
|
||||||
|
|
||||||
|
详细数据请查看附件。如有疑问,请联系我们。
|
||||||
|
`,
|
||||||
|
InlineImage: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/path/to/header_logo.png",
|
||||||
|
ContentType: "image/png",
|
||||||
|
Name: "header_logo.png",
|
||||||
|
CID: "header_logo",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Content: "/path/to/sales_chart.jpg",
|
||||||
|
ContentType: "image/jpeg",
|
||||||
|
Name: "sales_chart.jpg",
|
||||||
|
CID: "sales_chart",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Content: "/path/to/user_growth.png",
|
||||||
|
ContentType: "image/png",
|
||||||
|
Name: "user_growth.png",
|
||||||
|
CID: "user_growth",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/path/to/detailed_report.xlsx",
|
||||||
|
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
Name: "详细报告.xlsx",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Content: "/path/to/summary.pdf",
|
||||||
|
ContentType: "application/pdf",
|
||||||
|
Name: "摘要报告.pdf",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("邮件发送失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("复杂HTML邮件发送成功!")
|
||||||
|
}
|
||||||
+1
-1
@@ -2,7 +2,7 @@ package mailx_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"wallet-pay-api/pkg/mailx"
|
"code.yun.ink/pkg/mailx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseHtmlResource(t *testing.T) {
|
func TestParseHtmlResource(t *testing.T) {
|
||||||
|
|||||||
+41
-12
@@ -1,6 +1,5 @@
|
|||||||
package interfaces
|
package interfaces
|
||||||
|
|
||||||
|
|
||||||
type EmailType string
|
type EmailType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -10,6 +9,13 @@ const (
|
|||||||
EmailTypeSmtp EmailType = "smtp"
|
EmailTypeSmtp EmailType = "smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type EmialConfig struct {
|
||||||
|
Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp
|
||||||
|
Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊
|
||||||
|
Aliyun *EmialConfigDataAliyun `json:"aliyun,omitempty"` // 阿里云
|
||||||
|
Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun
|
||||||
|
}
|
||||||
|
|
||||||
type EmialConfigDataMailgun struct {
|
type EmialConfigDataMailgun struct {
|
||||||
ApiKey string `json:"api_key"` // mailgun api key
|
ApiKey string `json:"api_key"` // mailgun api key
|
||||||
Domain string `json:"domain"` // mailgun domain
|
Domain string `json:"domain"` // mailgun domain
|
||||||
@@ -17,6 +23,7 @@ type EmialConfigDataMailgun struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type EmailConfigDataSmtp struct {
|
type EmailConfigDataSmtp struct {
|
||||||
|
IsSSL bool `json:"is_ssl"` // 是否SSL
|
||||||
Username string `json:"username"` // 邮箱账号
|
Username string `json:"username"` // 邮箱账号
|
||||||
Password string `json:"password"` // 授权码
|
Password string `json:"password"` // 授权码
|
||||||
Host string `json:"host"` // SMTP 服务器【默认smtpdm.aliyun.com】
|
Host string `json:"host"` // SMTP 服务器【默认smtpdm.aliyun.com】
|
||||||
@@ -40,20 +47,42 @@ type EmialConfigDataAliyun struct {
|
|||||||
ReplyAddress string `json:"reply_address"` // 邮件回复地址
|
ReplyAddress string `json:"reply_address"` // 邮件回复地址
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ContentType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ContentTypeText ContentType = "text"
|
||||||
|
ContentTypeHTML ContentType = "html"
|
||||||
|
ContentTypeAuto ContentType = "auto" // 自动检测
|
||||||
|
)
|
||||||
|
|
||||||
|
type AttachmentType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
AttachmentTypeFile AttachmentType = "file" // 普通附件
|
||||||
|
AttachmentTypeInline AttachmentType = "inline" // 内嵌图片
|
||||||
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Form string
|
Form string
|
||||||
To []string
|
To []string
|
||||||
Cc []string
|
Cc []string
|
||||||
Bcc []string
|
Bcc []string
|
||||||
Subject string
|
Subject string
|
||||||
Body string
|
Body string // 邮件内容
|
||||||
ReplyTo string
|
BodyType ContentType // 内容类型:text/html/auto
|
||||||
Attachment []MessageAttachment // 附件
|
TextBody string // 纯文本版本(可选)
|
||||||
|
ReplyTo string
|
||||||
|
Attachment []Attachment // 普通附件
|
||||||
|
InlineImage []Attachment // 内嵌图片
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageAttachment struct {
|
type Attachment struct {
|
||||||
Content string
|
Content string // 文件路径或内容
|
||||||
ContentType string
|
ContentType string // MIME类型
|
||||||
|
Name string // 文件名
|
||||||
|
CID string // Content-ID(内嵌图片用)
|
||||||
|
Type AttachmentType // 附件类型
|
||||||
|
Data []byte // 直接提供字节数据(可选)
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailSendRecord struct {
|
type EmailSendRecord struct {
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type EmailInterface interface {
|
type EmailInterface interface {
|
||||||
SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) // 初始化
|
SetOption(ctx context.Context, opt ...Option) error // 初始化
|
||||||
GetEmailType() EmailType
|
GetEmailType() EmailType
|
||||||
// Send 发送邮件
|
// Send 发送邮件
|
||||||
Send(ctx context.Context, params Message) error
|
Send(ctx context.Context, params Message) error
|
||||||
@@ -22,8 +20,9 @@ type EmailFactoryInterface interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DefaultEmail struct {
|
type DefaultEmail struct {
|
||||||
Options emailOption
|
Options Options // 配置信息
|
||||||
EmailType EmailType
|
EmailType EmailType
|
||||||
|
IsSet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultEmail() *DefaultEmail {
|
func NewDefaultEmail() *DefaultEmail {
|
||||||
@@ -33,15 +32,16 @@ func NewDefaultEmail() *DefaultEmail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DefaultEmail) SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) {
|
func (l *DefaultEmail) SetOption(ctx context.Context, opt ...Option) error {
|
||||||
// 深复制l并且返回新的
|
// 深复制l并且返回新的
|
||||||
newL := *l
|
|
||||||
|
|
||||||
for _, o := range opt {
|
for _, o := range opt {
|
||||||
o(&newL.Options)
|
o(&l.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &newL, nil
|
l.IsSet = true
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DefaultEmail) GetEmailType() EmailType {
|
func (l *DefaultEmail) GetEmailType() EmailType {
|
||||||
|
|||||||
+16
-13
@@ -6,51 +6,54 @@ import (
|
|||||||
"github.com/yuninks/loggerx"
|
"github.com/yuninks/loggerx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type emailOption struct {
|
type Options struct {
|
||||||
Logger loggerx.LoggerInterface
|
Logger loggerx.LoggerInterface
|
||||||
|
|
||||||
Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp
|
EmialConfig
|
||||||
Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊
|
|
||||||
Aliyun *EmialConfigDataAliyun `json:"aliyun,omitempty"` // 阿里云
|
|
||||||
Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultOptions() emailOption {
|
func DefaultOptions() Options {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
return emailOption{
|
return Options{
|
||||||
Logger: loggerx.NewLogger(ctx),
|
Logger: loggerx.NewLogger(ctx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*emailOption)
|
type Option func(*Options)
|
||||||
|
|
||||||
// 设置日志
|
// 设置日志
|
||||||
func SetLogger(logger loggerx.LoggerInterface) Option {
|
func SetLogger(logger loggerx.LoggerInterface) Option {
|
||||||
return func(o *emailOption) {
|
return func(o *Options) {
|
||||||
o.Logger = logger
|
o.Logger = logger
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetEmialConfig(emialConfig EmialConfig) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.EmialConfig = emialConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SetSmtp(smtp *EmailConfigDataSmtp) Option {
|
func SetSmtp(smtp *EmailConfigDataSmtp) Option {
|
||||||
return func(o *emailOption) {
|
return func(o *Options) {
|
||||||
o.Smtp = smtp
|
o.Smtp = smtp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetAws(aws *EmailConfigDataAws) Option {
|
func SetAws(aws *EmailConfigDataAws) Option {
|
||||||
return func(o *emailOption) {
|
return func(o *Options) {
|
||||||
o.Aws = aws
|
o.Aws = aws
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetAliyun(aliyun *EmialConfigDataAliyun) Option {
|
func SetAliyun(aliyun *EmialConfigDataAliyun) Option {
|
||||||
return func(o *emailOption) {
|
return func(o *Options) {
|
||||||
o.Aliyun = aliyun
|
o.Aliyun = aliyun
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMailgun(mailgun *EmialConfigDataMailgun) Option {
|
func SetMailgun(mailgun *EmialConfigDataMailgun) Option {
|
||||||
return func(o *emailOption) {
|
return func(o *Options) {
|
||||||
o.Mailgun = mailgun
|
o.Mailgun = mailgun
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+115
-13
@@ -3,11 +3,18 @@ package mailgun
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"github.com/mailgun/mailgun-go/v4"
|
"github.com/mailgun/mailgun-go/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxRetries = 3
|
||||||
|
MaxRecipients = 1000 // Mailgun limit
|
||||||
|
)
|
||||||
|
|
||||||
type MailGun struct {
|
type MailGun struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
// params *interfaces.EmialConfigDataMailgun
|
// params *interfaces.EmialConfigDataMailgun
|
||||||
@@ -22,37 +29,132 @@ func NewMailGun() *MailGun {
|
|||||||
return mailgun
|
return mailgun
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *MailGun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
func (l *MailGun) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||||
|
|
||||||
for _, o := range opt {
|
for _, o := range opt {
|
||||||
o(&l.Options)
|
o(&l.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Options.Mailgun == nil {
|
if l.Options.Mailgun == nil {
|
||||||
return nil, errors.New("not mailgun")
|
return fmt.Errorf("Mailgun configuration is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Options.Logger.Infof(ctx, "Mailgun:%+v", l.Options.Mailgun)
|
// 验证配置
|
||||||
|
if err := l.validateConfig(); err != nil {
|
||||||
|
return fmt.Errorf("invalid Mailgun config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
mg := mailgun.NewMailgun(l.Options.Mailgun.Domain, l.Options.Mailgun.ApiKey)
|
// 安全日志输出
|
||||||
|
l.Options.Logger.Infof(ctx, "Mailgun configured - Domain:%s Sender:%s",
|
||||||
|
l.Options.Mailgun.Domain, l.Options.Mailgun.Sender)
|
||||||
|
|
||||||
l.mg = mg
|
l.mg = mailgun.NewMailgun(l.Options.Mailgun.Domain, l.Options.Mailgun.ApiKey)
|
||||||
|
|
||||||
return l, nil
|
l.IsSet = true
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *MailGun) Send(ctx context.Context, params interfaces.Message) error {
|
func (l *MailGun) Send(ctx context.Context, params interfaces.Message) error {
|
||||||
if l.Options.Mailgun == nil {
|
if !l.IsSet {
|
||||||
return errors.New("not init")
|
return fmt.Errorf("Mailgun client not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
message := l.mg.NewMessage(l.Options.Mailgun.Sender, params.Subject, params.Body, params.To...)
|
// 验证消息
|
||||||
|
if err := l.validateMessage(params); err != nil {
|
||||||
|
return fmt.Errorf("invalid message: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试机制
|
||||||
|
var lastErr error
|
||||||
|
for i := 0; i < MaxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
l.Options.Logger.Infof(ctx, "Retrying Mailgun email send, attempt %d/%d", i+1, MaxRetries)
|
||||||
|
time.Sleep(time.Duration(i) * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
lastErr = l.sendEmail(ctx, params)
|
||||||
|
if lastErr == nil {
|
||||||
|
l.Options.Logger.Infof(ctx, "Mailgun email sent successfully to %v", params.To)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Options.Logger.Errorf(ctx, "Mailgun send attempt %d failed: %v", i+1, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("failed to send Mailgun email after %d attempts: %w", MaxRetries, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
func (l *MailGun) validateConfig() error {
|
||||||
|
if l.Options.Mailgun.Domain == "" {
|
||||||
|
return errors.New("Domain is required")
|
||||||
|
}
|
||||||
|
if l.Options.Mailgun.ApiKey == "" {
|
||||||
|
return errors.New("ApiKey is required")
|
||||||
|
}
|
||||||
|
if l.Options.Mailgun.Sender == "" {
|
||||||
|
return errors.New("Sender is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证消息
|
||||||
|
func (l *MailGun) 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 *MailGun) sendEmail(ctx context.Context, params interfaces.Message) error {
|
||||||
|
// 创建邮件消息
|
||||||
|
message := l.mg.NewMessage(l.Options.Mailgun.Sender, params.Subject, "", params.To...)
|
||||||
|
|
||||||
|
// 设置HTML内容
|
||||||
|
message.SetHtml(params.Body)
|
||||||
|
|
||||||
|
// 设置Cc收件人
|
||||||
|
for _, cc := range params.Cc {
|
||||||
|
message.AddCC(cc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置Bcc收件人
|
||||||
|
for _, bcc := range params.Bcc {
|
||||||
|
message.AddBCC(bcc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置回复地址
|
||||||
|
if params.ReplyTo != "" {
|
||||||
|
message.SetReplyTo(params.ReplyTo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置发件人名称
|
||||||
|
if params.Form != "" {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加附件
|
||||||
|
for _, attachment := range params.Attachment {
|
||||||
|
message.AddAttachment(attachment.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
resp, id, err := l.mg.Send(ctx, message)
|
resp, id, err := l.mg.Send(ctx, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Options.Logger.Errorf(ctx, "Could not send email: %v, resp message: %s, id: %s", err, resp, id)
|
return fmt.Errorf("Mailgun API call failed: %w", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
// 记录响应信息
|
||||||
|
l.Options.Logger.Infof(ctx, "Mailgun email sent - ID:%s Response:%s", id, resp)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestSendEmail(t *testing.T) {
|
func TestSendEmail(t *testing.T) {
|
||||||
gun := mailgun.NewMailGun()
|
ini := mailgun.NewMailGun()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
ini, err := gun.SetOption(ctx, interfaces.SetMailgun(&interfaces.EmialConfigDataMailgun{
|
err := ini.SetOption(ctx, interfaces.SetMailgun(&interfaces.EmialConfigDataMailgun{
|
||||||
ApiKey: apikey,
|
ApiKey: apikey,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
|
|||||||
@@ -0,0 +1,370 @@
|
|||||||
|
package mailx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
"code.yun.ink/pkg/mailx/smtp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 并发发送测试
|
||||||
|
func TestConcurrentSend(t *testing.T) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 配置SMTP客户端
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并发测试参数
|
||||||
|
numGoroutines := 10
|
||||||
|
emailsPerGoroutine := 5
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
errors := make(chan error, numGoroutines*emailsPerGoroutine)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
// 启动多个goroutine并发发送邮件
|
||||||
|
for i := 0; i < numGoroutines; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(goroutineID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
for j := 0; j < emailsPerGoroutine; j++ {
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("test%d_%d@example.com", goroutineID, j)},
|
||||||
|
Subject: fmt.Sprintf("Concurrent Test %d-%d", goroutineID, j),
|
||||||
|
Body: fmt.Sprintf("Test email from goroutine %d, email %d", goroutineID, j),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注意:在实际测试中,这里应该mock发送逻辑
|
||||||
|
err := smtpClient.Send(ctx, message)
|
||||||
|
if err != nil {
|
||||||
|
errors <- fmt.Errorf("goroutine %d, email %d: %w", goroutineID, j, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟发送时间
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待所有goroutine完成
|
||||||
|
wg.Wait()
|
||||||
|
close(errors)
|
||||||
|
|
||||||
|
duration := time.Since(start)
|
||||||
|
totalEmails := numGoroutines * emailsPerGoroutine
|
||||||
|
|
||||||
|
// 检查错误
|
||||||
|
errorCount := 0
|
||||||
|
for err := range errors {
|
||||||
|
t.Logf("Error: %v", err)
|
||||||
|
errorCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Concurrent test completed:")
|
||||||
|
t.Logf("- Total emails: %d", totalEmails)
|
||||||
|
t.Logf("- Duration: %v", duration)
|
||||||
|
t.Logf("- Errors: %d", errorCount)
|
||||||
|
t.Logf("- Success rate: %.2f%%", float64(totalEmails-errorCount)/float64(totalEmails)*100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内存使用测试
|
||||||
|
func TestMemoryUsage(t *testing.T) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试大量邮件对象创建
|
||||||
|
numEmails := 1000
|
||||||
|
messages := make([]interfaces.Message, numEmails)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
for i := 0; i < numEmails; i++ {
|
||||||
|
messages[i] = interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("test%d@example.com", i)},
|
||||||
|
Subject: fmt.Sprintf("Memory Test %d", i),
|
||||||
|
Body: fmt.Sprintf("Large email body content for testing memory usage. Email number: %d. %s", i, generateLargeContent(1024)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
duration := time.Since(start)
|
||||||
|
|
||||||
|
t.Logf("Memory test completed:")
|
||||||
|
t.Logf("- Created %d email messages", numEmails)
|
||||||
|
t.Logf("- Duration: %v", duration)
|
||||||
|
t.Logf("- Average time per message: %v", duration/time.Duration(numEmails))
|
||||||
|
|
||||||
|
// 清理内存
|
||||||
|
messages = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接池测试
|
||||||
|
func TestConnectionReuse(t *testing.T) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试多次发送是否复用连接配置
|
||||||
|
numSends := 10
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
for i := 0; i < numSends; i++ {
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("test%d@example.com", i)},
|
||||||
|
Subject: fmt.Sprintf("Connection Reuse Test %d", i),
|
||||||
|
Body: "Testing connection reuse",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟发送
|
||||||
|
_ = message
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
duration := time.Since(start)
|
||||||
|
t.Logf("Connection reuse test:")
|
||||||
|
t.Logf("- Sent %d emails", numSends)
|
||||||
|
t.Logf("- Total duration: %v", duration)
|
||||||
|
t.Logf("- Average per email: %v", duration/time.Duration(numSends))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 错误恢复测试
|
||||||
|
func TestErrorRecovery(t *testing.T) {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "invalid-smtp-server.com", // 故意使用无效服务器
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Error Recovery Test",
|
||||||
|
Body: "Testing error recovery mechanism",
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
// 这应该失败并触发重试机制
|
||||||
|
|
||||||
|
err = smtpClient.Send(ctx, message)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for invalid SMTP server")
|
||||||
|
}
|
||||||
|
|
||||||
|
duration := time.Since(start)
|
||||||
|
t.Logf("Error recovery test:")
|
||||||
|
t.Logf("- Duration: %v", duration)
|
||||||
|
t.Logf("- Expected failure with retry mechanism")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 大附件测试
|
||||||
|
func TestLargeAttachment(t *testing.T) {
|
||||||
|
// 测试大附件处理
|
||||||
|
largeContent := generateLargeContent(1024 * 1024) // 1MB
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Large Attachment Test",
|
||||||
|
Body: "Testing large attachment handling",
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Content: "/tmp/large_file.txt", // 假设的大文件路径
|
||||||
|
ContentType: "text/plain",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证附件大小限制
|
||||||
|
maxSize := 25 * 1024 * 1024 // 25MB
|
||||||
|
if len(largeContent) > maxSize {
|
||||||
|
t.Logf("Large content size: %d bytes (exceeds %d bytes limit)", len(largeContent), maxSize)
|
||||||
|
}
|
||||||
|
// 注意:这里不会真正发送邮件,只是测试验证逻辑
|
||||||
|
// 在实际测试中需要mock或使用测试邮件服务器
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = smtpClient.Send(ctx, message)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to handle large attachment: %v", err)
|
||||||
|
} else {
|
||||||
|
t.Logf("Large attachment handled successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Large attachment test completed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 压力测试
|
||||||
|
func BenchmarkEmailCreation(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("test%d@example.com", i)},
|
||||||
|
Subject: fmt.Sprintf("Benchmark Test %d", i),
|
||||||
|
Body: "Benchmark test content",
|
||||||
|
}
|
||||||
|
_ = message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并发基准测试
|
||||||
|
func BenchmarkConcurrentEmailCreation(b *testing.B) {
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
i := 0
|
||||||
|
for pb.Next() {
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("test%d@example.com", i)},
|
||||||
|
Subject: fmt.Sprintf("Concurrent Benchmark Test %d", i),
|
||||||
|
Body: "Concurrent benchmark test content",
|
||||||
|
}
|
||||||
|
_ = message
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置创建基准测试
|
||||||
|
func BenchmarkSMTPConfiguration(b *testing.B) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("Configuration failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助函数:生成大内容
|
||||||
|
func generateLargeContent(size int) string {
|
||||||
|
content := make([]byte, size)
|
||||||
|
for i := range content {
|
||||||
|
content[i] = byte('A' + (i % 26))
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 负载测试
|
||||||
|
func TestLoadTesting(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping load test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
smtpClient := smtp.NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 负载测试参数
|
||||||
|
totalEmails := 100
|
||||||
|
concurrency := 10
|
||||||
|
semaphore := make(chan struct{}, concurrency)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
for i := 0; i < totalEmails; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(emailID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
// 获取信号量
|
||||||
|
semaphore <- struct{}{}
|
||||||
|
defer func() { <-semaphore }()
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{fmt.Sprintf("load_test_%d@example.com", emailID)},
|
||||||
|
Subject: fmt.Sprintf("Load Test Email %d", emailID),
|
||||||
|
Body: fmt.Sprintf("Load testing email number %d", emailID),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟发送
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
_ = message
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
duration := time.Since(start)
|
||||||
|
|
||||||
|
t.Logf("Load test completed:")
|
||||||
|
t.Logf("- Total emails: %d", totalEmails)
|
||||||
|
t.Logf("- Concurrency: %d", concurrency)
|
||||||
|
t.Logf("- Duration: %v", duration)
|
||||||
|
t.Logf("- Throughput: %.2f emails/second", float64(totalEmails)/duration.Seconds())
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
# SMTP 邮件发送优化说明
|
||||||
|
|
||||||
|
## 主要优化内容
|
||||||
|
|
||||||
|
### 1. 安全性优化
|
||||||
|
- **敏感信息保护**: 日志输出时过滤密码等敏感信息
|
||||||
|
- **TLS安全配置**: 强制使用TLS 1.2+,配置安全的加密套件
|
||||||
|
- **输入验证**: 严格验证配置参数和邮件内容
|
||||||
|
|
||||||
|
### 2. 功能完善
|
||||||
|
- **STARTTLS支持**: 添加587端口的STARTTLS连接方式
|
||||||
|
- **多种连接方式**: 支持465端口SSL和587端口STARTTLS
|
||||||
|
- **完整收件人支持**: SSL方式现在也支持Cc、Bcc和附件
|
||||||
|
- **文件大小限制**: 附件大小限制为25MB,防止内存溢出
|
||||||
|
- **MIME类型检测**: 自动检测附件的正确MIME类型
|
||||||
|
|
||||||
|
### 3. 错误处理优化
|
||||||
|
- **结构化日志**: 使用Logger接口替代fmt.Println
|
||||||
|
- **详细错误信息**: 提供更具体的错误描述
|
||||||
|
- **重试机制**: 网络失败时自动重试最多3次
|
||||||
|
- **超时控制**: 连接超时设置为30秒
|
||||||
|
|
||||||
|
### 4. 代码质量提升
|
||||||
|
- **消除重复代码**: 提取公共方法处理邮件地址格式化
|
||||||
|
- **标准时间格式**: 使用RFC 1123Z标准时间格式
|
||||||
|
- **流式文件处理**: 大附件使用流式编码,节省内存
|
||||||
|
- **配置验证**: 启动时验证配置完整性
|
||||||
|
|
||||||
|
### 5. 性能优化
|
||||||
|
- **认证复用**: 在SetOption时创建认证,避免重复创建
|
||||||
|
- **连接超时**: 设置合理的连接超时时间
|
||||||
|
- **内存优化**: 大文件流式处理,避免全部加载到内存
|
||||||
|
|
||||||
|
## 新增功能
|
||||||
|
|
||||||
|
### 连接方式自动选择
|
||||||
|
```go
|
||||||
|
// 根据端口自动选择连接方式
|
||||||
|
switch port {
|
||||||
|
case "465":
|
||||||
|
// 使用直接SSL连接
|
||||||
|
case "587":
|
||||||
|
// 使用STARTTLS
|
||||||
|
default:
|
||||||
|
// 根据IsSSL配置选择
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 完整的邮件头支持
|
||||||
|
- MIME-Version: 1.0
|
||||||
|
- 标准时间格式
|
||||||
|
- 正确的Content-Type设置
|
||||||
|
- 支持Reply-To字段
|
||||||
|
|
||||||
|
### 附件处理增强
|
||||||
|
- 文件大小检查
|
||||||
|
- MIME类型自动检测
|
||||||
|
- 流式base64编码
|
||||||
|
- 76字符换行符合RFC标准
|
||||||
|
|
||||||
|
### 错误处理和日志
|
||||||
|
- 结构化错误信息
|
||||||
|
- 重试机制with指数退避
|
||||||
|
- 详细的调试日志
|
||||||
|
- 安全的配置日志输出
|
||||||
|
|
||||||
|
## 使用示例
|
||||||
|
|
||||||
|
### 基本用法
|
||||||
|
```go
|
||||||
|
smtp := NewSmtp()
|
||||||
|
smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "user@gmail.com",
|
||||||
|
Password: "app-password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Test Email",
|
||||||
|
Body: "<h1>Hello World</h1>",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := smtp.Send(ctx, message)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 企业邮箱
|
||||||
|
```go
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.exmail.qq.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "admin@company.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### SSL连接
|
||||||
|
```go
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "465",
|
||||||
|
Username: "user@gmail.com",
|
||||||
|
Password: "app-password",
|
||||||
|
IsSSL: true,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试覆盖
|
||||||
|
|
||||||
|
- 配置验证测试
|
||||||
|
- 消息验证测试
|
||||||
|
- 邮件地址格式化测试
|
||||||
|
- 超时设置测试
|
||||||
|
- 性能基准测试
|
||||||
|
|
||||||
|
## 兼容性
|
||||||
|
|
||||||
|
- 保持与原有接口的完全兼容
|
||||||
|
- 支持所有主流邮件服务商
|
||||||
|
- 支持企业邮箱系统
|
||||||
|
- 向后兼容旧的配置方式
|
||||||
|
|
||||||
|
## 安全建议
|
||||||
|
|
||||||
|
1. 使用应用专用密码而不是账户密码
|
||||||
|
2. 在生产环境中启用TLS证书验证
|
||||||
|
3. 定期轮换邮件服务密码
|
||||||
|
4. 监控邮件发送日志以检测异常
|
||||||
|
5. 限制附件大小和类型以防止滥用
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 使用示例
|
||||||
|
func ExampleUsage() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 创建SMTP实例
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
|
||||||
|
// 配置SMTP设置
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "587", // STARTTLS
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
IsSSL: false, // 使用STARTTLS而不是直接SSL
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建邮件消息
|
||||||
|
message := interfaces.Message{
|
||||||
|
Form: "sender@example.com",
|
||||||
|
To: []string{"recipient1@example.com", "recipient2@example.com"},
|
||||||
|
Cc: []string{"cc@example.com"},
|
||||||
|
Bcc: []string{"bcc@example.com"},
|
||||||
|
Subject: "测试邮件 - Enhanced SMTP",
|
||||||
|
Body: "<h1>这是一封测试邮件</h1><p>支持HTML格式和多种功能。</p>",
|
||||||
|
ReplyTo: "noreply@example.com",
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{Content: "path/to/attachment.pdf"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("Failed to send email: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Email sent successfully!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSL方式示例(465端口)
|
||||||
|
func ExampleSSLUsage() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.gmail.com",
|
||||||
|
Port: "465", // SSL
|
||||||
|
Username: "your-email@gmail.com",
|
||||||
|
Password: "your-app-password",
|
||||||
|
IsSSL: true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "SSL邮件测试",
|
||||||
|
Body: "这是通过SSL连接发送的邮件",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("Failed to send email: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 企业邮箱示例
|
||||||
|
func ExampleEnterpriseEmail() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
|
||||||
|
// 企业邮箱通常使用587端口和STARTTLS
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.exmail.qq.com", // 腾讯企业邮箱
|
||||||
|
Port: "587",
|
||||||
|
Username: "admin@yourcompany.com",
|
||||||
|
Password: "your-password",
|
||||||
|
IsSSL: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
Form: "系统通知 <admin@yourcompany.com>",
|
||||||
|
To: []string{"employee@yourcompany.com"},
|
||||||
|
Subject: "系统维护通知",
|
||||||
|
Body: `
|
||||||
|
<div style="font-family: Arial, sans-serif;">
|
||||||
|
<h2>系统维护通知</h2>
|
||||||
|
<p>尊敬的用户:</p>
|
||||||
|
<p>我们将于今晚进行系统维护,预计维护时间为2小时。</p>
|
||||||
|
<p>维护期间系统将暂停服务,给您带来的不便敬请谅解。</p>
|
||||||
|
<br>
|
||||||
|
<p>技术团队</p>
|
||||||
|
</div>
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := smtpClient.Send(ctx, message); err != nil {
|
||||||
|
log.Fatalf("Failed to send email: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,366 @@
|
|||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 测试内容类型检测
|
||||||
|
func TestContentTypeDetection(t *testing.T) {
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
body string
|
||||||
|
expected interfaces.ContentType
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "HTML with html tag",
|
||||||
|
body: "<html><body>Test</body></html>",
|
||||||
|
expected: interfaces.ContentTypeHTML,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HTML with div tag",
|
||||||
|
body: "<div>Test content</div>",
|
||||||
|
expected: interfaces.ContentTypeHTML,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HTML with img tag",
|
||||||
|
body: "Check this image: <img src='test.jpg'>",
|
||||||
|
expected: interfaces.ContentTypeHTML,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Plain text",
|
||||||
|
body: "This is plain text without any HTML tags.",
|
||||||
|
expected: interfaces.ContentTypeText,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Text with angle brackets",
|
||||||
|
body: "Price: 100 < 200, Quality: A > B",
|
||||||
|
expected: interfaces.ContentTypeText,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := smtpClient.detectContentType(tt.body)
|
||||||
|
if result != tt.expected {
|
||||||
|
t.Errorf("Expected %s, got %s for body: %s", tt.expected, result, tt.body)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试邮件头构建
|
||||||
|
func TestBuildHeaders(t *testing.T) {
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
smtpClient.SetOption(context.Background(), interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{}))
|
||||||
|
boundary := "test-boundary"
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
message interfaces.Message
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "HTML with inline images",
|
||||||
|
message: interfaces.Message{
|
||||||
|
Subject: "Test Subject",
|
||||||
|
InlineImage: []interfaces.Attachment{{Name: "test.jpg"}},
|
||||||
|
},
|
||||||
|
expected: "multipart/related",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HTML with attachments",
|
||||||
|
message: interfaces.Message{
|
||||||
|
Subject: "Test Subject",
|
||||||
|
Attachment: []interfaces.Attachment{{Name: "test.pdf"}},
|
||||||
|
},
|
||||||
|
expected: "multipart/mixed",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Simple message",
|
||||||
|
message: interfaces.Message{
|
||||||
|
Subject: "Test Subject",
|
||||||
|
},
|
||||||
|
expected: "multipart/alternative",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
headers := smtpClient.buildHeaders(tt.message, boundary)
|
||||||
|
contentType := headers["Content-Type"]
|
||||||
|
if !strings.Contains(contentType, tt.expected) {
|
||||||
|
t.Errorf("Expected Content-Type to contain %s, got %s", tt.expected, contentType)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试附件类型处理
|
||||||
|
func TestAttachmentTypes(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
attachment interfaces.Attachment
|
||||||
|
expectCID bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "File attachment",
|
||||||
|
attachment: interfaces.Attachment{
|
||||||
|
Name: "document.pdf",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
expectCID: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Inline image",
|
||||||
|
attachment: interfaces.Attachment{
|
||||||
|
Name: "image.jpg",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
CID: "test-image",
|
||||||
|
},
|
||||||
|
expectCID: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if tt.expectCID && tt.attachment.CID == "" {
|
||||||
|
t.Error("Expected CID for inline attachment")
|
||||||
|
}
|
||||||
|
if !tt.expectCID && tt.attachment.Type == interfaces.AttachmentTypeFile {
|
||||||
|
// File attachments should not have CID
|
||||||
|
if tt.attachment.CID != "" {
|
||||||
|
t.Error("File attachment should not have CID")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试消息验证
|
||||||
|
func TestMessageValidation2(t *testing.T) {
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 配置SMTP客户端
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
message interfaces.Message
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Valid HTML message",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "<h1>Test</h1>",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid text message",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "Plain text",
|
||||||
|
BodyType: interfaces.ContentTypeText,
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Message with inline images",
|
||||||
|
message: interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "<img src='cid:test'>",
|
||||||
|
InlineImage: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Name: "test.jpg",
|
||||||
|
CID: "test",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Empty recipients",
|
||||||
|
message: interfaces.Message{
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "Test body",
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := smtpClient.validateMessage(tt.message)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("validateMessage() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试多部分邮件构建
|
||||||
|
func TestMultipartEmailConstruction2(t *testing.T) {
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to configure SMTP: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Multipart Test",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
Body: "<h1>HTML Content</h1><img src='cid:test-img'>",
|
||||||
|
TextBody: "Plain text version",
|
||||||
|
InlineImage: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Data: []byte("fake-image-data"),
|
||||||
|
ContentType: "image/jpeg",
|
||||||
|
Name: "test.jpg",
|
||||||
|
CID: "test-img",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Attachment: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Data: []byte("fake-pdf-data"),
|
||||||
|
ContentType: "application/pdf",
|
||||||
|
Name: "document.pdf",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试邮件构建(不实际发送)
|
||||||
|
boundary := "test-boundary"
|
||||||
|
headers := smtpClient.buildHeaders(message, boundary)
|
||||||
|
|
||||||
|
// 验证Content-Type
|
||||||
|
contentType := headers["Content-Type"]
|
||||||
|
if !strings.Contains(contentType, "multipart/related") {
|
||||||
|
t.Errorf("Expected multipart/related for message with inline images, got %s", contentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证必要的头部字段
|
||||||
|
requiredHeaders := []string{"From", "To", "Subject", "Date", "MIME-Version", "Content-Type"}
|
||||||
|
for _, header := range requiredHeaders {
|
||||||
|
if _, exists := headers[header]; !exists {
|
||||||
|
t.Errorf("Missing required header: %s", header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试字节数据附件
|
||||||
|
func TestByteDataAttachment(t *testing.T) {
|
||||||
|
attachment := interfaces.Attachment{
|
||||||
|
Data: []byte("test file content"),
|
||||||
|
ContentType: "text/plain",
|
||||||
|
Name: "test.txt",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(attachment.Data) == 0 {
|
||||||
|
t.Error("Attachment data should not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if attachment.ContentType != "text/plain" {
|
||||||
|
t.Errorf("Expected content type text/plain, got %s", attachment.ContentType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基准测试:HTML邮件构建性能
|
||||||
|
func BenchmarkHTMLEmailConstruction(b *testing.B) {
|
||||||
|
smtpClient := NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
message := interfaces.Message{
|
||||||
|
To: []string{"test@example.com"},
|
||||||
|
Subject: "Benchmark Test",
|
||||||
|
BodyType: interfaces.ContentTypeHTML,
|
||||||
|
Body: "<h1>Benchmark</h1><p>Performance test content</p>",
|
||||||
|
TextBody: "Benchmark - Performance test content",
|
||||||
|
InlineImage: []interfaces.Attachment{
|
||||||
|
{
|
||||||
|
Data: make([]byte, 1024), // 1KB fake image
|
||||||
|
ContentType: "image/jpeg",
|
||||||
|
Name: "test.jpg",
|
||||||
|
CID: "test-img",
|
||||||
|
Type: interfaces.AttachmentTypeInline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
boundary := "benchmark-boundary"
|
||||||
|
headers := smtpClient.buildHeaders(message, boundary)
|
||||||
|
_ = headers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试大附件处理
|
||||||
|
func TestLargeAttachmentHandling(t *testing.T) {
|
||||||
|
// 创建大附件数据(模拟)
|
||||||
|
largeData := make([]byte, 1024*1024) // 1MB
|
||||||
|
for i := range largeData {
|
||||||
|
largeData[i] = byte(i % 256)
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment := interfaces.Attachment{
|
||||||
|
Data: largeData,
|
||||||
|
ContentType: "application/octet-stream",
|
||||||
|
Name: "large_file.bin",
|
||||||
|
Type: interfaces.AttachmentTypeFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(attachment.Data) != 1024*1024 {
|
||||||
|
t.Errorf("Expected 1MB data, got %d bytes", len(attachment.Data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试是否超过限制(25MB)
|
||||||
|
maxSize := 25 * 1024 * 1024
|
||||||
|
if len(attachment.Data) > maxSize {
|
||||||
|
t.Errorf("Attachment size %d exceeds limit %d", len(attachment.Data), maxSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
+530
-85
@@ -3,56 +3,117 @@ package smtp
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime"
|
||||||
|
"net"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxAttachmentSize = 25 * 1024 * 1024 // 25MB
|
||||||
|
DefaultTimeout = 30 * time.Second
|
||||||
|
MaxRetries = 3
|
||||||
|
)
|
||||||
|
|
||||||
// 邮件发送的封装
|
// 邮件发送的封装
|
||||||
// 1. 支持文本
|
// 1. 支持文本
|
||||||
// 2. 支持文件
|
// 2. 支持文件
|
||||||
|
|
||||||
type Smtp struct {
|
type Smtp struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
// params *interfaces.EmailConfigDataSmtp
|
auth smtp.Auth
|
||||||
auth smtp.Auth
|
timeout time.Duration
|
||||||
// logger loggerx.LoggerInterface
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSmtp() *Smtp {
|
func NewSmtp() *Smtp {
|
||||||
smtp := &Smtp{}
|
smtp := &Smtp{
|
||||||
|
timeout: DefaultTimeout,
|
||||||
|
}
|
||||||
smtp.Options = interfaces.DefaultOptions()
|
smtp.Options = interfaces.DefaultOptions()
|
||||||
smtp.EmailType = interfaces.EmailTypeSmtp
|
smtp.EmailType = interfaces.EmailTypeSmtp
|
||||||
return smtp
|
return smtp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||||
|
|
||||||
for _, o := range opt {
|
for _, o := range opt {
|
||||||
o(&l.Options)
|
o(&l.Options)
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Options.Logger.Infof(ctx, "params:%+v", l.Options.Smtp)
|
|
||||||
|
|
||||||
if l.Options.Smtp == nil {
|
if l.Options.Smtp == nil {
|
||||||
return nil, errors.New("not smtp")
|
return fmt.Errorf("SMTP configuration is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
if err := l.validateConfig(); err != nil {
|
||||||
|
return fmt.Errorf("invalid SMTP config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化认证
|
||||||
l.auth = smtp.PlainAuth("", l.Options.Smtp.Username, l.Options.Smtp.Password, l.Options.Smtp.Host)
|
l.auth = smtp.PlainAuth("", l.Options.Smtp.Username, l.Options.Smtp.Password, l.Options.Smtp.Host)
|
||||||
return l, nil
|
|
||||||
|
// 安全日志输出
|
||||||
|
l.Options.Logger.Infof(ctx, "SMTP configured - Host:%s Port:%s Username:%s",
|
||||||
|
l.Options.Smtp.Host, l.Options.Smtp.Port, l.Options.Smtp.Username)
|
||||||
|
|
||||||
|
l.IsSet = true
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
||||||
if l.Options.Smtp == nil {
|
if !l.IsSet {
|
||||||
return errors.New("not init")
|
return fmt.Errorf("SMTP not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证消息
|
||||||
|
if err := l.validateMessage(message); err != nil {
|
||||||
|
return fmt.Errorf("invalid message: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试机制
|
||||||
|
var lastErr error
|
||||||
|
for i := 0; i < MaxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
l.Options.Logger.Infof(ctx, "Retrying email send, attempt %d/%d", i+1, MaxRetries)
|
||||||
|
time.Sleep(time.Duration(i) * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据端口和SSL配置选择连接方式
|
||||||
|
switch l.Options.Smtp.Port {
|
||||||
|
case "465":
|
||||||
|
lastErr = l.sendWithTLS(ctx, message)
|
||||||
|
case "587":
|
||||||
|
lastErr = l.sendWithSTARTTLS(ctx, message)
|
||||||
|
default:
|
||||||
|
if l.Options.Smtp.IsSSL {
|
||||||
|
lastErr = l.sendWithTLS(ctx, message)
|
||||||
|
} else {
|
||||||
|
lastErr = l.sendPlain(ctx, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if lastErr == nil {
|
||||||
|
l.Options.Logger.Infof(ctx, "Email sent successfully to %v", message.To)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Options.Logger.Errorf(ctx, "Send attempt %d failed: %v", i+1, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("failed to send email after %d attempts: %w", MaxRetries, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Smtp) sendPlain(ctx context.Context, message interfaces.Message) error {
|
||||||
// .Auth()
|
// .Auth()
|
||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
boundary := "YunBoundaryYun"
|
boundary := "YunBoundaryYun"
|
||||||
@@ -67,48 +128,18 @@ func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(message.To) > 0 {
|
if len(message.To) > 0 {
|
||||||
str := ""
|
Header["To"] = l.formatEmailAddresses(message.To)
|
||||||
for _, val := range message.To {
|
|
||||||
name := ""
|
|
||||||
s := strings.Split(val, "@")
|
|
||||||
if len(s) > 0 {
|
|
||||||
name = s[0]
|
|
||||||
}
|
|
||||||
str = str + "," + name + "<" + val + ">"
|
|
||||||
}
|
|
||||||
Header["To"] = strings.Trim(str, ",")
|
|
||||||
// Header["To"] = strings.Join(message.To, ",")
|
|
||||||
}
|
}
|
||||||
if len(message.Cc) > 0 {
|
if len(message.Cc) > 0 {
|
||||||
str := ""
|
Header["Cc"] = l.formatEmailAddresses(message.Cc)
|
||||||
for _, val := range message.Cc {
|
|
||||||
name := ""
|
|
||||||
s := strings.Split(val, "@")
|
|
||||||
if len(s) > 0 {
|
|
||||||
name = s[0]
|
|
||||||
}
|
|
||||||
str = str + "," + name + "<" + val + ">"
|
|
||||||
}
|
|
||||||
Header["Cc"] = strings.Trim(str, ",")
|
|
||||||
// Header["Cc"] = strings.Join(message.Cc, ",")
|
|
||||||
}
|
}
|
||||||
if len(message.Bcc) > 0 {
|
if len(message.Bcc) > 0 {
|
||||||
str := ""
|
Header["Bcc"] = l.formatEmailAddresses(message.Bcc)
|
||||||
for _, val := range message.Bcc {
|
|
||||||
name := ""
|
|
||||||
s := strings.Split(val, "@")
|
|
||||||
if len(s) > 0 {
|
|
||||||
name = s[0]
|
|
||||||
}
|
|
||||||
str = str + "," + name + "<" + val + ">"
|
|
||||||
}
|
|
||||||
Header["Bcc"] = strings.Trim(str, ",")
|
|
||||||
// Header["Bcc"] = strings.Join(message.Bcc, ",")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Header["Subject"] = message.Subject
|
Header["Subject"] = message.Subject
|
||||||
Header["Content-Type"] = "multipart/mixed; charset=UTF-8; boundary=" + boundary
|
Header["Content-Type"] = "multipart/mixed; charset=UTF-8; boundary=" + boundary
|
||||||
Header["Date"] = time.Now().String()
|
Header["Date"] = time.Now().Format(time.RFC1123Z)
|
||||||
Header["Reply-To"] = message.ReplyTo
|
Header["Reply-To"] = message.ReplyTo
|
||||||
|
|
||||||
Header["X-Priority"] = "3"
|
Header["X-Priority"] = "3"
|
||||||
@@ -128,58 +159,472 @@ func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
|||||||
buffer.WriteString(body)
|
buffer.WriteString(body)
|
||||||
|
|
||||||
for _, value := range message.Attachment {
|
for _, value := range message.Attachment {
|
||||||
newBuf := bytes.NewBuffer(nil)
|
if err := l.writeAttachment(buffer, boundary, value, ctx); err != nil {
|
||||||
err := l.writeFile(newBuf, value.Content)
|
l.Options.Logger.Errorf(ctx, "Failed to process attachment %s: %v", value.Name, err)
|
||||||
if err != nil {
|
|
||||||
fmt.Println("file err:", err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
f_name := path.Base(value.Content)
|
|
||||||
attachment := "--" + boundary + "\r\n"
|
|
||||||
attachment += "Content-Transfer-Encoding:base64\r\n"
|
|
||||||
attachment += "Content-Disposition:attachment;filename=" + f_name + "\r\n"
|
|
||||||
attachment += "Content-Type: application/octet-stream;charset=utf-8;name=" + f_name + "\r\n"
|
|
||||||
// attachment += "Contment-Type:" + message.attachment.contentType + ";name=\"" + message.attachment.name + "\"\r\n"
|
|
||||||
buffer.WriteString(attachment)
|
|
||||||
|
|
||||||
buffer.WriteString(newBuf.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.WriteString("\r\n--" + boundary + "--\r\n")
|
buffer.WriteString("\r\n--" + boundary + "--\r\n")
|
||||||
b := buffer.Bytes()
|
b := buffer.Bytes()
|
||||||
err := smtp.SendMail(l.Options.Smtp.Host+":"+l.Options.Smtp.Port, l.auth, l.Options.Smtp.Username, message.To, b)
|
|
||||||
|
// 合并所有收件人
|
||||||
|
allRecipients := append(append(message.To, message.Cc...), message.Bcc...)
|
||||||
|
err := smtp.SendMail(l.Options.Smtp.Host+":"+l.Options.Smtp.Port, l.auth, l.Options.Smtp.Username, allRecipients, b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 格式化header
|
func (l *Smtp) sendWithTLS(ctx context.Context, message interfaces.Message) error {
|
||||||
func (l *Smtp) writeHeader(buffer *bytes.Buffer, Header map[string]string) string {
|
// 构建完整邮件内容
|
||||||
header := ""
|
buffer := bytes.NewBuffer(nil)
|
||||||
// header := "Content-Type: multipart/mixed;charset=UTF-8;boundary=\"YunBoundaryYun\" \r\n"
|
boundary := "YunBoundaryYun"
|
||||||
for key, value := range Header {
|
|
||||||
if value != "" {
|
// 构建邮件头
|
||||||
header += key + ": " + value + "\r\n"
|
headers := l.buildHeaders(message, boundary)
|
||||||
|
l.writeHeader(buffer, headers)
|
||||||
|
|
||||||
|
// 构建邮件体
|
||||||
|
l.writeBody(buffer, boundary, message)
|
||||||
|
|
||||||
|
// 处理内嵌图片
|
||||||
|
for _, inlineImg := range message.InlineImage {
|
||||||
|
if err := l.writeAttachment(buffer, boundary, inlineImg, ctx); err != nil {
|
||||||
|
l.Options.Logger.Errorf(ctx, "Failed to process inline image %s: %v", inlineImg.Name, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
header += "\r\n"
|
|
||||||
buffer.WriteString(header)
|
// 处理普通附件
|
||||||
return header
|
for _, attachment := range message.Attachment {
|
||||||
|
if err := l.writeAttachment(buffer, boundary, attachment, ctx); err != nil {
|
||||||
|
l.Options.Logger.Errorf(ctx, "Failed to process attachment %s: %v", attachment.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString("\r\n--" + boundary + "--\r\n")
|
||||||
|
|
||||||
|
// 建立TLS连接
|
||||||
|
if l.Options.Smtp.Port == "" {
|
||||||
|
l.Options.Smtp.Port = "465"
|
||||||
|
}
|
||||||
|
host := l.Options.Smtp.Host + ":" + l.Options.Smtp.Port
|
||||||
|
|
||||||
|
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: l.timeout}, "tcp", host, &tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
ServerName: l.Options.Smtp.Host,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("TLS connection failed: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
client, err := smtp.NewClient(conn, l.Options.Smtp.Host)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("SMTP client creation failed: %w", err)
|
||||||
|
}
|
||||||
|
defer client.Quit()
|
||||||
|
|
||||||
|
if err := client.Auth(l.auth); err != nil {
|
||||||
|
return fmt.Errorf("authentication failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Mail(l.Options.Smtp.Username); err != nil {
|
||||||
|
return fmt.Errorf("set sender failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加所有收件人
|
||||||
|
allRecipients := append(append(message.To, message.Cc...), message.Bcc...)
|
||||||
|
for _, addr := range allRecipients {
|
||||||
|
if err := client.Rcpt(addr); err != nil {
|
||||||
|
return fmt.Errorf("set recipient %s failed: %w", addr, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := client.Data()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("data command failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(buffer.Bytes()); err != nil {
|
||||||
|
return fmt.Errorf("write message failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 格式化文件
|
// STARTTLS方式发送(587端口)
|
||||||
func (l *Smtp) writeFile(buffer *bytes.Buffer, fileName string) error {
|
func (l *Smtp) sendWithSTARTTLS(ctx context.Context, message interfaces.Message) error {
|
||||||
file, err := os.ReadFile(fileName)
|
// 构建完整邮件内容
|
||||||
if err != nil {
|
buffer := bytes.NewBuffer(nil)
|
||||||
return err
|
boundary := "YunBoundaryYun"
|
||||||
}
|
|
||||||
payload := make([]byte, base64.StdEncoding.EncodedLen(len(file)))
|
// 构建邮件头
|
||||||
base64.StdEncoding.Encode(payload, file)
|
headers := l.buildHeaders(message, boundary)
|
||||||
buffer.WriteString("\r\n")
|
l.writeHeader(buffer, headers)
|
||||||
for index, line := 0, len(payload); index < line; index++ {
|
|
||||||
buffer.WriteByte(payload[index])
|
// 构建邮件体
|
||||||
if (index+1)%76 == 0 {
|
l.writeBody(buffer, boundary, message)
|
||||||
buffer.WriteString("\r\n")
|
|
||||||
|
// 处理内嵌图片
|
||||||
|
for _, inlineImg := range message.InlineImage {
|
||||||
|
if err := l.writeAttachment(buffer, boundary, inlineImg, ctx); err != nil {
|
||||||
|
l.Options.Logger.Errorf(ctx, "Failed to process inline image %s: %v", inlineImg.Name, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理普通附件
|
||||||
|
for _, attachment := range message.Attachment {
|
||||||
|
if err := l.writeAttachment(buffer, boundary, attachment, ctx); err != nil {
|
||||||
|
l.Options.Logger.Errorf(ctx, "Failed to process attachment %s: %v", attachment.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString("\r\n--" + boundary + "--\r\n")
|
||||||
|
|
||||||
|
// 建立普通TCP连接
|
||||||
|
if l.Options.Smtp.Port == "" {
|
||||||
|
l.Options.Smtp.Port = "587"
|
||||||
|
}
|
||||||
|
host := l.Options.Smtp.Host + ":" + l.Options.Smtp.Port
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout("tcp", host, l.timeout)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("TCP connection failed: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
client, err := smtp.NewClient(conn, l.Options.Smtp.Host)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("SMTP client creation failed: %w", err)
|
||||||
|
}
|
||||||
|
defer client.Quit()
|
||||||
|
|
||||||
|
// 启动TLS
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
ServerName: l.Options.Smtp.Host,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.StartTLS(tlsConfig); err != nil {
|
||||||
|
return fmt.Errorf("STARTTLS failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Auth(l.auth); err != nil {
|
||||||
|
return fmt.Errorf("authentication failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Mail(l.Options.Smtp.Username); err != nil {
|
||||||
|
return fmt.Errorf("set sender failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加所有收件人
|
||||||
|
allRecipients := append(append(message.To, message.Cc...), message.Bcc...)
|
||||||
|
for _, addr := range allRecipients {
|
||||||
|
if err := client.Rcpt(addr); err != nil {
|
||||||
|
return fmt.Errorf("set recipient %s failed: %w", addr, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := client.Data()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("data command failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(buffer.Bytes()); err != nil {
|
||||||
|
return fmt.Errorf("write message failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证配置
|
||||||
|
func (l *Smtp) validateConfig() error {
|
||||||
|
if l.Options.Smtp.Host == "" {
|
||||||
|
return errors.New("host is required")
|
||||||
|
}
|
||||||
|
if l.Options.Smtp.Username == "" {
|
||||||
|
return errors.New("username is required")
|
||||||
|
}
|
||||||
|
if l.Options.Smtp.Password == "" {
|
||||||
|
return errors.New("password is required")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证消息
|
||||||
|
func (l *Smtp) validateMessage(message interfaces.Message) error {
|
||||||
|
if len(message.To) == 0 {
|
||||||
|
return errors.New("at least one recipient is required")
|
||||||
|
}
|
||||||
|
if message.Subject == "" {
|
||||||
|
return errors.New("subject is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化邮件地址
|
||||||
|
func (l *Smtp) formatEmailAddresses(addresses []string) string {
|
||||||
|
if len(addresses) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var parts []string
|
||||||
|
for _, addr := range addresses {
|
||||||
|
name := strings.Split(addr, "@")[0]
|
||||||
|
parts = append(parts, fmt.Sprintf("%s<%s>", name, addr))
|
||||||
|
}
|
||||||
|
return strings.Join(parts, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建邮件头
|
||||||
|
func (l *Smtp) buildHeaders(message interfaces.Message, boundary string) map[string]string {
|
||||||
|
headers := make(map[string]string)
|
||||||
|
|
||||||
|
if message.Form != "" {
|
||||||
|
headers["From"] = message.Form
|
||||||
|
} else {
|
||||||
|
headers["From"] = l.Options.Smtp.Username
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(message.To) > 0 {
|
||||||
|
headers["To"] = l.formatEmailAddresses(message.To)
|
||||||
|
}
|
||||||
|
if len(message.Cc) > 0 {
|
||||||
|
headers["Cc"] = l.formatEmailAddresses(message.Cc)
|
||||||
|
}
|
||||||
|
if len(message.Bcc) > 0 {
|
||||||
|
headers["Bcc"] = l.formatEmailAddresses(message.Bcc)
|
||||||
|
}
|
||||||
|
|
||||||
|
headers["Subject"] = message.Subject
|
||||||
|
headers["Date"] = time.Now().Format(time.RFC1123Z)
|
||||||
|
headers["MIME-Version"] = "1.0"
|
||||||
|
|
||||||
|
// 根据内容类型和附件情况选择Content-Type
|
||||||
|
if len(message.InlineImage) > 0 {
|
||||||
|
headers["Content-Type"] = "multipart/related; charset=UTF-8; boundary=" + boundary
|
||||||
|
} else if len(message.Attachment) > 0 {
|
||||||
|
headers["Content-Type"] = "multipart/mixed; charset=UTF-8; boundary=" + boundary
|
||||||
|
} else {
|
||||||
|
headers["Content-Type"] = "multipart/alternative; charset=UTF-8; boundary=" + boundary
|
||||||
|
}
|
||||||
|
|
||||||
|
if message.ReplyTo != "" {
|
||||||
|
headers["Reply-To"] = message.ReplyTo
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入邮件头
|
||||||
|
func (l *Smtp) writeHeader(buffer *bytes.Buffer, headers map[string]string) {
|
||||||
|
for key, value := range headers {
|
||||||
|
if value != "" {
|
||||||
|
buffer.WriteString(fmt.Sprintf("%s: %s\r\n", key, value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer.WriteString("\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入邮件体
|
||||||
|
func (l *Smtp) writeBody(buffer *bytes.Buffer, boundary string, message interfaces.Message) {
|
||||||
|
// 检测内容类型
|
||||||
|
bodyType := message.BodyType
|
||||||
|
if bodyType == interfaces.ContentTypeAuto {
|
||||||
|
bodyType = l.detectContentType(message.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有纯文本版本,创建 multipart/alternative
|
||||||
|
if message.TextBody != "" && bodyType == interfaces.ContentTypeHTML {
|
||||||
|
altBoundary := "alt-" + boundary
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", boundary))
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=%s\r\n\r\n", altBoundary))
|
||||||
|
|
||||||
|
// 纯文本版本
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
|
||||||
|
buffer.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
|
||||||
|
buffer.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||||
|
buffer.WriteString(message.TextBody + "\r\n")
|
||||||
|
|
||||||
|
// HTML版本
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
|
||||||
|
buffer.WriteString("Content-Type: text/html; charset=utf-8\r\n")
|
||||||
|
buffer.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||||
|
buffer.WriteString(message.Body + "\r\n")
|
||||||
|
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s--\r\n", altBoundary))
|
||||||
|
} else {
|
||||||
|
// 单一内容类型
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", boundary))
|
||||||
|
if bodyType == interfaces.ContentTypeHTML {
|
||||||
|
buffer.WriteString("Content-Type: text/html; charset=utf-8\r\n")
|
||||||
|
} else {
|
||||||
|
buffer.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
|
||||||
|
}
|
||||||
|
buffer.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||||
|
buffer.WriteString(message.Body + "\r\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测内容类型
|
||||||
|
func (l *Smtp) detectContentType(body string) interfaces.ContentType {
|
||||||
|
if strings.Contains(body, "<html>") || strings.Contains(body, "<HTML>") ||
|
||||||
|
strings.Contains(body, "<body>") || strings.Contains(body, "<BODY>") ||
|
||||||
|
strings.Contains(body, "<div>") || strings.Contains(body, "<p>") ||
|
||||||
|
strings.Contains(body, "<br>") || strings.Contains(body, "<img") {
|
||||||
|
return interfaces.ContentTypeHTML
|
||||||
|
}
|
||||||
|
return interfaces.ContentTypeText
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入附件
|
||||||
|
func (l *Smtp) writeAttachment(buffer *bytes.Buffer, boundary string, attachment interfaces.Attachment, ctx context.Context) error {
|
||||||
|
if attachment.Type == interfaces.AttachmentTypeInline {
|
||||||
|
return l.writeInlineImage(buffer, boundary, attachment, ctx)
|
||||||
|
}
|
||||||
|
return l.writeFileAttachment(buffer, boundary, attachment, ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入普通附件
|
||||||
|
func (l *Smtp) writeFileAttachment(buffer *bytes.Buffer, boundary string, attachment interfaces.Attachment, ctx context.Context) error {
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
var fileName string
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
if len(attachment.Data) > 0 {
|
||||||
|
data = attachment.Data
|
||||||
|
fileName = attachment.Name
|
||||||
|
} else {
|
||||||
|
data, err = l.readAttachmentFile(attachment.Content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fileName = filepath.Base(attachment.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
if attachment.Name != "" {
|
||||||
|
fileName = attachment.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测 MIME 类型
|
||||||
|
mimeType := attachment.ContentType
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = mime.TypeByExtension(filepath.Ext(fileName))
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", boundary))
|
||||||
|
buffer.WriteString("Content-Transfer-Encoding: base64\r\n")
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-Disposition: attachment; filename=%s\r\n", fileName))
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-Type: %s; name=%s\r\n\r\n", mimeType, fileName))
|
||||||
|
|
||||||
|
// base64编码
|
||||||
|
encoded := base64.StdEncoding.EncodeToString(data)
|
||||||
|
for i := 0; i < len(encoded); i += 76 {
|
||||||
|
end := i + 76
|
||||||
|
if end > len(encoded) {
|
||||||
|
end = len(encoded)
|
||||||
|
}
|
||||||
|
buffer.WriteString(encoded[i:end] + "\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入内嵌图片
|
||||||
|
func (l *Smtp) writeInlineImage(buffer *bytes.Buffer, boundary string, attachment interfaces.Attachment, ctx context.Context) error {
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
var fileName string
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
if len(attachment.Data) > 0 {
|
||||||
|
data = attachment.Data
|
||||||
|
fileName = attachment.Name
|
||||||
|
} else {
|
||||||
|
data, err = l.readAttachmentFile(attachment.Content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fileName = filepath.Base(attachment.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
if attachment.Name != "" {
|
||||||
|
fileName = attachment.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测 MIME 类型
|
||||||
|
mimeType := attachment.ContentType
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = mime.TypeByExtension(filepath.Ext(fileName))
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = "image/jpeg" // 默认图片类型
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成 Content-ID
|
||||||
|
cid := attachment.CID
|
||||||
|
if cid == "" {
|
||||||
|
cid = fmt.Sprintf("img_%d_%s", time.Now().UnixNano(), strings.ReplaceAll(fileName, ".", "_"))
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString(fmt.Sprintf("--%s\r\n", boundary))
|
||||||
|
buffer.WriteString("Content-Transfer-Encoding: base64\r\n")
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-Disposition: inline; filename=%s\r\n", fileName))
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-Type: %s; name=%s\r\n", mimeType, fileName))
|
||||||
|
buffer.WriteString(fmt.Sprintf("Content-ID: <%s>\r\n\r\n", cid))
|
||||||
|
|
||||||
|
// base64编码
|
||||||
|
encoded := base64.StdEncoding.EncodeToString(data)
|
||||||
|
for i := 0; i < len(encoded); i += 76 {
|
||||||
|
end := i + 76
|
||||||
|
if end > len(encoded) {
|
||||||
|
end = len(encoded)
|
||||||
|
}
|
||||||
|
buffer.WriteString(encoded[i:end] + "\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取附件文件
|
||||||
|
func (l *Smtp) readAttachmentFile(fileName string) ([]byte, error) {
|
||||||
|
stat, err := os.Stat(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("file stat failed: %w", err)
|
||||||
|
}
|
||||||
|
if stat.Size() > MaxAttachmentSize {
|
||||||
|
return nil, fmt.Errorf("attachment too large: %d bytes (max: %d)", stat.Size(), MaxAttachmentSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.ReadFile(fileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 行包装器,用于base64编码时的换行
|
||||||
|
type lineWrapper struct {
|
||||||
|
w io.Writer
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lw *lineWrapper) Write(p []byte) (n int, err error) {
|
||||||
|
for i, b := range p {
|
||||||
|
if lw.count == 76 {
|
||||||
|
if _, err := lw.w.Write([]byte("\r\n")); err != nil {
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
lw.count = 0
|
||||||
|
}
|
||||||
|
if _, err := lw.w.Write([]byte{b}); err != nil {
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
lw.count++
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSmtpEnhanced(t *testing.T) {
|
||||||
|
smtp := NewSmtp()
|
||||||
|
|
||||||
|
// 测试配置验证
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试空配置
|
||||||
|
err := smtp.SetOption(ctx)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for empty config")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试不完整配置
|
||||||
|
err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
// 缺少用户名和密码
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for incomplete config")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试完整配置
|
||||||
|
err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
IsSSL: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error for valid config: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageValidation(t *testing.T) {
|
||||||
|
smtp := NewSmtp()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 设置有效配置
|
||||||
|
smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||||
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||||
|
Host: "smtp.example.com",
|
||||||
|
Port: "587",
|
||||||
|
Username: "test@example.com",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 测试空收件人
|
||||||
|
err := smtp.validateMessage(interfaces.Message{
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "Test body",
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for empty recipients")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试空主题
|
||||||
|
err = smtp.validateMessage(interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Body: "Test body",
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for empty subject")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试有效消息
|
||||||
|
err = smtp.validateMessage(interfaces.Message{
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Test",
|
||||||
|
Body: "Test body",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error for valid message: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmailAddressFormatting(t *testing.T) {
|
||||||
|
smtp := NewSmtp()
|
||||||
|
|
||||||
|
addresses := []string{"user1@example.com", "user2@test.com"}
|
||||||
|
formatted := smtp.formatEmailAddresses(addresses)
|
||||||
|
|
||||||
|
expected := "user1<user1@example.com>,user2<user2@test.com>"
|
||||||
|
if formatted != expected {
|
||||||
|
t.Errorf("Expected %s, got %s", expected, formatted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeout(t *testing.T) {
|
||||||
|
smtp := NewSmtp()
|
||||||
|
|
||||||
|
// 测试默认超时
|
||||||
|
if smtp.timeout != DefaultTimeout {
|
||||||
|
t.Errorf("Expected default timeout %v, got %v", DefaultTimeout, smtp.timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试自定义超时
|
||||||
|
customTimeout := 60 * time.Second
|
||||||
|
smtp.timeout = customTimeout
|
||||||
|
if smtp.timeout != customTimeout {
|
||||||
|
t.Errorf("Expected custom timeout %v, got %v", customTimeout, smtp.timeout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基准测试
|
||||||
|
func BenchmarkEmailAddressFormatting(b *testing.B) {
|
||||||
|
smtp := NewSmtp()
|
||||||
|
addresses := []string{"user1@example.com", "user2@test.com", "user3@demo.com"}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
smtp.formatEmailAddresses(addresses)
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
-20
@@ -3,8 +3,6 @@ package smtp_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
@@ -12,39 +10,52 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMail(t *testing.T) {
|
func TestMail(t *testing.T) {
|
||||||
sm := smtp.NewSmtp()
|
ini := smtp.NewSmtp()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
// ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||||
Username: "support@email.blueoceanpay.com",
|
// Username: "support@email.blueoceanpay.com",
|
||||||
Password: "SupporT2017",
|
// Password: "SupporT2017",
|
||||||
|
// ReplyTo: "",
|
||||||
|
// Host: "smtpdm-ap-southeast-1.aliyun.com",
|
||||||
|
// Port: "80",
|
||||||
|
// }))
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
err := ini.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||||
|
IsSSL: true,
|
||||||
|
Username: "demo@yunink.net",
|
||||||
|
Password: "aAhVPzHydLoc9Fj8",
|
||||||
ReplyTo: "",
|
ReplyTo: "",
|
||||||
Host: "smtpdm-ap-southeast-1.aliyun.com",
|
Host: "smtp.exmail.qq.com",
|
||||||
Port: "80",
|
Port: "465",
|
||||||
}))
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.Get("https://baidu.com")
|
// req, err := http.Get("https://baidu.com")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
defer req.Body.Close()
|
// defer req.Body.Close()
|
||||||
|
|
||||||
by, err := io.ReadAll(req.Body)
|
// by, err := io.ReadAll(req.Body)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
msg := interfaces.Message{
|
msg := interfaces.Message{
|
||||||
|
Form: "demo@yunink.net",
|
||||||
To: []string{"995116474@qq.com"},
|
To: []string{"995116474@qq.com"},
|
||||||
Cc: []string{"287852692@qq.com"},
|
Cc: []string{"287852692@qq.com"},
|
||||||
Bcc: []string{"1362716835@qq.com"},
|
Bcc: []string{"1362716835@qq.com"},
|
||||||
ReplyTo: "huangxinyun@dreaminglife.cn",
|
ReplyTo: "huangxinyun@dreaminglife.cn",
|
||||||
Subject: "test mail",
|
Subject: "测试邮件",
|
||||||
Body: string(by),
|
Body: "这是测试邮件", //string(by),
|
||||||
Attachment: []interfaces.MessageAttachment{
|
Attachment: []interfaces.Attachment{
|
||||||
// {
|
// {
|
||||||
// Name: "/code/statistic/out.xlsx",
|
// Name: "/code/statistic/out.xlsx",
|
||||||
// ContentType: "",
|
// ContentType: "",
|
||||||
|
|||||||
Reference in New Issue
Block a user