Skip to content

Commit

Permalink
Merge pull request #11 from mapreal19/juaismar/refactor/mailer
Browse files Browse the repository at this point in the history
Juaismar/refactor/mailer
  • Loading branch information
juaismar authored Aug 24, 2022
2 parents 9744801 + e5df84f commit 4d522d5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
20 changes: 12 additions & 8 deletions notification/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,26 @@ type emailSender struct {
send sendInterface
}

type sendInterface func(Email)
type sendInterface func(Email) error

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 {
Expand Down Expand Up @@ -74,6 +79,5 @@ func (e *emailSender) Send(email Email) error {
)
beego.Info(message)
}
e.send(email)
return nil
return e.send(email)
}
56 changes: 43 additions & 13 deletions notification/mailer/mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,77 @@ 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) error {
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)
_, _, err := mg.Send(message)
return err
}

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 mailGunAddAttachments(message *mailgun.Message, attachment []Attachment) {

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)
}
Expand Down
3 changes: 2 additions & 1 deletion notification/mailer/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 19 additions & 10 deletions notification/mailer/sengrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) error {
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 {
Expand All @@ -33,32 +41,33 @@ 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",
)
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
sendgrid.MakeRequestAsync(request)
return nil
}

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)
}
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))
}
Expand Down

0 comments on commit 4d522d5

Please sign in to comment.