diff --git a/USE_CASES.md b/USE_CASES.md index 13209e6e..46e8df62 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -7,6 +7,7 @@ This documentation provides examples for specific use cases. Please [open an iss * [Personalizations](#personalizations) * [Substitutions](#substitutions) * [Sections](#sections) +* [Attachments](#attachments) # Transactional Templates @@ -971,65 +972,65 @@ func main() { ``` - # Substitutions +# Substitutions - ## With Mail Helper Class +## With Mail Helper Class - ```go - package main +```go +package main - import ( +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", "\n\n\t\n\n\nHello -name-,\n

\nI'm glad you are trying out the Substitutions feature!\n

\nI hope you are having a great day in -city- :)\n

\n\n") - 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-") +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", "\n\n\t\n\n\nHello -name-,\n

\nI'm glad you are trying out the Substitutions feature!\n

\nI hope you are having a great day in -city- :)\n

\n\n") + 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) - } - } + 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 +## Without Mail Helper Class - ```go - package main +```go +package main - import ( - "fmt" - "log" - "os" +import ( + "fmt" + "log" + "os" - "github.com/sendgrid/sendgrid-go" - ) + "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(` { +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": [ @@ -1059,79 +1060,79 @@ func main() { } ] }`) - response, err := sendgrid.API(request) - if err != nil { - log.Println(err) - } else { - fmt.Println(response.StatusCode) - fmt.Println(response.Body) - fmt.Println(response.Headers) - } - } + response, err := sendgrid.API(request) + if err != nil { + log.Println(err) + } else { + fmt.Println(response.StatusCode) + fmt.Println(response.Body) + fmt.Println(response.Headers) + } +} ``` - - # Sections + +# Sections - ## With Mail Helper Class +## With Mail Helper Class - ```go - package main +```go +package main - import ( - "fmt" - "log" - "os" +import ( + "fmt" + "log" + "os" - "github.com/sendgrid/sendgrid-go" - "github.com/sendgrid/sendgrid-go/helpers/mail" - ) + "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", "\n\n\t\n\n\n-wel-\n

\nI'm glad you are trying out the Sections feature!\n

\n-gday-\n

\n\n") - 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-") +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", "\n\n\t\n\n\n-wel-\n

\nI'm glad you are trying out the Sections feature!\n

\n-gday-\n

\n\n") + 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- :)") + 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) - } - } + 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 +## Without Mail Helper Class - ```go - package main +```go +package main - import ( - "fmt" - "log" - "os" +import ( + "fmt" + "log" + "os" - "github.com/sendgrid/sendgrid-go" - ) + "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(` { +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": [ @@ -1163,14 +1164,180 @@ func main() { "-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) - } - } + }`) + response, err := sendgrid.API(request) + if err != nil { + log.Println(err) + } else { + fmt.Println(response.StatusCode) + fmt.Println(response.Body) + fmt.Println(response.Headers) + } +} ``` + +# Attachments + +## With Mail Helper Class + +```go +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", "test@example.com") + content := mail.NewContent("text/html", "

Sending different attachments.

") + to := mail.NewEmail("Example User", "test1@example.com") + + 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) + } +} +``` + +## 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": "test1@example.com" + } + ], + "subject": "Attachments - Demystified!" + } + } + ], + "from": { + "email": "test@example.com" + }, + "content": [ + { + "type": "text/html", + "value": "

Sending different attachments.

" + } + ], + "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) + } +} +``` \ No newline at end of file