diff --git a/USE_CASES.md b/USE_CASES.md index f025e749..40a964e9 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -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) -# (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 + + + + + + Hello {{name}}, +

+ I'm glad you are trying out the template feature! +

+ I hope you are having a great day in {{city}} :) +

+ + +``` + +## With Mail Helper Class +```ruby +require 'sendgrid-ruby' +include SendGrid + +mail = Mail.new +mail.from = Email.new(email: 'test@example.com') +personalization = Personalization.new +personalization.add_to(Email.new(email: 'test@example.com')) +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": "test@example.com" + } + ], + "dynamic_template_data": { + "name": "Example User", + "city": "Denver" + } + } + ], + "from": { + "email": "test@example.com" + }, + "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 +``` + + +# 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: 'test@example.com')) 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 body tag')) 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": "test@example.com" }, - "content": [ - { - "type": "text/html", - "value": "I\'m replacing the body tag" - } - ], "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932" }') sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])