diff --git a/sendgrid_sender.go b/sendgrid_sender.go index 2551236..ceb453a 100644 --- a/sendgrid_sender.go +++ b/sendgrid_sender.go @@ -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 @@ -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" { diff --git a/sendgrid_sender_test.go b/sendgrid_sender_test.go index 8e04f3f..7b74408 100644 --- a/sendgrid_sender_test.go +++ b/sendgrid_sender_test.go @@ -24,12 +24,12 @@ func Test_build_Mail(t *testing.T) { From: "tatan@test.com", To: []string{"email@test.com", "anotheremail@test.com"}, Bodies: []mail.Body{ - mail.Body{ + { Content: "
Test Content of mail
", ContentType: "text/html", }, - mail.Body{ + { Content: "Test Content of mail", ContentType: "text/plain", }, @@ -42,12 +42,12 @@ func Test_build_Mail(t *testing.T) { From: "", To: []string{"email@test.com", "anotheremail@test.com"}, Bodies: []mail.Body{ - mail.Body{ + { Content: "Test Content of mail
", ContentType: "text/html", }, - mail.Body{ + { Content: "Test Content of mail", ContentType: "text/plain", }, @@ -60,12 +60,12 @@ func Test_build_Mail(t *testing.T) { From: "tatan@test.com", To: []string{"", "anotheremail@test.com"}, Bodies: []mail.Body{ - mail.Body{ + { Content: "Test Content of mail
", ContentType: "text/html", }, - mail.Body{ + { Content: "Test Content of mail", ContentType: "text/plain", }, @@ -79,24 +79,24 @@ func Test_build_Mail(t *testing.T) { From: "tatan@test.com", To: []string{"email@test.com", "anotheremail@test.com"}, Bodies: []mail.Body{ - mail.Body{ + { Content: "Test Content of mail
", 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", @@ -147,24 +147,24 @@ func Test_build_Mail_Custom_Args(t *testing.T) { m.Subject = "Test Mail" m.To = []string{"email@test.com", "anotheremail@test.com"} m.Bodies = []mail.Body{ - mail.Body{ + { Content: "Test Content of mail
", 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", @@ -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 = "digi@charat.com" + m.Subject = "Test Header Mail" + m.To = []string{"email@test.com", "anotheremail@test.com"} + m.Headers = map[string]string{ + "x-provider": "sendgrid", + "x-header": "testing_header", + } + m.Bodies = []mail.Body{ + { + Content: "Test Content of mail
", + 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"]) +}