Skip to content

Commit

Permalink
Merge pull request #8 from dmuriel/task-custom-headers
Browse files Browse the repository at this point in the history
Adding custom headers to sendgrid sender
  • Loading branch information
paganotoni authored Oct 7, 2021
2 parents c32bafb + 47bc4a4 commit 8832955
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
7 changes: 7 additions & 0 deletions sendgrid_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ func SetCustomArgs(m mail.Message, customArgs CustomArgs) {

func buildMail(m mail.Message) (*smail.SGMailV3, error) {
mm := new(smail.SGMailV3)

from, err := nmail.ParseAddress(m.From)
if err != nil {
return &smail.SGMailV3{}, fmt.Errorf("invalid from (%s): %s", from, err.Error())
}

mm.SetFrom(smail.NewEmail(from.Name, from.Address))
mm.Subject = m.Subject

Expand Down Expand Up @@ -109,7 +111,12 @@ func buildMail(m mail.Message) (*smail.SGMailV3, error) {
}
}

for k, v := range m.Headers {
p.SetHeader(k, v)
}

mm.AddPersonalizations(p)

contents := []*smail.Content{}
for _, b := range m.Bodies {
if b.ContentType == "text/plain" {
Expand Down
58 changes: 44 additions & 14 deletions sendgrid_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func Test_build_Mail(t *testing.T) {
From: "[email protected]",
To: []string{"[email protected]", "[email protected]"},
Bodies: []mail.Body{
mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

mail.Body{
{
Content: "Test Content of mail",
ContentType: "text/plain",
},
Expand All @@ -42,12 +42,12 @@ func Test_build_Mail(t *testing.T) {
From: "",
To: []string{"[email protected]", "[email protected]"},
Bodies: []mail.Body{
mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

mail.Body{
{
Content: "Test Content of mail",
ContentType: "text/plain",
},
Expand All @@ -60,12 +60,12 @@ func Test_build_Mail(t *testing.T) {
From: "[email protected]",
To: []string{"", "[email protected]"},
Bodies: []mail.Body{
mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

mail.Body{
{
Content: "Test Content of mail",
ContentType: "text/plain",
},
Expand All @@ -79,24 +79,24 @@ func Test_build_Mail(t *testing.T) {
From: "[email protected]",
To: []string{"[email protected]", "[email protected]"},
Bodies: []mail.Body{
mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

mail.Body{
{
Content: "Test Content of mail",
ContentType: "text/plain",
},
},
Attachments: []mail.Attachment{
mail.Attachment{
{
Name: "test_file.pdf",
Reader: bytes.NewReader([]byte("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")),
ContentType: "application/pdf",
Embedded: false,
},
mail.Attachment{
{
Name: "test_image.png",
Reader: bytes.NewReader([]byte("R29zIGxvdmVzIHlvdQ==")),
ContentType: "image/png",
Expand Down Expand Up @@ -147,24 +147,24 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
m.Subject = "Test Mail"
m.To = []string{"[email protected]", "[email protected]"}
m.Bodies = []mail.Body{
mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

mail.Body{
{
Content: "Test Content of mail",
ContentType: "text/plain",
},
}
m.Attachments = []mail.Attachment{
mail.Attachment{
{
Name: "test_file.pdf",
Reader: bytes.NewReader([]byte("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")),
ContentType: "application/pdf",
Embedded: false,
},
mail.Attachment{
{
Name: "test_image.png",
Reader: bytes.NewReader([]byte("R29zIGxvdmVzIHlvdQ==")),
ContentType: "image/png",
Expand Down Expand Up @@ -196,3 +196,33 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_1"])
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_2"])
}

func Test_build_Mail_Custom_Headers(t *testing.T) {
a := require.New(t)
m := mail.NewMessage()

m.From = "[email protected]"
m.Subject = "Test Header Mail"
m.To = []string{"[email protected]", "[email protected]"}
m.Headers = map[string]string{
"x-provider": "sendgrid",
"x-header": "testing_header",
}
m.Bodies = []mail.Body{
{
Content: "<p>Test Content of mail</p>",
ContentType: "text/html",
},

{
Content: "Test Content of mail",
ContentType: "text/plain",
},
}

mm, err := buildMail(m)
a.NoError(err)
a.Equal(2, len(mm.Personalizations[0].Headers))
a.Equal("sendgrid", mm.Personalizations[0].Headers["x-provider"])
a.Equal("testing_header", mm.Personalizations[0].Headers["x-header"])
}

0 comments on commit 8832955

Please sign in to comment.