调整接口
This commit is contained in:
+31
-21
@@ -13,36 +13,48 @@ import (
|
|||||||
dm20151123 "github.com/alibabacloud-go/dm-20151123/v2/client"
|
dm20151123 "github.com/alibabacloud-go/dm-20151123/v2/client"
|
||||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||||
"github.com/alibabacloud-go/tea/tea"
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Aliyun struct {
|
type Aliyun struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
client *dm20151123.Client
|
client *dm20151123.Client
|
||||||
params *interfaces.EmialConfigDataAliyun
|
// params *interfaces.EmialConfigDataAliyun
|
||||||
logger loggerx.LoggerInterface
|
// logger loggerx.LoggerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) {
|
func NewAliyun() *Aliyun {
|
||||||
l.logger.Infof(ctx, "params:%+v", params)
|
aliyun := &Aliyun{}
|
||||||
if params.Aliyun == nil {
|
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")
|
return nil, errors.New("not aliyun")
|
||||||
}
|
}
|
||||||
l.logger.Infof(ctx, "params:%+v", params.Aliyun)
|
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
|
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
|
||||||
config := &openapi.Config{
|
config := &openapi.Config{
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
AccessKeyId: tea.String(params.Aliyun.AccessId),
|
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
AccessKeySecret: tea.String(params.Aliyun.AccessKey),
|
AccessKeySecret: tea.String(l.Options.Aliyun.AccessKey),
|
||||||
}
|
}
|
||||||
if params.Aliyun.Endpoint == "" {
|
if l.Options.Aliyun.Endpoint == "" {
|
||||||
params.Aliyun.Endpoint = "dm.aliyuncs.com"
|
l.Options.Aliyun.Endpoint = "dm.aliyuncs.com"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Dm
|
// 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)
|
result, err := dm20151123.NewClient(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,8 +63,6 @@ func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigDat
|
|||||||
|
|
||||||
return &Aliyun{
|
return &Aliyun{
|
||||||
client: result,
|
client: result,
|
||||||
params: params.Aliyun,
|
|
||||||
logger: logger,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +73,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
|
|||||||
if len(params.To) > 100 {
|
if len(params.To) > 100 {
|
||||||
return errors.New("最多 100 个地址")
|
return errors.New("最多 100 个地址")
|
||||||
}
|
}
|
||||||
if l.params.AccountName == "" {
|
if l.Options.Aliyun.AccountName == "" {
|
||||||
return errors.New("AccountName 必填")
|
return errors.New("AccountName 必填")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +81,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
|
|||||||
|
|
||||||
singleSendMailRequest := &dm20151123.SingleSendMailRequest{}
|
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.ToAddress = tea.String(toAddress) // 目标地址,多个 email 地址可以用逗号分隔,最多 100 个地址(支持邮件组)。
|
||||||
singleSendMailRequest.Subject = tea.String(params.Subject)
|
singleSendMailRequest.Subject = tea.String(params.Subject)
|
||||||
singleSendMailRequest.HtmlBody = tea.String(params.Body)
|
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)
|
resp, err := l.client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
|
||||||
by, _ := json.Marshal(resp)
|
by, _ := json.Marshal(resp)
|
||||||
fmt.Printf("resp:%+v err:%+v", string(by), err)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -104,7 +114,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if tryErr != nil {
|
if tryErr != nil {
|
||||||
l.logger.Errorf(ctx, "err:%+v", tryErr)
|
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
|
||||||
return tryErr
|
return tryErr
|
||||||
|
|
||||||
// var error = &tea.SDKError{}
|
// var error = &tea.SDKError{}
|
||||||
@@ -142,7 +152,7 @@ func (l *Aliyun) SyncStatus(ctx context.Context) (resp []interfaces.EmailSendRec
|
|||||||
// 一次同步一天的数据
|
// 一次同步一天的数据
|
||||||
for {
|
for {
|
||||||
list, next, err := l.getSendStatus(ctx, start)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -205,7 +215,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
|
|||||||
// 复制代码运行请自行打印 API 的返回值
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
resp, _err := l.client.SenderStatisticsDetailByParamWithOptions(senderStatisticsDetailByParamRequest, runtime)
|
resp, _err := l.client.SenderStatisticsDetailByParamWithOptions(senderStatisticsDetailByParamRequest, runtime)
|
||||||
if _err != nil {
|
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
|
return _err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +230,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if tryErr != nil {
|
if tryErr != nil {
|
||||||
l.logger.Errorf(ctx, "err:%+v", tryErr)
|
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
|
||||||
return nil, nil, tryErr
|
return nil, nil, tryErr
|
||||||
|
|
||||||
// var error = &tea.SDKError{}
|
// var error = &tea.SDKError{}
|
||||||
|
|||||||
+6
-11
@@ -2,30 +2,25 @@ package aliyun_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/aliyun"
|
"code.yun.ink/pkg/mailx/aliyun"
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSend(t *testing.T) {
|
func TestSend(t *testing.T) {
|
||||||
aliyun := &aliyun.Aliyun{}
|
aliyun := aliyun.NewAliyun()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
logger := loggerx.NewLogger(ctx)
|
ali, err := aliyun.SetOption(ctx, interfaces.SetAliyun(&interfaces.EmialConfigDataAliyun{
|
||||||
|
|
||||||
ali, err := aliyun.InitEmail(ctx, interfaces.EmailConfigData{
|
|
||||||
Aliyun: &interfaces.EmialConfigDataAliyun{
|
|
||||||
AccessId: "LTAI5tEQ8L8fmDir8udD3CFr",
|
AccessId: "LTAI5tEQ8L8fmDir8udD3CFr",
|
||||||
AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0",
|
AccessKey: "llg9M1U56s2SW5PuerlKPvTB1xYhn0",
|
||||||
Endpoint: "dm.aliyuncs.com",
|
Endpoint: "dm.aliyuncs.com",
|
||||||
AccountName: "test@email.aisz.org", //"test@email.aisz.org",
|
AccountName: "test@email.aisz.org", //"test@email.aisz.org",
|
||||||
ReplyAddress: "287852692@qq.com",
|
ReplyAddress: "287852692@qq.com",
|
||||||
},
|
}))
|
||||||
}, logger)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -35,10 +30,10 @@ func TestSend(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(by))
|
t.Log(string(by))
|
||||||
|
|
||||||
err = ali.Send(ctx, interfaces.Message{
|
err = ali.Send(ctx, interfaces.Message{
|
||||||
To: []string{"huangxinyun@dreaminglife.cn"},
|
To: []string{"995116474@qq.com"},
|
||||||
Subject: "测试主题",
|
Subject: "测试主题",
|
||||||
Body: string(by),
|
Body: string(by),
|
||||||
})
|
})
|
||||||
|
|||||||
+24
-16
@@ -9,40 +9,48 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/ses"
|
"github.com/aws/aws-sdk-go/service/ses"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 不支持变更发信人(必须配置好)
|
// 不支持变更发信人(必须配置好)
|
||||||
|
|
||||||
type Aws struct {
|
type Aws struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
params *interfaces.EmailConfigDataAws
|
// params *interfaces.EmailConfigDataAws
|
||||||
logger loggerx.LoggerInterface
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Aws) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) {
|
func NewAws() *Aws {
|
||||||
l.logger.Infof(ctx, "params:%+v", params)
|
aws := &Aws{}
|
||||||
if params.Aws == nil {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Options.Logger.Infof(ctx, "Aws:%+v", l.Options.Aws)
|
||||||
|
if l.Options.Aws == nil {
|
||||||
return nil, errors.New("not aws")
|
return nil, errors.New("not aws")
|
||||||
}
|
}
|
||||||
l.logger.Infof(ctx, "params:%+v", params.Aws)
|
|
||||||
if params.Aws.Region == "" {
|
if l.Options.Aws.Region == "" {
|
||||||
params.Aws.Region = "ap-northeast-1"
|
l.Options.Aws.Region = "ap-northeast-1"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Aws{
|
return l, nil
|
||||||
params: params.Aws,
|
|
||||||
}, 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.params == nil {
|
if l.Options.Aws == nil {
|
||||||
return errors.New("not init")
|
return errors.New("not init")
|
||||||
}
|
}
|
||||||
// 配置AWS认证信息
|
// 配置AWS认证信息
|
||||||
config := aws.Config{
|
config := aws.Config{
|
||||||
Region: aws.String(l.params.Region), // 设置你的AWS区域
|
Region: aws.String(l.Options.Aws.Region), // 设置你的AWS区域
|
||||||
Credentials: credentials.NewStaticCredentials(l.params.AccessId, l.params.AccessSecret, ""),
|
Credentials: credentials.NewStaticCredentials(l.Options.Aws.AccessId, l.Options.Aws.AccessSecret, ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建AWS会话
|
// 创建AWS会话
|
||||||
@@ -73,7 +81,7 @@ func (l *Aws) Send(ctx context.Context, params interfaces.Message) error {
|
|||||||
Charset: aws.String("UTF-8"),
|
Charset: aws.String("UTF-8"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Source: aws.String(l.params.Sender),
|
Source: aws.String(l.Options.Aws.Sender),
|
||||||
})
|
})
|
||||||
|
|
||||||
// svc.SendRawEmail()
|
// svc.SendRawEmail()
|
||||||
|
|||||||
+4
-8
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"code.yun.ink/pkg/mailx/aws"
|
"code.yun.ink/pkg/mailx/aws"
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"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
|
// 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"
|
// Source: "chenlihan@dreaminglife.cn"
|
||||||
|
|
||||||
|
a := aws.NewAws()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
logger := loggerx.NewLogger(ctx)
|
ini, err := a.SetOption(ctx, interfaces.SetAws(&interfaces.EmailConfigDataAws{
|
||||||
|
|
||||||
mail := aws.Aws{}
|
|
||||||
ini, err := mail.InitEmail(ctx, interfaces.EmailConfigData{
|
|
||||||
Aws: &interfaces.EmailConfigDataAws{
|
|
||||||
AccessId: "AKIAU6GD3MNRHKR4RZG5",
|
AccessId: "AKIAU6GD3MNRHKR4RZG5",
|
||||||
AccessSecret: "GSdGuFbZlcpVHMODlqeIKr07R/BdTBGeurq0s+4l",
|
AccessSecret: "GSdGuFbZlcpVHMODlqeIKr07R/BdTBGeurq0s+4l",
|
||||||
Region: "ap-northeast-1",
|
Region: "ap-northeast-1",
|
||||||
Sender: "chenlihan@dreaminglife.cn",
|
Sender: "chenlihan@dreaminglife.cn",
|
||||||
},
|
}))
|
||||||
}, logger)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package mailx
|
||||||
|
|
||||||
|
type Platform3rdType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Platform3rdTypeAliyun Platform3rdType = "aliyun"
|
||||||
|
Platform3rdTypeAws Platform3rdType = "aws"
|
||||||
|
Platform3rdTypeMailgun Platform3rdType = "mailgun"
|
||||||
|
Platform3rdTypeSmtp Platform3rdType = "smtp"
|
||||||
|
)
|
||||||
@@ -5,23 +5,81 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Email interface {
|
type EmailType string
|
||||||
InitEmail(ctx context.Context, opt ...Option) (Email, error)
|
|
||||||
|
type EmailInterface interface {
|
||||||
|
SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) // 初始化
|
||||||
|
GetEmailType() EmailType
|
||||||
// Send 发送邮件
|
// Send 发送邮件
|
||||||
Send(ctx context.Context, params Message) error
|
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) {
|
type DefaultEmail struct {
|
||||||
return &DefaultEmail{}, errors.New("not implemented")
|
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 {
|
func (l *DefaultEmail) Send(ctx context.Context, params Message) error {
|
||||||
return errors.New("not implemented")
|
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)
|
||||||
|
}
|
||||||
|
|||||||
+24
-5
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type emailOption struct {
|
type emailOption struct {
|
||||||
logger loggerx.LoggerInterface
|
Logger loggerx.LoggerInterface
|
||||||
|
|
||||||
Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp
|
Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp
|
||||||
Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊
|
Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊
|
||||||
@@ -15,10 +15,10 @@ type emailOption struct {
|
|||||||
Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun
|
Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() emailOption {
|
func DefaultOptions() emailOption {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
return emailOption{
|
return emailOption{
|
||||||
logger: loggerx.NewLogger(ctx),
|
Logger: loggerx.NewLogger(ctx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +27,30 @@ type Option func(*emailOption)
|
|||||||
// 设置日志
|
// 设置日志
|
||||||
func SetLogger(logger loggerx.LoggerInterface) Option {
|
func SetLogger(logger loggerx.LoggerInterface) Option {
|
||||||
return func(o *emailOption) {
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+26
-14
@@ -6,39 +6,51 @@ import (
|
|||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"github.com/mailgun/mailgun-go/v4"
|
"github.com/mailgun/mailgun-go/v4"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MailGun struct {
|
type MailGun struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
params *interfaces.EmialConfigDataMailgun
|
// params *interfaces.EmialConfigDataMailgun
|
||||||
mg *mailgun.MailgunImpl
|
mg *mailgun.MailgunImpl
|
||||||
logger loggerx.LoggerInterface
|
// 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")
|
return nil, errors.New("not mailgun")
|
||||||
}
|
}
|
||||||
l.logger.Infof(ctx, "params:%+v", params.Mailgun)
|
|
||||||
mg := mailgun.NewMailgun(params.Mailgun.Domain, params.Mailgun.ApiKey)
|
l.Options.Logger.Infof(ctx, "Mailgun:%+v", l.Options.Mailgun)
|
||||||
return &MailGun{
|
|
||||||
params: params.Mailgun,
|
mg := mailgun.NewMailgun(l.Options.Mailgun.Domain, l.Options.Mailgun.ApiKey)
|
||||||
mg: mg,
|
|
||||||
}, nil
|
l.mg = mg
|
||||||
|
|
||||||
|
return l, 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.params == nil {
|
if l.Options.Mailgun == nil {
|
||||||
return errors.New("not init")
|
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)
|
resp, id, err := l.mg.Send(ctx, message)
|
||||||
if err != nil {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"code.yun.ink/pkg/mailx/mailgun"
|
"code.yun.ink/pkg/mailx/mailgun"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -16,18 +15,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestSendEmail(t *testing.T) {
|
func TestSendEmail(t *testing.T) {
|
||||||
gun := &mailgun.MailGun{}
|
gun := mailgun.NewMailGun()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
logger := loggerx.NewLogger(ctx)
|
ini, err := gun.SetOption(ctx, interfaces.SetMailgun(&interfaces.EmialConfigDataMailgun{
|
||||||
|
|
||||||
ini, err := gun.InitEmail(ctx, interfaces.EmailConfigData{
|
|
||||||
Mailgun: &interfaces.EmialConfigDataMailgun{
|
|
||||||
ApiKey: apikey,
|
ApiKey: apikey,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
},
|
}))
|
||||||
}, logger)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package mailx
|
package mailx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/aliyun"
|
"code.yun.ink/pkg/mailx/aliyun"
|
||||||
"code.yun.ink/pkg/mailx/aws"
|
"code.yun.ink/pkg/mailx/aws"
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
@@ -11,31 +8,22 @@ import (
|
|||||||
"code.yun.ink/pkg/mailx/smtp"
|
"code.yun.ink/pkg/mailx/smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var platform map[consts.Platform3rdType]interfaces.Email
|
var platform *interfaces.DefaultEmailFactory
|
||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
func init() {
|
func init() {
|
||||||
platform = make(map[consts.Platform3rdType]interfaces.Email)
|
platform = interfaces.NewDefaultEmailFactory()
|
||||||
|
|
||||||
// 阿里
|
// 阿里
|
||||||
platform[consts.Platform3rdTypeAliyun] = &aliyun.Aliyun{}
|
platform.Register(aliyun.NewAliyun())
|
||||||
|
|
||||||
// AWS
|
// AWS
|
||||||
platform[consts.Platform3rdTypeAws] = &aws.Aws{}
|
platform.Register(aws.NewAws())
|
||||||
|
|
||||||
// Smtp
|
// Smtp
|
||||||
platform[consts.Platform3rdTypeSmtp] = &smtp.Smtp{}
|
platform.Register(smtp.NewSmtp())
|
||||||
|
|
||||||
// mailgun
|
// 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")
|
|
||||||
}
|
|
||||||
|
|||||||
+22
-14
@@ -13,7 +13,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 邮件发送的封装
|
// 邮件发送的封装
|
||||||
@@ -22,27 +21,36 @@ import (
|
|||||||
|
|
||||||
type Smtp struct {
|
type Smtp struct {
|
||||||
interfaces.DefaultEmail
|
interfaces.DefaultEmail
|
||||||
params *interfaces.EmailConfigDataSmtp
|
// params *interfaces.EmailConfigDataSmtp
|
||||||
auth smtp.Auth
|
auth smtp.Auth
|
||||||
logger loggerx.LoggerInterface
|
// 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")
|
return nil, errors.New("not smtp")
|
||||||
}
|
}
|
||||||
l.logger.Infof(ctx, "params:%+v", params.Smtp)
|
|
||||||
|
|
||||||
m := &Smtp{
|
l.auth = smtp.PlainAuth("", l.Options.Smtp.Username, l.Options.Smtp.Password, l.Options.Smtp.Host)
|
||||||
params: params.Smtp,
|
return l, nil
|
||||||
}
|
|
||||||
m.auth = smtp.PlainAuth("", params.Smtp.Username, params.Smtp.Password, params.Smtp.Host)
|
|
||||||
return m, 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.params == nil {
|
if l.Options.Smtp == nil {
|
||||||
return errors.New("not init")
|
return errors.New("not init")
|
||||||
}
|
}
|
||||||
// .Auth()
|
// .Auth()
|
||||||
@@ -55,7 +63,7 @@ func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
|||||||
if message.Form != "" {
|
if message.Form != "" {
|
||||||
Header["From"] = message.Form
|
Header["From"] = message.Form
|
||||||
} else {
|
} else {
|
||||||
Header["From"] = l.params.Username
|
Header["From"] = l.Options.Smtp.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(message.To) > 0 {
|
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")
|
buffer.WriteString("\r\n--" + boundary + "--\r\n")
|
||||||
b := buffer.Bytes()
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-8
@@ -9,24 +9,19 @@ import (
|
|||||||
|
|
||||||
"code.yun.ink/pkg/mailx/interfaces"
|
"code.yun.ink/pkg/mailx/interfaces"
|
||||||
"code.yun.ink/pkg/mailx/smtp"
|
"code.yun.ink/pkg/mailx/smtp"
|
||||||
"github.com/yuninks/loggerx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMail(t *testing.T) {
|
func TestMail(t *testing.T) {
|
||||||
sm := smtp.Smtp{}
|
sm := smtp.NewSmtp()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
logger := loggerx.NewLogger(ctx)
|
ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||||
|
|
||||||
ini, err := sm.InitEmail(ctx, interfaces.EmailConfigData{
|
|
||||||
Smtp: &interfaces.EmailConfigDataSmtp{
|
|
||||||
Username: "test@email.aisz.org",
|
Username: "test@email.aisz.org",
|
||||||
Password: "FDuSoJKiC9tNSDIG6DFh",
|
Password: "FDuSoJKiC9tNSDIG6DFh",
|
||||||
ReplyTo: "",
|
ReplyTo: "",
|
||||||
Host: "smtpdm.aliyun.com",
|
Host: "smtpdm.aliyun.com",
|
||||||
Port: "80",
|
Port: "80",
|
||||||
},
|
}))
|
||||||
}, logger)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user