From 12da33399bec674569e1d63edf03161045fb4ee4 Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 10 Aug 2025 21:17:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliyun/aliyun.go | 52 +++++++++++++++++------------ aliyun/aliyun_test.go | 25 ++++++-------- aws/aws.go | 44 ++++++++++++++---------- aws/aws_test.go | 20 +++++------ consts.go | 10 ++++++ interfaces/interfaces.go | 72 ++++++++++++++++++++++++++++++++++++---- interfaces/options.go | 29 +++++++++++++--- mailgun/mailgun.go | 42 ++++++++++++++--------- mailgun/mailgun_test.go | 17 ++++------ mailx.go | 24 ++++---------- smtp/smtp.go | 38 ++++++++++++--------- smtp/smtp_test.go | 21 +++++------- 12 files changed, 244 insertions(+), 150 deletions(-) create mode 100644 consts.go diff --git a/aliyun/aliyun.go b/aliyun/aliyun.go index 52d406b..86980a5 100644 --- a/aliyun/aliyun.go +++ b/aliyun/aliyun.go @@ -13,36 +13,48 @@ import ( dm20151123 "github.com/alibabacloud-go/dm-20151123/v2/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" - "github.com/yuninks/loggerx" ) type Aliyun struct { interfaces.DefaultEmail client *dm20151123.Client - params *interfaces.EmialConfigDataAliyun - logger loggerx.LoggerInterface + // params *interfaces.EmialConfigDataAliyun + // logger loggerx.LoggerInterface } -func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) { - l.logger.Infof(ctx, "params:%+v", params) - if params.Aliyun == nil { +func NewAliyun() *Aliyun { + aliyun := &Aliyun{} + aliyun.Options = interfaces.DefaultOptions() + aliyun.EmailType = "Aliyun" + return aliyun +} + +func (l *Aliyun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) { + + for _, o := range opt { + o(&l.Options) + } + + l.Options.Logger.Infof(ctx, "Aliyun:%+v", l.Options.Aliyun) + + if l.Options.Aliyun == nil { return nil, errors.New("not aliyun") } - l.logger.Infof(ctx, "params:%+v", params.Aliyun) + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。 config := &openapi.Config{ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - AccessKeyId: tea.String(params.Aliyun.AccessId), + AccessKeyId: tea.String(l.Options.Aliyun.AccessId), // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - AccessKeySecret: tea.String(params.Aliyun.AccessKey), + AccessKeySecret: tea.String(l.Options.Aliyun.AccessKey), } - if params.Aliyun.Endpoint == "" { - params.Aliyun.Endpoint = "dm.aliyuncs.com" + if l.Options.Aliyun.Endpoint == "" { + l.Options.Aliyun.Endpoint = "dm.aliyuncs.com" } // Endpoint 请参考 https://api.aliyun.com/product/Dm - config.Endpoint = tea.String(params.Aliyun.Endpoint) + config.Endpoint = tea.String(l.Options.Aliyun.Endpoint) result, err := dm20151123.NewClient(config) if err != nil { @@ -51,8 +63,6 @@ func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigDat return &Aliyun{ client: result, - params: params.Aliyun, - logger: logger, }, nil } @@ -63,7 +73,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error { if len(params.To) > 100 { return errors.New("最多 100 个地址") } - if l.params.AccountName == "" { + if l.Options.Aliyun.AccountName == "" { return errors.New("AccountName 必填") } @@ -71,7 +81,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error { singleSendMailRequest := &dm20151123.SingleSendMailRequest{} - singleSendMailRequest.AccountName = tea.String(l.params.AccountName) + 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) @@ -95,7 +105,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error { resp, err := l.client.SingleSendMailWithOptions(singleSendMailRequest, runtime) by, _ := json.Marshal(resp) fmt.Printf("resp:%+v err:%+v", string(by), err) - l.logger.Infof(ctx, "resp:%+v err:%+v", resp, err) + l.Options.Logger.Infof(ctx, "resp:%+v err:%+v", resp, err) if err != nil { return err @@ -104,7 +114,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error { }() if tryErr != nil { - l.logger.Errorf(ctx, "err:%+v", tryErr) + l.Options.Logger.Errorf(ctx, "err:%+v", tryErr) return tryErr // var error = &tea.SDKError{} @@ -142,7 +152,7 @@ func (l *Aliyun) SyncStatus(ctx context.Context) (resp []interfaces.EmailSendRec // 一次同步一天的数据 for { list, next, err := l.getSendStatus(ctx, start) - l.logger.Infof(ctx, "list:%+v next:%+v err:%+v", list, next, err) + l.Options.Logger.Infof(ctx, "list:%+v next:%+v err:%+v", list, next, err) if err != nil { return nil, err } @@ -205,7 +215,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201 // 复制代码运行请自行打印 API 的返回值 resp, _err := l.client.SenderStatisticsDetailByParamWithOptions(senderStatisticsDetailByParamRequest, runtime) if _err != nil { - l.logger.Errorf(ctx, "resp:%+v err:%+v", resp, _err) + l.Options.Logger.Errorf(ctx, "resp:%+v err:%+v", resp, _err) return _err } @@ -220,7 +230,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201 }() if tryErr != nil { - l.logger.Errorf(ctx, "err:%+v", tryErr) + l.Options.Logger.Errorf(ctx, "err:%+v", tryErr) return nil, nil, tryErr // var error = &tea.SDKError{} diff --git a/aliyun/aliyun_test.go b/aliyun/aliyun_test.go index d273444..05f4b5b 100644 --- a/aliyun/aliyun_test.go +++ b/aliyun/aliyun_test.go @@ -2,30 +2,25 @@ package aliyun_test import ( "context" - "fmt" "os" "testing" "code.yun.ink/pkg/mailx/aliyun" "code.yun.ink/pkg/mailx/interfaces" - "github.com/yuninks/loggerx" ) func TestSend(t *testing.T) { - aliyun := &aliyun.Aliyun{} + aliyun := aliyun.NewAliyun() ctx := context.Background() - logger := loggerx.NewLogger(ctx) + ali, err := aliyun.SetOption(ctx, interfaces.SetAliyun(&interfaces.EmialConfigDataAliyun{ + AccessId: "LTAI5tEQ8L8fmDir8udD3CFr", + AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0", + Endpoint: "dm.aliyuncs.com", + AccountName: "test@email.aisz.org", //"test@email.aisz.org", + ReplyAddress: "287852692@qq.com", + })) - ali, err := aliyun.InitEmail(ctx, interfaces.EmailConfigData{ - Aliyun: &interfaces.EmialConfigDataAliyun{ - AccessId: "LTAI5tEQ8L8fmDir8udD3CFr", - AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0", - Endpoint: "dm.aliyuncs.com", - AccountName: "test@email.aisz.org", //"test@email.aisz.org", - ReplyAddress: "287852692@qq.com", - }, - }, logger) if err != nil { t.Fatal(err) } @@ -35,10 +30,10 @@ func TestSend(t *testing.T) { t.Fatal(err) } - fmt.Println(string(by)) + t.Log(string(by)) err = ali.Send(ctx, interfaces.Message{ - To: []string{"huangxinyun@dreaminglife.cn"}, + To: []string{"995116474@qq.com"}, Subject: "测试主题", Body: string(by), }) diff --git a/aws/aws.go b/aws/aws.go index 441dff8..ff56aaf 100644 --- a/aws/aws.go +++ b/aws/aws.go @@ -9,40 +9,48 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ses" - "github.com/yuninks/loggerx" ) // 不支持变更发信人(必须配置好) type Aws struct { interfaces.DefaultEmail - params *interfaces.EmailConfigDataAws - logger loggerx.LoggerInterface + // params *interfaces.EmailConfigDataAws } -func (l *Aws) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) { - l.logger.Infof(ctx, "params:%+v", params) - if params.Aws == nil { - return nil, errors.New("not aws") - } - l.logger.Infof(ctx, "params:%+v", params.Aws) - if params.Aws.Region == "" { - params.Aws.Region = "ap-northeast-1" +func NewAws() *Aws { + aws := &Aws{} + aws.Options = interfaces.DefaultOptions() + aws.EmailType = "Aws" + return aws +} + +func (l *Aws) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) { + + for _, o := range opt { + o(&l.Options) } - return &Aws{ - params: params.Aws, - }, nil + l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws) + if l.Options.Aws == nil { + return nil, errors.New("not aws") + } + + if l.Options.Aws.Region == "" { + l.Options.Aws.Region = "ap-northeast-1" + } + + return l, nil } func (l *Aws) Send(ctx context.Context, params interfaces.Message) error { - if l.params == nil { + if l.Options.Aws == nil { return errors.New("not init") } // 配置AWS认证信息 config := aws.Config{ - Region: aws.String(l.params.Region), // 设置你的AWS区域 - Credentials: credentials.NewStaticCredentials(l.params.AccessId, l.params.AccessSecret, ""), + Region: aws.String(l.Options.Aws.Region), // 设置你的AWS区域 + Credentials: credentials.NewStaticCredentials(l.Options.Aws.AccessId, l.Options.Aws.AccessSecret, ""), } // 创建AWS会话 @@ -73,7 +81,7 @@ func (l *Aws) Send(ctx context.Context, params interfaces.Message) error { Charset: aws.String("UTF-8"), }, }, - Source: aws.String(l.params.Sender), + Source: aws.String(l.Options.Aws.Sender), }) // svc.SendRawEmail() diff --git a/aws/aws_test.go b/aws/aws_test.go index 647276c..d6300ee 100644 --- a/aws/aws_test.go +++ b/aws/aws_test.go @@ -6,7 +6,6 @@ import ( "code.yun.ink/pkg/mailx/aws" "code.yun.ink/pkg/mailx/interfaces" - "github.com/yuninks/loggerx" ) // https://ap-northeast-1.console.aws.amazon.com/ses/home?region=ap-northeast-1#/identities @@ -22,19 +21,16 @@ func TestSend(t *testing.T) { // #发件人 // Source: "chenlihan@dreaminglife.cn" + a := aws.NewAws() + ctx := context.Background() - logger := loggerx.NewLogger(ctx) - - mail := aws.Aws{} - ini, err := mail.InitEmail(ctx, interfaces.EmailConfigData{ - Aws: &interfaces.EmailConfigDataAws{ - AccessId: "AKIAU6GD3MNRHKR4RZG5", - AccessSecret: "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", - Region: "ap-northeast-1", - Sender: "chenlihan@dreaminglife.cn", - }, - }, logger) + ini, err := a.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{ + AccessId: "AKIAU6GD3MNRHKR4RZG5", + AccessSecret: "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", + Region: "ap-northeast-1", + Sender: "chenlihan@dreaminglife.cn", + })) if err != nil { t.Fatal(err) diff --git a/consts.go b/consts.go new file mode 100644 index 0000000..b4a60ef --- /dev/null +++ b/consts.go @@ -0,0 +1,10 @@ +package mailx + +type Platform3rdType string + +const ( + Platform3rdTypeAliyun Platform3rdType = "aliyun" + Platform3rdTypeAws Platform3rdType = "aws" + Platform3rdTypeMailgun Platform3rdType = "mailgun" + Platform3rdTypeSmtp Platform3rdType = "smtp" +) diff --git a/interfaces/interfaces.go b/interfaces/interfaces.go index 3710908..f738189 100644 --- a/interfaces/interfaces.go +++ b/interfaces/interfaces.go @@ -5,23 +5,81 @@ import ( "errors" ) -type Email interface { - InitEmail(ctx context.Context, opt ...Option) (Email, error) +type EmailType string + +type EmailInterface interface { + SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) // 初始化 + GetEmailType() EmailType // Send 发送邮件 Send(ctx context.Context, params Message) error } -type DefaultEmail struct{} +type EmailFactoryInterface interface { + Register(EmailInterface) // 注册一个接口 + SetOption(opt ...Option) // 针对已注册的进行初始化 + GetEmail(EmailType) (EmailInterface, error) + UnRegister(EmailType) +} -func (l *DefaultEmail) InitEmail(ctx context.Context, opt ...Option) (Email, error) { - return &DefaultEmail{}, errors.New("not implemented") +type DefaultEmail struct { + Options emailOption + EmailType EmailType +} + +func NewDefaultEmail() *DefaultEmail { + return &DefaultEmail{ + Options: DefaultOptions(), + EmailType: "DefaultEmail", + } +} + +func (l *DefaultEmail) SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) { + // 深复制l并且返回新的 + newL := *l + + for _, o := range opt { + o(&newL.Options) + } + + return &newL, nil +} + +func (l *DefaultEmail) GetEmailType() EmailType { + return l.EmailType } func (l *DefaultEmail) Send(ctx context.Context, params Message) error { return errors.New("not implemented") } -// 初始化一个接口 +type DefaultEmailFactory struct { + Emails map[EmailType]EmailInterface +} -// 实际执行一个接口 +func NewDefaultEmailFactory() *DefaultEmailFactory { + return &DefaultEmailFactory{ + Emails: make(map[EmailType]EmailInterface), + } +} +func (l *DefaultEmailFactory) Register(email EmailInterface) { + l.Emails[email.GetEmailType()] = email +} + +func (l *DefaultEmailFactory) SetOption(opt ...Option) { + for _, email := range l.Emails { + email.SetOption(context.Background(), opt...) + } +} + +func (l *DefaultEmailFactory) GetEmail(emailType EmailType) (EmailInterface, error) { + email, ok := l.Emails[emailType] + if !ok { + return nil, errors.New("not implemented") + } + return email, nil +} + +func (l *DefaultEmailFactory) UnRegister(emailType EmailType) { + delete(l.Emails, emailType) +} diff --git a/interfaces/options.go b/interfaces/options.go index b96241e..247a121 100644 --- a/interfaces/options.go +++ b/interfaces/options.go @@ -7,7 +7,7 @@ import ( ) type emailOption struct { - logger loggerx.LoggerInterface + Logger loggerx.LoggerInterface Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊 @@ -15,10 +15,10 @@ type emailOption struct { Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun } -func defaultOptions() emailOption { +func DefaultOptions() emailOption { ctx := context.Background() return emailOption{ - logger: loggerx.NewLogger(ctx), + Logger: loggerx.NewLogger(ctx), } } @@ -27,11 +27,30 @@ type Option func(*emailOption) // 设置日志 func SetLogger(logger loggerx.LoggerInterface) Option { return func(o *emailOption) { - o.logger = logger + o.Logger = logger } } +func SetSmtp(smtp *EmailConfigDataSmtp) Option { + return func(o *emailOption) { + o.Smtp = smtp + } +} +func SetAws(aws *EmailConfigDataAws) Option { + return func(o *emailOption) { + o.Aws = aws + } +} +func SetAliyun(aliyun *EmialConfigDataAliyun) Option { + return func(o *emailOption) { + o.Aliyun = aliyun + } +} - +func SetMailgun(mailgun *EmialConfigDataMailgun) Option { + return func(o *emailOption) { + o.Mailgun = mailgun + } +} diff --git a/mailgun/mailgun.go b/mailgun/mailgun.go index 43d334e..f88b146 100644 --- a/mailgun/mailgun.go +++ b/mailgun/mailgun.go @@ -6,39 +6,51 @@ import ( "code.yun.ink/pkg/mailx/interfaces" "github.com/mailgun/mailgun-go/v4" - "github.com/yuninks/loggerx" ) type MailGun struct { interfaces.DefaultEmail - params *interfaces.EmialConfigDataMailgun - mg *mailgun.MailgunImpl - logger loggerx.LoggerInterface + // params *interfaces.EmialConfigDataMailgun + mg *mailgun.MailgunImpl + // logger loggerx.LoggerInterface } -func (l *MailGun) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) { +func NewMailGun() *MailGun { + mailgun := &MailGun{} + mailgun.Options = interfaces.DefaultOptions() + mailgun.EmailType = "MailGun" + return mailgun +} - if params.Mailgun == nil { +func (l *MailGun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) { + + for _, o := range opt { + o(&l.Options) + } + + if l.Options.Mailgun == nil { return nil, errors.New("not mailgun") } - l.logger.Infof(ctx, "params:%+v", params.Mailgun) - mg := mailgun.NewMailgun(params.Mailgun.Domain, params.Mailgun.ApiKey) - return &MailGun{ - params: params.Mailgun, - mg: mg, - }, nil + + l.Options.Logger.Infof(ctx, "Mailgun:%+v", l.Options.Mailgun) + + mg := mailgun.NewMailgun(l.Options.Mailgun.Domain, l.Options.Mailgun.ApiKey) + + l.mg = mg + + return l, nil } func (l *MailGun) Send(ctx context.Context, params interfaces.Message) error { - if l.params == nil { + if l.Options.Mailgun == nil { return errors.New("not init") } - message := l.mg.NewMessage(l.params.Sender, params.Subject, params.Body, params.To...) + message := l.mg.NewMessage(l.Options.Mailgun.Sender, params.Subject, params.Body, params.To...) resp, id, err := l.mg.Send(ctx, message) if err != nil { - l.logger.Errorf(ctx, "Could not send email: %v, resp message: %s, id: %s", err, resp, id) + l.Options.Logger.Errorf(ctx, "Could not send email: %v, resp message: %s, id: %s", err, resp, id) return err } diff --git a/mailgun/mailgun_test.go b/mailgun/mailgun_test.go index bb2581e..98d9400 100644 --- a/mailgun/mailgun_test.go +++ b/mailgun/mailgun_test.go @@ -6,7 +6,6 @@ import ( "code.yun.ink/pkg/mailx/interfaces" "code.yun.ink/pkg/mailx/mailgun" - "github.com/yuninks/loggerx" ) var ( @@ -16,18 +15,14 @@ var ( ) func TestSendEmail(t *testing.T) { - gun := &mailgun.MailGun{} + gun := mailgun.NewMailGun() ctx := context.Background() - logger := loggerx.NewLogger(ctx) - - ini, err := gun.InitEmail(ctx, interfaces.EmailConfigData{ - Mailgun: &interfaces.EmialConfigDataMailgun{ - ApiKey: apikey, - Domain: domain, - Sender: sender, - }, - }, logger) + ini, err := gun.SetOption(ctx, interfaces.SetMailgun(&interfaces.EmialConfigDataMailgun{ + ApiKey: apikey, + Domain: domain, + Sender: sender, + })) if err != nil { t.Fatal(err) diff --git a/mailx.go b/mailx.go index 84d31ca..f11c054 100644 --- a/mailx.go +++ b/mailx.go @@ -1,9 +1,6 @@ package mailx import ( - "context" - "errors" - "code.yun.ink/pkg/mailx/aliyun" "code.yun.ink/pkg/mailx/aws" "code.yun.ink/pkg/mailx/interfaces" @@ -11,31 +8,22 @@ import ( "code.yun.ink/pkg/mailx/smtp" ) -var platform map[consts.Platform3rdType]interfaces.Email +var platform *interfaces.DefaultEmailFactory // 注册 func init() { - platform = make(map[consts.Platform3rdType]interfaces.Email) + platform = interfaces.NewDefaultEmailFactory() // 阿里 - platform[consts.Platform3rdTypeAliyun] = &aliyun.Aliyun{} + platform.Register(aliyun.NewAliyun()) // AWS - platform[consts.Platform3rdTypeAws] = &aws.Aws{} + platform.Register(aws.NewAws()) // Smtp - platform[consts.Platform3rdTypeSmtp] = &smtp.Smtp{} + platform.Register(smtp.NewSmtp()) // mailgun - platform[consts.Platform3rdTypeMailgun] = &mailgun.MailGun{} + platform.Register(mailgun.NewMailGun()) } - -func GetPlatform(ctx context.Context, plat consts.Platform3rdType) (interfaces.Email, error) { - iplat, ok := platform[plat] - if ok { - return iplat, nil - } - - return nil, errors.New("not found") -} diff --git a/smtp/smtp.go b/smtp/smtp.go index cc8f985..065b597 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -13,7 +13,6 @@ import ( "time" "code.yun.ink/pkg/mailx/interfaces" - "github.com/yuninks/loggerx" ) // 邮件发送的封装 @@ -22,27 +21,36 @@ import ( type Smtp struct { interfaces.DefaultEmail - params *interfaces.EmailConfigDataSmtp - auth smtp.Auth - logger loggerx.LoggerInterface + // params *interfaces.EmailConfigDataSmtp + auth smtp.Auth + // logger loggerx.LoggerInterface } -func (l *Smtp) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) { +func NewSmtp() *Smtp { + smtp := &Smtp{} + smtp.Options = interfaces.DefaultOptions() + smtp.EmailType = "Smtp" + return smtp +} - if params.Smtp == nil { +func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) { + + for _, o := range opt { + o(&l.Options) + } + + l.Options.Logger.Infof(ctx, "params:%+v", l.Options.Smtp) + + if l.Options.Smtp == nil { return nil, errors.New("not smtp") } - l.logger.Infof(ctx, "params:%+v", params.Smtp) - m := &Smtp{ - params: params.Smtp, - } - m.auth = smtp.PlainAuth("", params.Smtp.Username, params.Smtp.Password, params.Smtp.Host) - return m, nil + l.auth = smtp.PlainAuth("", l.Options.Smtp.Username, l.Options.Smtp.Password, l.Options.Smtp.Host) + return l, nil } func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error { - if l.params == nil { + if l.Options.Smtp == nil { return errors.New("not init") } // .Auth() @@ -55,7 +63,7 @@ func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error { if message.Form != "" { Header["From"] = message.Form } else { - Header["From"] = l.params.Username + Header["From"] = l.Options.Smtp.Username } if len(message.To) > 0 { @@ -140,7 +148,7 @@ func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error { buffer.WriteString("\r\n--" + boundary + "--\r\n") b := buffer.Bytes() - err := smtp.SendMail(l.params.Host+":"+l.params.Port, l.auth, l.params.Username, message.To, b) + err := smtp.SendMail(l.Options.Smtp.Host+":"+l.Options.Smtp.Port, l.auth, l.Options.Smtp.Username, message.To, b) return err } diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index 110c26d..36c61c0 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -9,24 +9,19 @@ import ( "code.yun.ink/pkg/mailx/interfaces" "code.yun.ink/pkg/mailx/smtp" - "github.com/yuninks/loggerx" ) func TestMail(t *testing.T) { - sm := smtp.Smtp{} + sm := smtp.NewSmtp() ctx := context.Background() - logger := loggerx.NewLogger(ctx) - - ini, err := sm.InitEmail(ctx, interfaces.EmailConfigData{ - Smtp: &interfaces.EmailConfigDataSmtp{ - Username: "test@email.aisz.org", - Password: "FDuSoJKiC9tNSDIG6DFh", - ReplyTo: "", - Host: "smtpdm.aliyun.com", - Port: "80", - }, - }, logger) + ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{ + Username: "test@email.aisz.org", + Password: "FDuSoJKiC9tNSDIG6DFh", + ReplyTo: "", + Host: "smtpdm.aliyun.com", + Port: "80", + })) if err != nil { t.Fatal(err) }