Skip to content

Commit

Permalink
Merge pull request #343 from sendgrid/kylearoberts-patch-1
Browse files Browse the repository at this point in the history
Update USE_CASES.md
  • Loading branch information
thinkingserious authored Oct 12, 2018
2 parents 6dba466 + 6a2e31b commit c3e6da3
Showing 1 changed file with 100 additions and 10 deletions.
110 changes: 100 additions & 10 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit c3e6da3

Please sign in to comment.