Skip to content

Commit

Permalink
Merge pull request #1 from hdm/preserve-to-and-from-names
Browse files Browse the repository at this point in the history
Preserve the From and To names in from/to
  • Loading branch information
paganotoni authored Nov 10, 2018
2 parents 8af4e5f + a43faba commit 2e00eec
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions sendgrid_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package sender

import (
"errors"
"fmt"
"os"

nmail "net/mail"

"github.com/gobuffalo/buffalo/mail"
sendgrid "github.com/sendgrid/sendgrid-go"
smail "github.com/sendgrid/sendgrid-go/helpers/mail"
Expand All @@ -29,20 +32,28 @@ func (ps SendgridSender) Send(m mail.Message) error {
}

mm := new(smail.SGMailV3)
mm.SetFrom(smail.NewEmail("", m.From))
from, err := nmail.ParseAddress(m.From)
if err != nil {
return fmt.Errorf("invalid from (%s): %s", from, err.Error())
}
mm.SetFrom(smail.NewEmail(from.Name, from.Address))
mm.Subject = m.Subject

p := smail.NewPersonalization()
for _, to := range m.To {
p.AddTos(smail.NewEmail("", to))
for _, toEmail := range m.To {
to, err := nmail.ParseAddress(toEmail)
if err != nil {
return fmt.Errorf("invalid to (%s): %s", toEmail, err.Error())
}
p.AddTos(smail.NewEmail(to.Name, to.Address))
}

html := smail.NewContent("text/html", m.Bodies[0].Content)
text := smail.NewContent("text/plain", m.Bodies[1].Content)
mm.AddPersonalizations(p)
mm.AddContent(text, html)

_, err := ps.client.Send(mm)
_, err = ps.client.Send(mm)
return err
}

Expand Down

0 comments on commit 2e00eec

Please sign in to comment.