Skip to content

Commit

Permalink
Merge pull request #7 from tatang26/task-add-custom-args
Browse files Browse the repository at this point in the history
Add custom args to SGMailV3
  • Loading branch information
paganotoni authored Jun 28, 2019
2 parents dda4061 + 0c8a421 commit b7320ad
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,42 @@ func init() {

And then in your mailers you would do the same `sender.Send(m)` as this sender matches buffalos [`mail.Sender`](https://github.com/gobuffalo/buffalo/blob/master/mail/mail.go#L4) interface.

#### Add Custom Args

To add custom args, you must add values using `CustomArgs` type and add it into Message.Data ussing `CustomArgsKey` key

```go
CustomArgsKey = "sendgrid_custom_args_key"
...
type CustomArgs map[string]string
```

#### How to implement custom args

```go
import (
...
ssender "github.com/paganotoni/sendgrid-sender"
)

func main() {
APIKey := envy.Get("SENDGRID_API_KEY", "")
sender = ssender.NewSendgridSender(APIKey)

m := mail.NewMessage()
...
m.Data[ssender.CustomArgsKey] = ssender.CustomArgs{
"custom_arg_0": "custom_value_0",
"custom_arg_1": "custom_value_1",
...
}

if err := sender.Send(m); err != nil{
...
}
}
```

#### Test mode

Whenever the GO_ENV variable is set to be `test` this sender will use [mocksmtp](https://github.com/stanislas-m/mocksmtp) sender to send messages, you can read values in your tests within the property `TestSender` of the SendgridSender.
14 changes: 14 additions & 0 deletions sendgrid_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"github.com/stanislas-m/mocksmtp"
)

const (
// CustomArgsKey is used as default key to search the custom args into Message.Data
CustomArgsKey = "sendgrid_custom_args_key"
)

// CustomArgs is the type that must have Message.Data[CustomArgsKey]
type CustomArgs map[string]string

//SendgridSender implements the Sender interface to be used
//within buffalo mailer generated package.
type SendgridSender struct {
Expand Down Expand Up @@ -73,6 +81,12 @@ func buildMail(m mail.Message) (*smail.SGMailV3, error) {
p.AddTos(smail.NewEmail(to.Name, to.Address))
}

if customArgs, ok := m.Data[CustomArgsKey].(CustomArgs); ok {
for k, v := range customArgs {
p.SetCustomArg(k, v)
}
}

html := smail.NewContent("text/html", m.Bodies[0].Content)
text := smail.NewContent("text/plain", m.Bodies[1].Content)
mm.AddPersonalizations(p)
Expand Down
58 changes: 58 additions & 0 deletions sendgrid_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,61 @@ func Test_build_Mail(t *testing.T) {

}
}

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

m.From = "[email protected]"
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.Data = map[string]interface{}{
CustomArgsKey: CustomArgs{
"custom_key_0": "custom_value_0",
"custom_key_1": "custom_value_1",
"custom_key_2": "[val_2 val_3 val_4]",
},
}
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",
Embedded: true,
},
}

mm, err := buildMail(m)

a.NoError(err)
a.Equal(3, len(mm.Personalizations[0].CustomArgs))
a.Equal("custom_value_0", mm.Personalizations[0].CustomArgs["custom_key_0"])
a.Equal("custom_value_1", mm.Personalizations[0].CustomArgs["custom_key_1"])
a.Equal("[val_2 val_3 val_4]", mm.Personalizations[0].CustomArgs["custom_key_2"])

m.Data = map[string]interface{}{}
mm, err = buildMail(m)

a.NoError(err)
a.Equal(0, len(mm.Personalizations[0].CustomArgs))
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_0"])
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_1"])
a.Equal("", mm.Personalizations[0].CustomArgs["custom_key_2"])
}

0 comments on commit b7320ad

Please sign in to comment.