Skip to content

Commit

Permalink
Plain body enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Iscar <[email protected]>
  • Loading branch information
juaismar authored and mapreal19 committed Dec 29, 2020
1 parent 36fd551 commit 3dddd09
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
8 changes: 4 additions & 4 deletions notification/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type Email struct {
Subject, Body, From string
Tos, Ccs, Bccs []string
FromName string //Name of sender ej: M. Bison
Attachements []Attachement
Subject, Body, PlainBody, From string
Tos, Ccs, Bccs []string
FromName string //Name of sender ej: M. Bison
Attachements []Attachement
}

type Attachement struct {
Expand Down
6 changes: 4 additions & 2 deletions notification/mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var _ = Describe("mailer", func() {
email.From = "[email protected]"
email.FromName = "M. Bison"
email.Subject = "Surrender!"
email.Body = "I go to win!"
email.Body = "<b>I go to win!</b>"
email.PlainBody = "I go to win!"
email.Tos = []string{"[email protected]"}
email.Ccs = []string{"[email protected]"}
email.Bccs = []string{"[email protected]"}
Expand All @@ -34,7 +35,8 @@ var _ = Describe("mailer", func() {
Expect(mockEmail.From).To(Equal("[email protected]"))
Expect(mockEmail.FromName).To(Equal("M. Bison"))
Expect(mockEmail.Subject).To(Equal("Surrender!"))
Expect(mockEmail.Body).To(Equal("I go to win!"))
Expect(mockEmail.Body).To(Equal("<b>I go to win!</b>"))
Expect(mockEmail.PlainBody).To(Equal("I go to win!"))
Expect(len(mockEmail.Tos)).To(Equal(1))
Expect(len(mockEmail.Ccs)).To(Equal(1))
Expect(len(mockEmail.Bccs)).To(Equal(1))
Expand Down
2 changes: 1 addition & 1 deletion notification/mailer/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func mockSend(errToReturn error) (sendInterface, *Email) {
r := new(Email)

return func(email Email) error {
*r = Email{email.Subject, email.Body, email.From, email.Tos, email.Ccs, email.Bccs, email.FromName, email.Attachements}
*r = Email{email.Subject, email.Body, email.PlainBody, email.From, email.Tos, email.Ccs, email.Bccs, email.FromName, email.Attachements}
return errToReturn
}, r
}
15 changes: 13 additions & 2 deletions notification/mailer/sengrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func sengridSender(email Email) error {
beego.Info("Sending email through Sendgrid... Recipient: ", email.Tos[0])

fromMail := mail.NewEmail(email.FromName, email.From)
body := mail.NewContent("text/html", email.Body)

m := mail.NewV3Mail()
m.SetFrom(fromMail)
m.AddContent(body)
contents := buildBodies(email)
m.AddContent(contents...)

for _, atachement := range email.Attachements {
encoded := base64.StdEncoding.EncodeToString(atachement.Data)
Expand All @@ -37,6 +37,7 @@ func sengridSender(email Email) error {
p.AddCCs(convertMails(email.Ccs)...)
p.AddBCCs(convertMails(email.Bccs)...)
p.Subject = email.Subject

m.AddPersonalizations(p)

request := sendgrid.GetRequest(
Expand All @@ -58,3 +59,13 @@ func convertMails(addresses []string) []*mail.Email {
}
return mails
}

func buildBodies(email Email) (contents []*mail.Content) {
if email.PlainBody != "" {
contents = append(contents, mail.NewContent("text/plain", email.PlainBody))
}
if email.Body != "" {
contents = append(contents, mail.NewContent("text/html", email.Body))
}
return
}

0 comments on commit 3dddd09

Please sign in to comment.