-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from sendgrid/kylearoberts-patch-1
Update USE_CASES.md
- Loading branch information
Showing
1 changed file
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,115 @@ This documentation provides examples for specific use cases. Please [open an iss | |
# Table of Contents | ||
|
||
* [Transactional Templates](#transactional-templates) | ||
* [Legacy Templates](#legacy-templates) | ||
* [How to Setup a Domain Whitelabel](#domain-whitelabel) | ||
* [How to View Email Statistics](#email-statistics) | ||
|
||
<a name="transactional-templates"></a> | ||
# (LEGACY) Transactional Templates | ||
|
||
IF YOU ARE USING OUR NEW TEMPLATES, PLEASE SEE [THIS ISSUE](https://github.com/sendgrid/sendgrid-ruby/issues/301). | ||
# Transactional Templates | ||
|
||
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. | ||
|
||
Template ID (replace with your own): | ||
|
||
```text | ||
d-2c214ac919e84170b21855cc129b4a5f | ||
``` | ||
Email Subject: | ||
```text | ||
{{subject}} | ||
``` | ||
|
||
Template Body: | ||
|
||
```html | ||
<html> | ||
<head> | ||
<title></title> | ||
</head> | ||
<body> | ||
Hello {{name}}, | ||
<br/><br/> | ||
I'm glad you are trying out the template feature! | ||
<br/><br/> | ||
I hope you are having a great day in {{city}} :) | ||
<br/><br/> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## With Mail Helper Class | ||
```ruby | ||
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
mail = Mail.new | ||
mail.from = Email.new(email: '[email protected]') | ||
personalization = Personalization.new | ||
personalization.add_to(Email.new(email: '[email protected]')) | ||
personalization.add_dynamic_template_data({ | ||
"name" => "Example User", | ||
"city" => "Denver" | ||
}) | ||
mail.add_personalization(personalization) | ||
mail.template_id = 'd-2c214ac919e84170b21855cc129b4a5f' | ||
|
||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
begin | ||
response = sg.client.mail._("send").post(request_body: mail.to_json) | ||
rescue Exception => e | ||
puts e.message | ||
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
## Without Mail Helper Class | ||
|
||
```ruby | ||
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
data = JSON.parse('{ | ||
"personalizations": [ | ||
{ | ||
"to": [ | ||
{ | ||
"email": "[email protected]" | ||
} | ||
], | ||
"dynamic_template_data": { | ||
"name": "Example User", | ||
"city": "Denver" | ||
} | ||
} | ||
], | ||
"from": { | ||
"email": "[email protected]" | ||
}, | ||
"template_id": "d-2c214ac919e84170b21855cc129b4a5f" | ||
}') | ||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
begin | ||
response = sg.client.mail._("send").post(request_body: data) | ||
rescue Exception => e | ||
puts e.message | ||
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
<a name="legacy-templates"></a> | ||
# Legacy Templates | ||
|
||
For this example, we assume you have created a [legacy template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. | ||
|
||
Template ID (replace with your own): | ||
|
||
```text | ||
13b8f94f-bcae-4ec6-b752-70d6cb59f932 | ||
``` | ||
|
@@ -59,7 +156,6 @@ personalization.add_to(Email.new(email: '[email protected]')) | |
personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User')) | ||
personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver')) | ||
mail.add_personalization(personalization) | ||
mail.add_content(Content.new(type: 'text/html', value: 'I\'m replacing the <strong>body tag</strong>')) | ||
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932' | ||
|
||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
|
@@ -98,12 +194,6 @@ data = JSON.parse('{ | |
"from": { | ||
"email": "[email protected]" | ||
}, | ||
"content": [ | ||
{ | ||
"type": "text/html", | ||
"value": "I\'m replacing the <strong>body tag</strong>" | ||
} | ||
], | ||
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932" | ||
}') | ||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
|