Skip to content

Commit

Permalink
Updated sendgrid binding to add the option to add names for the to an… (
Browse files Browse the repository at this point in the history
#1751)

* Updated sendgrid binding to add the option to add names for the to and from addresses. This commit relates to #1749

Signed-off-by: Adam Rahja <adam@resolve.com>

* Updated sendgrid binding from code suggestion.

Signed-off-by: Adam Rahja <adam@resolve.com>
  • Loading branch information
arahja committed Jun 7, 2022
1 parent 3f85dd6 commit 4322a22
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
48 changes: 38 additions & 10 deletions bindings/twilio/sendgrid/sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ type SendGrid struct {

// Our metadata holds standard email properties.
type sendGridMetadata struct {
APIKey string `json:"apiKey"`
EmailFrom string `json:"emailFrom"`
EmailTo string `json:"emailTo"`
Subject string `json:"subject"`
EmailCc string `json:"emailCc"`
EmailBcc string `json:"emailBcc"`
APIKey string `json:"apiKey"`
EmailFrom string `json:"emailFrom"`
EmailFromName string `json:"emailFromName"`
EmailTo string `json:"emailTo"`
EmailToName string `json:"emailToName"`
Subject string `json:"subject"`
EmailCc string `json:"emailCc"`
EmailBcc string `json:"emailBcc"`
}

// Wrapper to help decode SendGrid API errors.
Expand Down Expand Up @@ -71,7 +73,9 @@ func (sg *SendGrid) parseMetadata(meta bindings.Metadata) (sendGridMetadata, err

// Optional properties, these can be set on a per request basis
sgMeta.EmailTo = meta.Properties["emailTo"]
sgMeta.EmailToName = meta.Properties["emailToName"]
sgMeta.EmailFrom = meta.Properties["emailFrom"]
sgMeta.EmailFromName = meta.Properties["emailFromName"]
sgMeta.Subject = meta.Properties["subject"]
sgMeta.EmailCc = meta.Properties["emailCc"]
sgMeta.EmailBcc = meta.Properties["emailBcc"]
Expand Down Expand Up @@ -105,10 +109,22 @@ func (sg *SendGrid) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*b
// Build email from address, this is required
var fromAddress *mail.Email
if sg.metadata.EmailFrom != "" {
fromAddress = mail.NewEmail("", sg.metadata.EmailFrom)
// Optionally set the email from name
fromName := ""
if sg.metadata.EmailFromName != "" {
fromName = sg.metadata.EmailFromName
}

fromAddress = mail.NewEmail(fromName, sg.metadata.EmailFrom)
}
if req.Metadata["emailFrom"] != "" {
fromAddress = mail.NewEmail("", req.Metadata["emailFrom"])
// Optionally set the email from name
fromName := ""
if req.Metadata["emailFromName"] != "" {
fromName = req.Metadata["emailFromName"]
}

fromAddress = mail.NewEmail(fromName, req.Metadata["emailFrom"])
}
if fromAddress == nil {
return nil, fmt.Errorf("error SendGrid from email not supplied")
Expand All @@ -117,10 +133,22 @@ func (sg *SendGrid) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*b
// Build email to address, this is required
var toAddress *mail.Email
if sg.metadata.EmailTo != "" {
toAddress = mail.NewEmail("", sg.metadata.EmailTo)
// Optionally set the email to name
toName := ""
if sg.metadata.EmailToName != "" {
toName = sg.metadata.EmailToName
}

toAddress = mail.NewEmail(toName, sg.metadata.EmailTo)
}
if req.Metadata["emailTo"] != "" {
toAddress = mail.NewEmail("", req.Metadata["emailTo"])
// Optionally set the email to name
toName := ""
if req.Metadata["emailToName"] != "" {
toName = req.Metadata["emailToName"]
}

toAddress = mail.NewEmail(toName, req.Metadata["emailTo"])
}
if toAddress == nil {
return nil, fmt.Errorf("error SendGrid to email not supplied")
Expand Down
18 changes: 18 additions & 0 deletions bindings/twilio/sendgrid/sendgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ func TestParseMetadata(t *testing.T) {
assert.Equal(t, "hello", sgMeta.Subject)
})
}

func TestParseMetadataWithOptionalNames(t *testing.T) {
logger := logger.NewLogger("test")

t.Run("Has correct metadata", func(t *testing.T) {
m := bindings.Metadata{}
m.Properties = map[string]string{"apiKey": "123", "emailFrom": "test1@example.net", "emailFromName": "test 1", "emailTo": "test2@example.net", "emailToName": "test 2", "subject": "hello"}
r := SendGrid{logger: logger}
sgMeta, err := r.parseMetadata(m)
assert.Nil(t, err)
assert.Equal(t, "123", sgMeta.APIKey)
assert.Equal(t, "test1@example.net", sgMeta.EmailFrom)
assert.Equal(t, "test 1", sgMeta.EmailFromName)
assert.Equal(t, "test2@example.net", sgMeta.EmailTo)
assert.Equal(t, "test 2", sgMeta.EmailToName)
assert.Equal(t, "hello", sgMeta.Subject)
})
}

0 comments on commit 4322a22

Please sign in to comment.