From bd63fe12ead80d396047820b42d186941f189e6e Mon Sep 17 00:00:00 2001 From: Juan Iscar Date: Wed, 24 Aug 2022 10:57:11 +0200 Subject: [PATCH 1/2] mailer refactor and fix some mailgun feat Signed-off-by: Juan Iscar --- notification/mailer/mailer.go | 15 ++++++---- notification/mailer/mailgun.go | 52 ++++++++++++++++++++++++++-------- notification/mailer/sengrid.go | 28 +++++++++++------- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/notification/mailer/mailer.go b/notification/mailer/mailer.go index e33e57a..b40ec12 100644 --- a/notification/mailer/mailer.go +++ b/notification/mailer/mailer.go @@ -34,17 +34,22 @@ type sendInterface func(Email) var recorder *Email var sender *emailSender -var apiKey string -func Init(key, provider string) { +type globalConf struct { + ApiKey string +} +func Init(key, provider string) { if envs.IsProduction() { - apiKey = key + + g := globalConf{ + ApiKey: key, + } switch provider { case Provider.MailGun: - sender = newMailgunSender() + sender = newMailgunSender(g) case Provider.SendGrid: - sender = newSengridSender() + sender = newSengridSender(g) } } else { diff --git a/notification/mailer/mailgun.go b/notification/mailer/mailgun.go index cbd7ee4..bc72b89 100644 --- a/notification/mailer/mailgun.go +++ b/notification/mailer/mailgun.go @@ -2,47 +2,75 @@ package mailer import ( "fmt" - "os" "github.com/astaxie/beego" "github.com/mailgun/mailgun-go" ) -func newMailgunSender() *emailSender { - return &emailSender{mailgunSender} +var Region = struct { + EU, US string +}{ + EU: "EU", + US: "US", } -func mailgunSender(email Email) { +type mailGun struct { + globalConf + Region, Domain string +} + +var mailGunConf mailGun + +func SetMailgun(region, domain string) { + mailGunConf.Domain = domain + mailGunConf.Region = region +} + +func newMailgunSender(g globalConf) *emailSender { + mailGunConf.globalConf = g + return &emailSender{mailGunConf.mailgunSender} +} + +func (m *mailGun) mailgunSender(email Email) { beego.Info("Sending email through MailGun... Recipient: ", email.Tos[0]) - mg := mailgun.NewMailgun(os.Getenv("MAILGUN_DOMAIN"), apiKey) + mg := mailgun.NewMailgun(m.Domain, m.ApiKey) + m.setRegion(mg, m.Region) message := mg.NewMessage( - fmt.Sprint("%s<%s>", email.FromName, email.From), + fmt.Sprintf("%s<%s>", email.FromName, email.From), email.Subject, email.PlainBody, email.Tos...) - mailGunAddCCs(message, email.Ccs) - mailGunAddBCCs(message, email.Bccs) - mailGunAddAttachments(message, email.Attachments) + m.addCCs(message, email.Ccs) + m.addBCCs(message, email.Bccs) + m.addAttachments(message, email.Attachments) message.SetHtml(email.Body) mg.Send(message) } -func mailGunAddAttachments(message *mailgun.Message, attachment []Attachment) { +func (m *mailGun) setRegion(mg *mailgun.MailgunImpl, region string) { + switch region { + case Region.EU: + mg.SetAPIBase("https://api.eu.mailgun.net/v3") + case Region.US: + mg.SetAPIBase("https://api.mailgun.net/v3") + } +} +func (m *mailGun) addAttachments(message *mailgun.Message, attachment []Attachment) { for _, att := range attachment { message.AddBufferAttachment(att.Name, att.Data) } } -func mailGunAddCCs(message *mailgun.Message, addresses []string) { +func (m *mailGun) addCCs(message *mailgun.Message, addresses []string) { for _, addr := range addresses { message.AddCC(addr) } } -func mailGunAddBCCs(message *mailgun.Message, addresses []string) { +func (m *mailGun) addBCCs(message *mailgun.Message, addresses []string) { for _, addr := range addresses { message.AddBCC(addr) } diff --git a/notification/mailer/sengrid.go b/notification/mailer/sengrid.go index 3ff1799..f92e9e6 100644 --- a/notification/mailer/sengrid.go +++ b/notification/mailer/sengrid.go @@ -8,18 +8,26 @@ import ( "github.com/sendgrid/sendgrid-go/helpers/mail" ) -func newSengridSender() *emailSender { - return &emailSender{sengridSender} +type sendGrid struct { + globalConf + Region, Domain string } -func sengridSender(email Email) { +var sendGridConf sendGrid + +func newSengridSender(g globalConf) *emailSender { + sendGridConf.globalConf = g + return &emailSender{sendGridConf.sengridSender} +} + +func (s *sendGrid) sengridSender(email Email) { beego.Info("Sending email through Sendgrid... Recipient: ", email.Tos[0]) fromMail := mail.NewEmail(email.FromName, email.From) m := mail.NewV3Mail() m.SetFrom(fromMail) - contents := buildBodies(email) + contents := s.buildBodies(email) m.AddContent(contents...) for _, attachment := range email.Attachments { @@ -33,15 +41,15 @@ func sengridSender(email Email) { } p := mail.NewPersonalization() - p.AddTos(convertMails(email.Tos)...) - p.AddCCs(convertMails(email.Ccs)...) - p.AddBCCs(convertMails(email.Bccs)...) + p.AddTos(s.convertMails(email.Tos)...) + p.AddCCs(s.convertMails(email.Ccs)...) + p.AddBCCs(s.convertMails(email.Bccs)...) p.Subject = email.Subject m.AddPersonalizations(p) request := sendgrid.GetRequest( - apiKey, + s.ApiKey, "/v3/mail/send", "https://api.sendgrid.com", ) @@ -50,7 +58,7 @@ func sengridSender(email Email) { sendgrid.MakeRequestAsync(request) } -func convertMails(addresses []string) []*mail.Email { +func (s *sendGrid) convertMails(addresses []string) []*mail.Email { mails := make([]*mail.Email, len(addresses)) for i, addr := range addresses { mails[i] = mail.NewEmail("", addr) @@ -58,7 +66,7 @@ func convertMails(addresses []string) []*mail.Email { return mails } -func buildBodies(email Email) (contents []*mail.Content) { +func (s *sendGrid) buildBodies(email Email) (contents []*mail.Content) { if email.PlainBody != "" { contents = append(contents, mail.NewContent("text/plain", email.PlainBody)) } From e5df84fb735ce1c1c300759c8c6832035510b2bd Mon Sep 17 00:00:00 2001 From: Juan Iscar Date: Wed, 24 Aug 2022 11:16:32 +0200 Subject: [PATCH 2/2] Return error Signed-off-by: Juan Iscar --- notification/mailer/mailer.go | 5 ++--- notification/mailer/mailgun.go | 8 +++++--- notification/mailer/mock.go | 3 ++- notification/mailer/sengrid.go | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/notification/mailer/mailer.go b/notification/mailer/mailer.go index b40ec12..6769534 100644 --- a/notification/mailer/mailer.go +++ b/notification/mailer/mailer.go @@ -30,7 +30,7 @@ type emailSender struct { send sendInterface } -type sendInterface func(Email) +type sendInterface func(Email) error var recorder *Email var sender *emailSender @@ -79,6 +79,5 @@ func (e *emailSender) Send(email Email) error { ) beego.Info(message) } - e.send(email) - return nil + return e.send(email) } diff --git a/notification/mailer/mailgun.go b/notification/mailer/mailgun.go index bc72b89..4ed8c84 100644 --- a/notification/mailer/mailgun.go +++ b/notification/mailer/mailgun.go @@ -31,7 +31,7 @@ func newMailgunSender(g globalConf) *emailSender { return &emailSender{mailGunConf.mailgunSender} } -func (m *mailGun) mailgunSender(email Email) { +func (m *mailGun) mailgunSender(email Email) error { beego.Info("Sending email through MailGun... Recipient: ", email.Tos[0]) mg := mailgun.NewMailgun(m.Domain, m.ApiKey) m.setRegion(mg, m.Region) @@ -47,9 +47,10 @@ func (m *mailGun) mailgunSender(email Email) { m.addAttachments(message, email.Attachments) message.SetHtml(email.Body) - mg.Send(message) - + _, _, err := mg.Send(message) + return err } + func (m *mailGun) setRegion(mg *mailgun.MailgunImpl, region string) { switch region { case Region.EU: @@ -58,6 +59,7 @@ func (m *mailGun) setRegion(mg *mailgun.MailgunImpl, region string) { mg.SetAPIBase("https://api.mailgun.net/v3") } } + func (m *mailGun) addAttachments(message *mailgun.Message, attachment []Attachment) { for _, att := range attachment { message.AddBufferAttachment(att.Name, att.Data) diff --git a/notification/mailer/mock.go b/notification/mailer/mock.go index bac7f32..bea65bc 100644 --- a/notification/mailer/mock.go +++ b/notification/mailer/mock.go @@ -11,7 +11,8 @@ func mockSender() *emailSender { func mockSend() (sendInterface, *Email) { r := new(Email) - return func(email Email) { + return func(email Email) error { *r = Email{email.Subject, email.Body, email.PlainBody, email.From, email.Tos, email.Ccs, email.Bccs, email.FromName, email.Attachments} + return nil }, r } diff --git a/notification/mailer/sengrid.go b/notification/mailer/sengrid.go index f92e9e6..fcaeb2c 100644 --- a/notification/mailer/sengrid.go +++ b/notification/mailer/sengrid.go @@ -20,7 +20,7 @@ func newSengridSender(g globalConf) *emailSender { return &emailSender{sendGridConf.sengridSender} } -func (s *sendGrid) sengridSender(email Email) { +func (s *sendGrid) sengridSender(email Email) error { beego.Info("Sending email through Sendgrid... Recipient: ", email.Tos[0]) fromMail := mail.NewEmail(email.FromName, email.From) @@ -56,6 +56,7 @@ func (s *sendGrid) sengridSender(email Email) { request.Method = "POST" request.Body = mail.GetRequestBody(m) sendgrid.MakeRequestAsync(request) + return nil } func (s *sendGrid) convertMails(addresses []string) []*mail.Email {