Skip to content

Commit

Permalink
Update custom args
Browse files Browse the repository at this point in the history
  • Loading branch information
Walderman committed Jun 27, 2019
1 parent 7415261 commit 0c8a421
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,37 @@ And then in your mailers you would do the same `sender.Send(m)` as this sender m

#### Add Custom Args

If you want to send a mail with `custom args`, you need to add it into `mail.Message.Data` as `map[string]interface{}` this will be into `mail.Message.Personalizations` field :
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"
bmail "github.com/gobuffalo/buffalo/mail"
)

var sender mail.Sender

func init() {
APIKey := envy.Get("SENDGRID_API_KEY", "")
func main() {
APIKey := envy.Get("SENDGRID_API_KEY", "")
sender = ssender.NewSendgridSender(APIKey)
...
message := bmail.NewMessage()
...
customArgs := map[string]interface{}{

m := mail.NewMessage()
...
m.Data[ssender.CustomArgsKey] = ssender.CustomArgs{
"custom_arg_0": "custom_value_0",
"custom_arg_1": 100,
"custom_arg_2": []string{"val_0", "val_1", "val_2"},
"custom_arg_3": map[string]string{
"firstName": "John",
"lastName": "Smith",
"age": "24",
},
"custom_arg_1": "custom_value_1",
...
}

message.Data = customArgs
...

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

Expand Down
14 changes: 12 additions & 2 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,8 +81,10 @@ func buildMail(m mail.Message) (*smail.SGMailV3, error) {
p.AddTos(smail.NewEmail(to.Name, to.Address))
}

for k, v := range m.Data {
p.SetCustomArg(k, fmt.Sprintf("%v", v))
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)
Expand Down
19 changes: 6 additions & 13 deletions sendgrid_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
},
}
m.Data = map[string]interface{}{
"custom_key_0": "custom_value_0",
"custom_key_1": "custom_value_1",
"custom_key_2": []string{"val_2", "val_3", "val_4"},
"custom_key_3": 25,
"custom_key_4": map[string]string{
"firstName": "John",
"lastName": "Smith",
"age": "24",
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{
Expand All @@ -186,14 +182,10 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
mm, err := buildMail(m)

a.NoError(err)
a.Equal(5, len(mm.Personalizations[0].CustomArgs))
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"])
a.Equal("25", mm.Personalizations[0].CustomArgs["custom_key_3"])
a.Contains(mm.Personalizations[0].CustomArgs["custom_key_4"], "firstName:John")
a.Contains(mm.Personalizations[0].CustomArgs["custom_key_4"], "lastName:Smith")
a.Contains(mm.Personalizations[0].CustomArgs["custom_key_4"], "age:24")

m.Data = map[string]interface{}{}
mm, err = buildMail(m)
Expand All @@ -202,4 +194,5 @@ func Test_build_Mail_Custom_Args(t *testing.T) {
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 0c8a421

Please sign in to comment.