This documentation provides examples for specific use cases. Please open an issue or make a pull request for any use cases you would like us to document here. Thank you!
- Transactional Templates
- Legacy Templates
- CustomArgs
- Personalizations
- Substitutions
- Sections
- Attachments
- How to View Email Statistics
- How to Setup a Domain Whitelabel
For this example, we assume you have created a dynamic transactional template. Following is the dynamic template data we used for testing.
Template ID (replace with your own):
d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c
package main
import (
"fmt"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"os"
)
func main() {
m := mail.NewV3Mail()
address := "[email protected]"
name := "Example User"
e := mail.NewEmail(name, address)
m.SetFrom(e)
m.SetTemplateID("d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c")
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail("Example User", "[email protected]"),
}
p.AddTos(tos...)
p.SetDynamicTemplateData("receipt", "true")
p.SetDynamicTemplateData("total", "$ 239.85")
items := []struct {
text string
image string
price string
}{
{"New Line Sneakers", "https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png", "$ 79.95"},
{"Old Line Sneakers", "https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png", "$ 79.95"},
{"Blue Line Sneakers", "https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png", "$ 79.95"},
}
var itemList []map[string]string
var item map[string]string
for _, v := range items {
item = make(map[string]string)
item["text"] = v.text
item["image"] = v.image
item["price"] = v.price
itemList = append(itemList, item)
}
p.SetDynamicTemplateData("items", itemList)
p.SetDynamicTemplateData("name", "Sample Name")
p.SetDynamicTemplateData("address01", "1234 Fake St.")
p.SetDynamicTemplateData("address02", "Apt. 123")
p.SetDynamicTemplateData("city", "Place")
p.SetDynamicTemplateData("state", "CO")
p.SetDynamicTemplateData("zip", "80202")
m.AddPersonalizations(p)
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
var Body = mail.GetRequestBody(m)
request.Body = Body
response, err := sendgrid.API(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
package main
import (
"fmt"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"os"
)
func main() {
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = []byte(` {
"from": {
"email": "[email protected]"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]"
}
],
"dynamic_template_data":{
"total":"$ 239.85",
"items":[
{
"text":"New Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png",
"price":"$ 79.95"
},
{
"text":"Old Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png",
"price":"$ 79.95"
},
{
"text":"Blue Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png",
"price":"$ 79.95"
}
],
"receipt":true,
"name":"Sample Name",
"address01":"1234 Fake St.",
"address02":"Apt. 123",
"city":"Place",
"state":"CO",
"zip":"80202"
}
}
],
"template_id":"d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c"
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
For this example, we assume you have created a transactional template. Following is the template content we used for testing.
Template ID (replace with your own):
13b8f94f-bcae-4ec6-b752-70d6cb59f932
Email Subject:
<%subject%>
Template Body:
<html>
<head>
<title></title>
</head>
<body>
Hello -name-,
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in -city- :)
<br /><br/>
</body>
</html>
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", "[email protected]")
subject := "I'm replacing the subject tag"
to := mail.NewEmail("Example User", "[email protected]")
content := mail.NewContent("text/html", "I'm replacing the <strong>body tag</strong>")
m := mail.NewV3MailInit(from, subject, to, content)
m.Personalizations[0].SetSubstitution("-name-", "Example User")
m.Personalizations[0].SetSubstitution("-city-", "Denver")
m.SetTemplateID("13b8f94f-bcae-4ec6-b752-70d6cb59f932")
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)
}
}
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": "[email protected]"
}
],
"subject": "I'm replacing the subject tag",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver"
},
}
],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "I'm replacing the <strong>body tag</strong>"
}
],
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
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", "[email protected]")
subject := "CustomArgs can be fun"
to := mail.NewEmail("Example User", "[email protected]")
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 CustomArgs 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].SetCustomArg("user_id", "343")
m.Personalizations[0].SetCustomArg("batch_id", "3")
m.SetCustomArg("campaign", "welcome")
m.SetCustomArg("weekday", "morning")
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)
}
}
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": "[email protected]"
}
],
"subject": "CustomArgs can be fun",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver"
},
"custom_args": {
"user_id": "343",
"batch_id": "3"
}
}
],
"from": {
"email": "[email protected]"
},
"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 CustomArgs feature!\n<br /><br/>\nI hope you are having a great day in -city- :)\n<br /><br/>\n</body>\n</html>"
}
],
"custom_args": {
"campaign": "welcome",
"weekday": "morning"
}
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
// populate `personalization` with data
to := mail.NewEmail("Example User", "[email protected]")
personalization.AddTos(to)
personalization.SetSubstitution("%fname%", "recipient")
personalization.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization.Subject = "Having fun learning about personalizations?"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
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)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
// populate `personalization` with data
to := mail.NewEmail("Example User", "[email protected]")
cc1 := mail.NewEmail("Example CC", "[email protected]")
personalization.AddTos(to)
personalization.AddCCs(cc1)
personalization.SetSubstitution("%fname%", "recipient")
personalization.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization.Subject = "Having fun learning about personalizations?"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
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)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
// populate `personalization` with data
to := mail.NewEmail("Example User", "[email protected]")
cc1 := mail.NewEmail("Example CC", "[email protected]")
bcc1 := mail.NewEmail("Example BCC", "[email protected]")
personalization.AddTos(to)
personalization.AddCCs(cc1)
personalization.AddBCCs(bcc1)
personalization.SetSubstitution("%fname%", "recipient")
personalization.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization.Subject = "Having fun learning about personalizations?"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
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)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
// populate `personalization` with data
to1 := mail.NewEmail("Example User 1", "[email protected]")
to2 := mail.NewEmail("Example User 2", "[email protected]")
to3 := mail.NewEmail("Example User 3", "[email protected]")
personalization.AddTos(to1, to2, to3)
personalization.SetSubstitution("%fname%", "recipient")
personalization.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization.Subject = "Having fun learning about personalizations?"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
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)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
// populate `personalization` with data
to := mail.NewEmail("Example User 1", "[email protected]")
cc1 := mail.NewEmail("Example User 2", "[email protected]")
cc2 := mail.NewEmail("Example User 3", "[email protected]")
cc3 := mail.NewEmail("Example User 3", "[email protected]")
personalization.AddTos(to)
personalization.AddCCs(cc1, cc2, cc3)
personalization.SetSubstitution("%fname%", "recipient")
personalization.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization.Subject = "Having fun learning about personalizations?"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
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)
}
}
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("test", "[email protected]")
content := mail.NewContent("text/html", "<p> %fname% : %CustomerID% - Personalizations are awesome!</p>")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization(s)
personalization1 := mail.NewPersonalization()
personalization2 := mail.NewPersonalization()
// populate `personalization1` with data
p1_to := mail.NewEmail("Example User 1", "[email protected]")
p1_cc1 := mail.NewEmail("Example User 2", "[email protected]")
p1_cc2 := mail.NewEmail("Example User 3", "[email protected]")
p1_cc3 := mail.NewEmail("Example User 3", "[email protected]")
personalization1.AddTos(p1_to)
personalization1.AddCCs(p1_cc1, p1_cc2, p1_cc3)
personalization1.SetSubstitution("%fname%", "recipient")
personalization1.SetSubstitution("%CustomerID%", "CUSTOMER ID GOES HERE")
personalization1.Subject = "Having fun learning about personalizations?"
// populate `personalization2` with data
p2_to := mail.NewEmail("Example User 1", "[email protected]")
p2_cc1 := mail.NewEmail("Example User 2", "[email protected]")
p2_cc2 := mail.NewEmail("Example User 3", "[email protected]")
p2_cc3 := mail.NewEmail("Example User 3", "[email protected]")
personalization2.AddTos(p2_to)
personalization2.AddCCs(p2_cc1, p2_cc2, p2_cc3)
personalization2.SetSubstitution("%fname%", "recipient2")
personalization2.SetSubstitution("%CustomerID%", "55")
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)
}
}
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": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}],
"bcc": [{
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
}
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}, {
"email": "[email protected]"
}, {
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}, {
"email": "[email protected]"
}, {
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}, {
"email": "[email protected]"
}, {
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient",
"%CustomerID%": "CUSTOMER ID GOES HERE"
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}, {
"to": [{
"email": "[email protected]"
}],
"cc": [{
"email": "[email protected]"
}, {
"email": "[email protected]"
}, {
"email": "[email protected]"
}],
"substitutions": {
"%fname%": "recipient2",
"%CustomerID%": 55
},
"subject": "YOUR SUBJECT LINE GOES HERE"
}],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p> %fname% : %CustomerID% - 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)
}
}
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", "[email protected]")
subject := "Substitutions can be fun"
to := mail.NewEmail("Example User", "[email protected]")
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)
}
}
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": "[email protected]"
}
],
"subject": "Substitutions can be fun",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver",
"-user_id-": "343"
},
"custom_args": {
"user_id": "-user_id-",
"city": "-city-"
}
}
],
"from": {
"email": "[email protected]"
},
"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)
}
}
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", "[email protected]")
subject := "Sections can be fun"
to := mail.NewEmail("Example User", "[email protected]")
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)
}
}
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": "[email protected]"
}
],
"subject": "Sections can be fun",
"substitutions": {
"-name-": "Example User",
"-city-": "Denver",
"-wel-": "-welcome-",
"-gday-": "-great_day-"
}
}
],
"from": {
"email": "[email protected]"
},
"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)
}
}
package main
import (
"fmt"
"log"
"os"
"encoding/base64"
"io/ioutil"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
func main() {
// create new *SGMailV3
m := mail.NewV3Mail()
from := mail.NewEmail("test", "[email protected]")
content := mail.NewContent("text/html", "<p>Sending different attachments.</p>")
to := mail.NewEmail("Example User", "[email protected]")
m.SetFrom(from)
m.AddContent(content)
// create new *Personalization
personalization := mail.NewPersonalization()
personalization.AddTos(to)
personalization.Subject = "Attachments - Demystified!"
// add `personalization` to `m`
m.AddPersonalizations(personalization)
// read/attach .txt file
a_txt := mail.NewAttachment()
dat, err := ioutil.ReadFile("testing.txt")
if err != nil {
fmt.Println(err)
}
encoded := base64.StdEncoding.EncodeToString([]byte(dat))
a_txt.SetContent(encoded)
a_txt.SetType("text/plain")
a_txt.SetFilename("testing.txt")
a_txt.SetDisposition("attachment")
a_txt.SetContentID("Test Document")
// read/attach .pdf file
a_pdf := mail.NewAttachment()
dat, err = ioutil.ReadFile("testing.pdf")
if err != nil {
fmt.Println(err)
}
encoded = base64.StdEncoding.EncodeToString([]byte(dat))
a_pdf.SetContent(encoded)
a_pdf.SetType("application/pdf")
a_pdf.SetFilename("testing.pdf")
a_pdf.SetDisposition("attachment")
a_pdf.SetContentID("Test Attachment")
// read/attach .jpg file
a_jpg := mail.NewAttachment()
dat, err = ioutil.ReadFile("testing.jpg")
if err != nil {
fmt.Println(err)
}
encoded = base64.StdEncoding.EncodeToString([]byte(dat))
a_jpg.SetContent(encoded)
a_jpg.SetType("image/jpeg")
a_jpg.SetFilename("testing.jpg")
a_jpg.SetDisposition("attachment")
a_jpg.SetContentID("Test Attachment")
// add `a_txt`, `a_pdf` and `a_jpg` to `m`
m.AddAttachment(a_txt)
m.AddAttachment(a_pdf)
m.AddAttachment(a_jpg)
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)
}
}
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": "[email protected]"
}
],
"subject": "Attachments - Demystified!"
}
}
],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<p>Sending different attachments.</p>"
}
],
"attachments": [
{
"content": "SGVsbG8gV29ybGQh",
"content_id": "testing_1",
"disposition": "attachment",
"filename": "testing.txt",
"type": "txt"
},
{
"content": "BASE64 encoded content block here",
"content_id": "testing_2",
"disposition": "attachment",
"filename": "testing.jpg",
"type": "jpg"
},
{
"content": "BASE64 encoded content block here",
"content_id": "testing_3",
"disposition": "attachment",
"filename": "testing.pdf",
"type": "pdf"
}
]
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
You can find documentation for how to view your email statistics via the UI here. To view Email Statistics via the API:
package main
import (
"fmt"
"log"
"os"
"github.com/sendgrid/sendgrid-go"
)
func main() {
apiKey := os.Getenv("SENDGRID_API_KEY")
host := "https://api.sendgrid.com"
request := sendgrid.GetRequest(apiKey, "/v3/stats", host)
request.Method = "GET"
queryParams := make(map[string]string)
queryParams["aggregated_by"] = "day"
queryParams["limit"] = "1"
queryParams["start_date"] = "2017-01-01"
queryParams["end_date"] = "2017-10-12"
queryParams["offset"] = "1"
request.QueryParams = queryParams
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
You can find documentation for how to setup a domain whitelabel via the UI here. Find more information about all of SendGrid's whitelabeling related documentation here.
To create a Domain Whitelabel Via the API:
package main
import (
"fmt"
"log"
"os"
"github.com/sendgrid/sendgrid-go"
)
func main() {
apiKey := os.Getenv("SENDGRID_API_KEY")
host := "https://api.sendgrid.com"
request := sendgrid.GetRequest(apiKey, "/v3/whitelabel/domains", host)
request.Method = "POST"
request.Body = []byte(` {
"automatic_security": false,
"custom_spf": true,
"default": true,
"domain": "example.com",
"ips": [
"192.168.1.1",
"192.168.1.2"
],
"subdomain": "SUBDOMAIN",
"username": "YOUR_SENDGRID_SUBUSER_NAME"
}`)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}