Skip to content

Commit 8832955

Browse files
authored
Merge pull request #8 from dmuriel/task-custom-headers
Adding custom headers to sendgrid sender
2 parents c32bafb + 47bc4a4 commit 8832955

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

sendgrid_sender.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ func SetCustomArgs(m mail.Message, customArgs CustomArgs) {
7171

7272
func buildMail(m mail.Message) (*smail.SGMailV3, error) {
7373
mm := new(smail.SGMailV3)
74+
7475
from, err := nmail.ParseAddress(m.From)
7576
if err != nil {
7677
return &smail.SGMailV3{}, fmt.Errorf("invalid from (%s): %s", from, err.Error())
7778
}
79+
7880
mm.SetFrom(smail.NewEmail(from.Name, from.Address))
7981
mm.Subject = m.Subject
8082

@@ -109,7 +111,12 @@ func buildMail(m mail.Message) (*smail.SGMailV3, error) {
109111
}
110112
}
111113

114+
for k, v := range m.Headers {
115+
p.SetHeader(k, v)
116+
}
117+
112118
mm.AddPersonalizations(p)
119+
113120
contents := []*smail.Content{}
114121
for _, b := range m.Bodies {
115122
if b.ContentType == "text/plain" {

sendgrid_sender_test.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func Test_build_Mail(t *testing.T) {
2424
From: "tatan@test.com",
2525
To: []string{"email@test.com", "anotheremail@test.com"},
2626
Bodies: []mail.Body{
27-
mail.Body{
27+
{
2828
Content: "<p>Test Content of mail</p>",
2929
ContentType: "text/html",
3030
},
3131

32-
mail.Body{
32+
{
3333
Content: "Test Content of mail",
3434
ContentType: "text/plain",
3535
},
@@ -42,12 +42,12 @@ func Test_build_Mail(t *testing.T) {
4242
From: "",
4343
To: []string{"email@test.com", "anotheremail@test.com"},
4444
Bodies: []mail.Body{
45-
mail.Body{
45+
{
4646
Content: "<p>Test Content of mail</p>",
4747
ContentType: "text/html",
4848
},
4949

50-
mail.Body{
50+
{
5151
Content: "Test Content of mail",
5252
ContentType: "text/plain",
5353
},
@@ -60,12 +60,12 @@ func Test_build_Mail(t *testing.T) {
6060
From: "tatan@test.com",
6161
To: []string{"", "anotheremail@test.com"},
6262
Bodies: []mail.Body{
63-
mail.Body{
63+
{
6464
Content: "<p>Test Content of mail</p>",
6565
ContentType: "text/html",
6666
},
6767

68-
mail.Body{
68+
{
6969
Content: "Test Content of mail",
7070
ContentType: "text/plain",
7171
},
@@ -79,24 +79,24 @@ func Test_build_Mail(t *testing.T) {
7979
From: "tatan@test.com",
8080
To: []string{"email@test.com", "anotheremail@test.com"},
8181
Bodies: []mail.Body{
82-
mail.Body{
82+
{
8383
Content: "<p>Test Content of mail</p>",
8484
ContentType: "text/html",
8585
},
8686

87-
mail.Body{
87+
{
8888
Content: "Test Content of mail",
8989
ContentType: "text/plain",
9090
},
9191
},
9292
Attachments: []mail.Attachment{
93-
mail.Attachment{
93+
{
9494
Name: "test_file.pdf",
9595
Reader: bytes.NewReader([]byte("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")),
9696
ContentType: "application/pdf",
9797
Embedded: false,
9898
},
99-
mail.Attachment{
99+
{
100100
Name: "test_image.png",
101101
Reader: bytes.NewReader([]byte("R29zIGxvdmVzIHlvdQ==")),
102102
ContentType: "image/png",
@@ -147,24 +147,24 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
147147
m.Subject = "Test Mail"
148148
m.To = []string{"email@test.com", "anotheremail@test.com"}
149149
m.Bodies = []mail.Body{
150-
mail.Body{
150+
{
151151
Content: "<p>Test Content of mail</p>",
152152
ContentType: "text/html",
153153
},
154154

155-
mail.Body{
155+
{
156156
Content: "Test Content of mail",
157157
ContentType: "text/plain",
158158
},
159159
}
160160
m.Attachments = []mail.Attachment{
161-
mail.Attachment{
161+
{
162162
Name: "test_file.pdf",
163163
Reader: bytes.NewReader([]byte("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")),
164164
ContentType: "application/pdf",
165165
Embedded: false,
166166
},
167-
mail.Attachment{
167+
{
168168
Name: "test_image.png",
169169
Reader: bytes.NewReader([]byte("R29zIGxvdmVzIHlvdQ==")),
170170
ContentType: "image/png",
@@ -196,3 +196,33 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
196196
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_1"])
197197
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_2"])
198198
}
199+
200+
func Test_build_Mail_Custom_Headers(t *testing.T) {
201+
a := require.New(t)
202+
m := mail.NewMessage()
203+
204+
m.From = "digi@charat.com"
205+
m.Subject = "Test Header Mail"
206+
m.To = []string{"email@test.com", "anotheremail@test.com"}
207+
m.Headers = map[string]string{
208+
"x-provider": "sendgrid",
209+
"x-header": "testing_header",
210+
}
211+
m.Bodies = []mail.Body{
212+
{
213+
Content: "<p>Test Content of mail</p>",
214+
ContentType: "text/html",
215+
},
216+
217+
{
218+
Content: "Test Content of mail",
219+
ContentType: "text/plain",
220+
},
221+
}
222+
223+
mm, err := buildMail(m)
224+
a.NoError(err)
225+
a.Equal(2, len(mm.Personalizations[0].Headers))
226+
a.Equal("sendgrid", mm.Personalizations[0].Headers["x-provider"])
227+
a.Equal("testing_header", mm.Personalizations[0].Headers["x-header"])
228+
}

0 commit comments

Comments
 (0)