Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow personalization of From name and email for each recipient #410

Merged
merged 5 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/helpers/mail/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func kitchenSink() []byte {
mail.NewEmail("Example User", "test6@example.com"),
}
p.AddBCCs(bccs...)
from := mail.NewEmail("Example Sender", "test7@example.com")
p.AddFrom(from)
p.Subject = "Hello World from the Personalized SendGrid Go Library"
p.SetHeader("X-Test", "test")
p.SetHeader("X-Mock", "true")
Expand Down
4 changes: 4 additions & 0 deletions examples/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func v3MailSend() {
"substitutions": {
"id": "substitutions",
"type": "object"
},
"from": {
"email": "bob.doe@example.com",
"name": "Bob Doe"
},
"to": [
{
Expand Down
6 changes: 6 additions & 0 deletions helpers/mail/mail_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SGMailV3 struct {
// Personalization holds mail body struct
type Personalization struct {
To []*Email `json:"to,omitempty"`
From *Email `json:"from,omitempty"`
CC []*Email `json:"cc,omitempty"`
BCC []*Email `json:"bcc,omitempty"`
Subject string `json:"subject,omitempty"`
Expand Down Expand Up @@ -313,6 +314,11 @@ func (p *Personalization) AddTos(to ...*Email) {
p.To = append(p.To, to...)
}

//AddFrom ...
func (p *Personalization) AddFrom(from *Email) {
p.From = from
}

// AddCCs ...
func (p *Personalization) AddCCs(cc ...*Email) {
p.CC = append(p.CC, cc...)
Expand Down
12 changes: 12 additions & 0 deletions helpers/mail/mail_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ func TestV3PersonalizationAddTos(t *testing.T) {
assert.Equal(t, len(tos), len(p.To), fmt.Sprintf("length of To should be %d, got %d", len(tos), len(p.To)))
}

func TestV3PersonalizationAddFrom(t *testing.T) {
address := "test@example.com"
name := "Test User"
from := NewEmail(name, address)

p := NewPersonalization()
p.AddFrom(from)

assert.Equal(t, name, p.From.Name, fmt.Sprintf("name should be %s got %s", name, p.From.Name))
assert.Equal(t, address, p.From.Address, fmt.Sprintf("address should be %s got %s", address, p.From.Address))
}

func TestV3PersonalizationAddCCs(t *testing.T) {
ccs := []*Email{
NewEmail("Example User", "test@example.com"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

### Personalization (with helper) - Sending Two Different Emails to Two Different Groups of Recipients from two different From emails

```go
package main

import (
"fmt"
"log"
"os"

"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)

func main() {
// create new *SGMailV3
m := mail.NewV3Mail()

from := mail.NewEmail("Example Sender 1", "defaultSender@example.com")
content := mail.NewContent("text/html", "<p>Personalizations are awesome!</p>")

m.SetFrom(from)
m.AddContent(content)

// create new *Personalization(s)
personalization1 := mail.NewPersonalization()
personalization2 := mail.NewPersonalization()

// populate `personalization1` with data
//this email will be sent from Example Sender 1
p1_to := mail.NewEmail("Example User 1", "test1@example.com")

personalization1.AddTos(p1_to)
personalization1.Subject = "Having fun learning about personalizations?"

// populate `personalization2` with data
//this email will be sent from Example Sender 2
p2_from :=mail.NewEmail("Example Sender 2", "sender2@example.com")
p2_to := mail.NewEmail("Example User 1", "test1@example.com")

personalization2.AddFrom(p2_from)
personalization2.AddTos(p2_to)
personalization2.Subject = "Personalizations are fun!"

// add `personalization1` and `personalization2` to `m`
m.AddPersonalizations(personalization1, personalization2)

request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(m)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### Personalization (without helper) - Sending Two Different Emails to Two Different Groups of Recipients from two different From email address

```go
package main

import (
"fmt"
"log"
"os"

"github.com/sendgrid/sendgrid-go"
)

func main() {
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = []byte(`{
"personalizations": [{
"to": [{
"email": "recipient1@example.com"
}],
"subject": "YOUR SUBJECT LINE GOES HERE"
}, {
"to": [{
"email": "recipient2@example.com"
}],
"from": {
"email": "sender2@example.com"
},
"subject": "YOUR OTHER SUBJECT LINE GOES HERE"
}],
"from": {
"email": "defaultSender@example.com"
},
"content": [
{
"type": "text/html",
"value": "<p>Personalizations are awesome!</p>"
}
]
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
```
3 changes: 2 additions & 1 deletion use-cases/personalizations-with-mailer-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
* [Sending a Single Email to a Single Recipient with a CC and a BCC](personalization-sending-single-email-single-recipient-with-cc-bcc.md)
* [Sending a Single Email to Multiple Recipients](personalization-sending-single-email-to-multiple-recipients.md)
* [Sending a Single Email to a Single Recipient with Multiple CCs/BCCs](personalization-sending-single-email-to-single-recipients-with-multiple-cc-bcc.md)
* [Sending Two Different Emails to Two Different Groups of Recipients](personalization-sending-two-emails-to-two-groups-recipients.md)
* [Sending Two Different Emails to Two Different Groups of Recipients](personalization-sending-two-emails-to-two-groups-recipients.md)
* [Sending Two Different Emails to Two Different Groups of Recipients from two different From email addresses ](personalization-sending-two-emails-to-two-groups-recipients-from-two-different-from-emails.md)
3 changes: 2 additions & 1 deletion use-cases/personalizations-without-mailer-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
* [Sending a Single Email to a Single Recipient with a CC and a BCC](personalization-without-helper-sending-single-email-single-recipient-with-cc-bcc.md)
* [Sending the same Email to Multiple Recipients](personalization-without-helper-sending-same-email-to-multiple-recipients.md)
* [Sending a Single Email to a Single Recipient with Multiple CCs/BCCs](personalization-without-helper-sending-single-email-to-single-recipients-with-multiple-cc-bcc.md)
* [Sending Two Different Emails to Two Different Groups of Recipients](personalization-without-helper-sending-two-emails-to-two-groups-recipients.md)
* [Sending Two Different Emails to Two Different Groups of Recipients](personalization-without-helper-sending-two-emails-to-two-groups-recipients.md)
* [Sending Two Different Emails to Two Different Groups of Recipients from two different From email addresses](personalization-without-helper-sending-two-emails-to-two-groups-recipients-from-two-different-from-emails.md)