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

Update USE_CASES.md #123

Merged
merged 1 commit into from
Oct 4, 2017
Merged
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
207 changes: 207 additions & 0 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This documentation provides examples for specific use cases. Please [open an iss

* [Transactional Templates](#transactional_templates)
* [CustomArgs](#customargs)
* [Substitutions](#substitutions)
* [Sections](#sections)

<a name="transactional_templates"></a>
# Transactional Templates
Expand Down Expand Up @@ -237,3 +239,208 @@ func main() {
}
}
```

<a name="substitutions"></a>
# Substitutions

## With Mail Helper Class

```go
package main

import (
"fmt"
"log"
"os"

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

func main() {
from := mail.NewEmail("Example User", "test@example.com")
subject := "Substitutions can be fun"
to := mail.NewEmail("Example User", "test@example.com")
content := mail.NewContent("text/html", "<html>\n<head>\n\t<title></title>\n</head>\n<body>\nHello -name-,\n<br /><br/>\nI'm glad you are trying out the Substitutions feature!\n<br /><br/>\nI hope you are having a great day in -city- :)\n<br /><br/>\n</body>\n</html>")
m := mail.NewV3MailInit(from, subject, to, content)
m.Personalizations[0].SetSubstitution("-name-", "Example User")
m.Personalizations[0].SetSubstitution("-city-", "Denver")
m.Personalizations[0].SetSubstitution("-user_id-", "343")
m.Personalizations[0].SetCustomArg("user_id", "-user_id-")
m.Personalizations[0].SetCustomArg("city", "-city-")

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)
}
}
```

## Without Mail Helper Class

```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": "test@example.com"
}
],
"subject": "Substitutions can be fun",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver",
"-user_id-": "343"
},
"custom_args": {
"user_id": "-user_id-",
"city": "-city-"
}
}
],
"from": {
"email": "test@example.com"
},
"content": [
{
"type": "text/html",
"value": "<html>\n<head>\n\t<title></title>\n</head>\n<body>\nHello -name-,\n<br /><br/>\nI'm glad you are trying out the Substitutions feature!\n<br /><br/>\nI hope you are having a great day in -city- :)\n<br /><br/>\n</body>\n</html>"
}
]
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
```

<a name="sections"></a>
# Sections

## With Mail Helper Class

```go
package main

import (
"fmt"
"log"
"os"

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

func main() {
from := mail.NewEmail("Example User", "test@example.com")
subject := "Sections can be fun"
to := mail.NewEmail("Example User", "test@example.com")
content := mail.NewContent("text/html", "<html>\n<head>\n\t<title></title>\n</head>\n<body>\n-wel-\n<br /><br/>\nI'm glad you are trying out the Sections feature!\n<br /><br/>\n-gday-\n<br /><br/>\n</body>\n</html>")
m := mail.NewV3MailInit(from, subject, to, content)
m.Personalizations[0].SetSubstitution("-name-", "Example User")
m.Personalizations[0].SetSubstitution("-city-", "Denver")
m.Personalizations[0].SetSubstitution("-wel-", "-welcome-")
m.Personalizations[0].SetSubstitution("-gday-", "-great_day-")

m.AddSection("-welcome-", "Hello -name-,")
m.AddSection("-great_day-", "I hope you are having a great day in -city- :)")

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)
}
}
```

## Without Mail Helper Class

```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": "test@example.com"
}
],
"subject": "Sections can be fun",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver",
"-wel-": "-welcome-",
"-gday-": "-great_day-"
}
}
],
"from": {
"email": "test@example.com"
},
"content": [
{
"type": "text/html",
"value": "<html>\n<head>\n\t<title></title>\n</head>\n<body>\n-wel-\n<br /><br/>\nI'm glad you are trying out the Sections feature!\n<br /><br/>\n-gday-\n<br /><br/>\n</body>\n</html>"
}
],
"sections": {
"section": {
"-welcome-": "Hello -name-,",
"-great_day-": "I hope you are having a great day in -city- :)"
}
}
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
```