sendgrid-ruby 1.1.6 → 6.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.env_sample +1 -3
- data/.github/ISSUE_TEMPLATE/config.yml +10 -0
- data/.github/workflows/test-and-deploy.yml +120 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +5 -27
- data/.rubocop_todo.yml +127 -0
- data/CHANGELOG.md +417 -1
- data/CODE_OF_CONDUCT.md +73 -0
- data/CONTRIBUTING.md +202 -0
- data/Dockerfile +14 -0
- data/FIRST_TIMERS.md +79 -0
- data/Gemfile +4 -4
- data/ISSUE_TEMPLATE.md +30 -0
- data/LICENSE +21 -0
- data/Makefile +14 -0
- data/PULL_REQUEST_TEMPLATE.md +31 -0
- data/README.md +153 -229
- data/Rakefile +9 -3
- data/TROUBLESHOOTING.md +151 -0
- data/UPGRADE.md +5 -0
- data/USAGE.md +5144 -0
- data/config.ru +4 -0
- data/examples/accesssettings/accesssettings.rb +80 -0
- data/examples/alerts/alerts.rb +59 -0
- data/examples/apikeys/apikeys.rb +81 -0
- data/examples/asm/asm.rb +170 -0
- data/examples/browsers/browsers.rb +13 -0
- data/examples/campaigns/campaigns.rb +150 -0
- data/examples/categories/categories.rb +33 -0
- data/examples/clients/clients.rb +24 -0
- data/examples/contactdb/contactdb.rb +392 -0
- data/examples/devices/devices.rb +13 -0
- data/examples/emailactivity/emailactivity.rb +52 -0
- data/examples/geo/geo.rb +13 -0
- data/examples/helpers/eventwebhook/example.rb +16 -0
- data/examples/helpers/mail/example.rb +158 -0
- data/examples/helpers/settings/example.rb +23 -0
- data/examples/helpers/stats/example.rb +42 -0
- data/examples/ips/ips.rb +164 -0
- data/examples/mail/mail.rb +170 -0
- data/examples/mailboxproviders/mailboxproviders.rb +13 -0
- data/examples/mailsettings/mailsettings.rb +216 -0
- data/examples/partnersettings/partnersettings.rb +36 -0
- data/examples/scopes/scopes.rb +59 -0
- data/examples/senderauthentication/senderauthentication.rb +307 -0
- data/examples/senders/senders.rb +95 -0
- data/examples/stats/stats.rb +13 -0
- data/examples/subusers/subusers.rb +166 -0
- data/examples/suppression/suppression.rb +198 -0
- data/examples/templates/templates.rb +127 -0
- data/examples/trackingsettings/trackingsettings.rb +107 -0
- data/examples/user/user.rb +290 -0
- data/gemfiles/Sinatra_1.gemfile +6 -0
- data/gemfiles/Sinatra_2.gemfile +6 -0
- data/lib/rack/sendgrid_webhook_verification.rb +55 -0
- data/lib/sendgrid/base_interface.rb +40 -0
- data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
- data/lib/sendgrid/helpers/inbound/README.md +98 -0
- data/lib/sendgrid/helpers/inbound/app.rb +32 -0
- data/lib/sendgrid/helpers/inbound/config.yml +26 -0
- data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
- data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
- data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
- data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
- data/lib/sendgrid/helpers/inbound/send.rb +26 -0
- data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
- data/lib/sendgrid/helpers/mail/README.md +14 -0
- data/lib/sendgrid/helpers/mail/asm.rb +19 -0
- data/lib/sendgrid/helpers/mail/attachment.rb +55 -0
- data/lib/sendgrid/helpers/mail/bcc_settings.rb +19 -0
- data/lib/sendgrid/helpers/mail/bypass_list_management.rb +31 -0
- data/lib/sendgrid/helpers/mail/category.rb +18 -0
- data/lib/sendgrid/helpers/mail/click_tracking.rb +19 -0
- data/lib/sendgrid/helpers/mail/content.rb +19 -0
- data/lib/sendgrid/helpers/mail/custom_arg.rb +18 -0
- data/lib/sendgrid/helpers/mail/email.rb +32 -0
- data/lib/sendgrid/helpers/mail/footer.rb +21 -0
- data/lib/sendgrid/helpers/mail/ganalytics.rb +28 -0
- data/lib/sendgrid/helpers/mail/header.rb +18 -0
- data/lib/sendgrid/helpers/mail/mail.rb +122 -0
- data/lib/sendgrid/helpers/mail/mail_settings.rb +45 -0
- data/lib/sendgrid/helpers/mail/open_tracking.rb +19 -0
- data/lib/sendgrid/helpers/mail/personalization.rb +93 -0
- data/lib/sendgrid/helpers/mail/section.rb +18 -0
- data/lib/sendgrid/helpers/mail/spam_check.rb +21 -0
- data/lib/sendgrid/helpers/mail/subscription_tracking.rb +23 -0
- data/lib/sendgrid/helpers/mail/substitution.rb +18 -0
- data/lib/sendgrid/helpers/mail/tracking_settings.rb +39 -0
- data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
- data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
- data/lib/sendgrid/helpers/settings/README.md +14 -0
- data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/settings/settings.rb +28 -0
- data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +22 -0
- data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
- data/lib/sendgrid/helpers/stats/metrics.rb +33 -0
- data/lib/sendgrid/helpers/stats/stats_response.rb +29 -0
- data/lib/sendgrid/sendgrid.rb +21 -0
- data/lib/sendgrid/twilio_email.rb +21 -0
- data/lib/sendgrid/version.rb +1 -1
- data/lib/sendgrid-ruby.rb +32 -7
- data/mail_helper_v3.md +390 -0
- data/sendgrid-ruby.gemspec +16 -20
- data/spec/fixtures/event_webhook.rb +22 -0
- data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
- data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
- data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
- data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
- data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
- data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
- data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
- data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
- data/spec/sendgrid/helpers/stats/email_stats_spec.rb +111 -0
- data/spec/sendgrid/helpers/stats/metrics_spec.rb +45 -0
- data/spec/sendgrid/helpers/stats/stats_response_spec.rb +75 -0
- data/spec/sendgrid/sendgrid_spec.rb +11 -0
- data/spec/sendgrid/twilio_email_spec.rb +11 -0
- data/spec/spec_helper.rb +12 -1
- data/static/img/github-fork.png +0 -0
- data/static/img/github-sign-up.png +0 -0
- data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
- data/test/sendgrid/helpers/mail/test_category.rb +25 -0
- data/test/sendgrid/helpers/mail/test_email.rb +41 -0
- data/test/sendgrid/helpers/mail/test_mail.rb +260 -0
- data/test/sendgrid/helpers/mail/test_personalizations.rb +214 -0
- data/test/sendgrid/permissions/test_scopes.rb +36 -0
- data/test/sendgrid/test_sendgrid-ruby.rb +2729 -0
- data/twilio_sendgrid_logo.png +0 -0
- data/use-cases/README.md +17 -0
- data/use-cases/domain-authentication.md +5 -0
- data/use-cases/email-statistics.md +52 -0
- data/use-cases/legacy-templates.md +98 -0
- data/use-cases/personalizations.md +34 -0
- data/use-cases/sms.md +39 -0
- data/use-cases/transactional-templates.md +111 -0
- data/use-cases/twilio-email.md +13 -0
- data/use-cases/twilio-setup.md +54 -0
- metadata +187 -112
- data/.rspec +0 -2
- data/.travis.yml +0 -20
- data/FETCH_HEAD +0 -0
- data/Guardfile +0 -10
- data/LICENSE.txt +0 -22
- data/example.rb +0 -41
- data/lib/sendgrid/client.rb +0 -62
- data/lib/sendgrid/exceptions.rb +0 -7
- data/lib/sendgrid/mail.rb +0 -182
- data/lib/sendgrid/recipient.rb +0 -29
- data/lib/sendgrid/response.rb +0 -14
- data/lib/sendgrid/template.rb +0 -26
- data/lib/sendgrid/template_mailer.rb +0 -59
- data/spec/lib/sendgrid/client_spec.rb +0 -87
- data/spec/lib/sendgrid/mail_spec.rb +0 -151
- data/spec/lib/sendgrid/recipient_spec.rb +0 -91
- data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
- data/spec/lib/sendgrid/template_spec.rb +0 -61
- data/spec/lib/sendgrid_spec.rb +0 -7
|
Binary file
|
data/use-cases/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
This directory provides examples for specific use cases.
|
|
2
|
+
|
|
3
|
+
Please [open an issue](https://github.com/sendgrid/sendgrid-ruby/issues) or [make a pull request](https://github.com/sendgrid/sendgrid-ruby/pulls) for any use cases you would like us to document here. Thank you!
|
|
4
|
+
|
|
5
|
+
# Email Use Cases
|
|
6
|
+
* [Transactional Templates](transactional-templates.md)
|
|
7
|
+
* [Legacy Templates](legacy-templates.md)
|
|
8
|
+
* [Personalizations](personalizations.md)
|
|
9
|
+
|
|
10
|
+
# Twilio Use Cases
|
|
11
|
+
* [Twilio Setup](twilio-setup.md)
|
|
12
|
+
* [Send an Email With Twilio Email (Pilot)](twilio-email.md)
|
|
13
|
+
* [Send an SMS Message](sms.md)
|
|
14
|
+
|
|
15
|
+
# Non-Email Use Cases
|
|
16
|
+
* [How to Set up a Domain Authentication](domain-authentication.md)
|
|
17
|
+
* [How to View Email Statistics](email-statistics.md)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# How to Setup a Domain Authentication
|
|
2
|
+
|
|
3
|
+
You can find documentation for how to setup a domain authentication via the UI [here](https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/) and via API [here](../USAGE.md#sender-authentication).
|
|
4
|
+
|
|
5
|
+
Find more information about all of Twilio SendGrid's authentication related documentation [here](https://sendgrid.com/docs/ui/account-and-settings/).
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# How to View Email Statistics
|
|
2
|
+
|
|
3
|
+
You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](../USAGE.md#stats).
|
|
4
|
+
|
|
5
|
+
Alternatively, we can post events to a URL of your choice via our [Event Webhook](https://sendgrid.com/docs/API_Reference/Webhooks/event.html) about events that occur as Twilio SendGrid processes your email.
|
|
6
|
+
|
|
7
|
+
You can also use the email statistics helper to make it easier to interact with the API.
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
require 'sendgrid-ruby'
|
|
11
|
+
require 'date'
|
|
12
|
+
|
|
13
|
+
include SendGrid
|
|
14
|
+
|
|
15
|
+
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
|
|
16
|
+
stats = SendGrid::EmailStats.new(sendgrid_client: sg_client)
|
|
17
|
+
|
|
18
|
+
# Fetch stats by day, between 2 dates
|
|
19
|
+
from = Date.new(2017, 10, 01)
|
|
20
|
+
to = Date.new(2017, 10, 12)
|
|
21
|
+
|
|
22
|
+
email_stats = stats.by_day(from, to)
|
|
23
|
+
|
|
24
|
+
email_stats.metrics
|
|
25
|
+
|
|
26
|
+
if !email_stats.error?
|
|
27
|
+
email_stats.metrics.each do |metric|
|
|
28
|
+
puts "Date - #{metric.date}"
|
|
29
|
+
puts "Number of Requests - #{metric.requests}"
|
|
30
|
+
puts "Bounces - #{metric.bounces}"
|
|
31
|
+
puts "Opens - #{metric.opens}"
|
|
32
|
+
puts "Clicks - #{metric.clicks}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Fetch stats by week, between 2 dates for a category
|
|
37
|
+
from = Date.new(2017, 10, 01)
|
|
38
|
+
to = Date.new(2017, 10, 12)
|
|
39
|
+
category = 'abcd'
|
|
40
|
+
|
|
41
|
+
email_stats = stats.by_week(from, to, category)
|
|
42
|
+
|
|
43
|
+
if !email_stats.error?
|
|
44
|
+
email_stats.metrics.each do |metric|
|
|
45
|
+
puts "Date - #{metric.date}"
|
|
46
|
+
puts "Number of Requests - #{metric.requests}"
|
|
47
|
+
puts "Bounces - #{metric.bounces}"
|
|
48
|
+
puts "Opens - #{metric.opens}"
|
|
49
|
+
puts "Clicks - #{metric.clicks}"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
```
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Legacy Templates
|
|
2
|
+
|
|
3
|
+
For this example, we assume you have created a [legacy transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html) in the UI or via the API. Following is the template content we used for testing.
|
|
4
|
+
|
|
5
|
+
Template ID (replace with your own):
|
|
6
|
+
```text
|
|
7
|
+
13b8f94f-bcae-4ec6-b752-70d6cb59f932
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Email Subject:
|
|
11
|
+
```text
|
|
12
|
+
<%subject%>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Template Body:
|
|
16
|
+
```html
|
|
17
|
+
<html>
|
|
18
|
+
<head>
|
|
19
|
+
<title></title>
|
|
20
|
+
</head>
|
|
21
|
+
<body>
|
|
22
|
+
Hello -name-,
|
|
23
|
+
<br /><br/>
|
|
24
|
+
I'm glad you are trying out the template feature!
|
|
25
|
+
<br /><br/>
|
|
26
|
+
<%body%>
|
|
27
|
+
<br /><br/>
|
|
28
|
+
I hope you are having a great day in -city- :)
|
|
29
|
+
<br /><br/>
|
|
30
|
+
</body>
|
|
31
|
+
</html>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## With Mail Helper Class
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
require 'sendgrid-ruby'
|
|
38
|
+
include SendGrid
|
|
39
|
+
|
|
40
|
+
mail = SendGrid::Mail.new
|
|
41
|
+
mail.from = Email.new(email: 'test@example.com')
|
|
42
|
+
mail.subject = 'I\'m replacing the subject tag'
|
|
43
|
+
personalization = Personalization.new
|
|
44
|
+
personalization.add_to(Email.new(email: 'test@example.com'))
|
|
45
|
+
personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User'))
|
|
46
|
+
personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver'))
|
|
47
|
+
mail.add_personalization(personalization)
|
|
48
|
+
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
|
|
49
|
+
|
|
50
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
|
51
|
+
begin
|
|
52
|
+
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
|
53
|
+
rescue Exception => e
|
|
54
|
+
puts e.message
|
|
55
|
+
end
|
|
56
|
+
puts response.status_code
|
|
57
|
+
puts response.body
|
|
58
|
+
puts response.parsed_body
|
|
59
|
+
puts response.headers
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Without Mail Helper Class
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
require 'sendgrid-ruby'
|
|
66
|
+
include SendGrid
|
|
67
|
+
|
|
68
|
+
data = JSON.parse('{
|
|
69
|
+
"personalizations": [
|
|
70
|
+
{
|
|
71
|
+
"to": [
|
|
72
|
+
{
|
|
73
|
+
"email": "test@example.com"
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
"substitutions": {
|
|
77
|
+
"-name-": "Example User",
|
|
78
|
+
"-city-": "Denver"
|
|
79
|
+
},
|
|
80
|
+
"subject": "I\'m replacing the subject tag"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"from": {
|
|
84
|
+
"email": "test@example.com"
|
|
85
|
+
},
|
|
86
|
+
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
|
|
87
|
+
}')
|
|
88
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
|
89
|
+
begin
|
|
90
|
+
response = sg.client.mail._("send").post(request_body: data)
|
|
91
|
+
rescue Exception => e
|
|
92
|
+
puts e.message
|
|
93
|
+
end
|
|
94
|
+
puts response.status_code
|
|
95
|
+
puts response.body
|
|
96
|
+
puts response.parsed_body
|
|
97
|
+
puts response.headers
|
|
98
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Personalizations
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to send multiple emails with personalizations. For further documentation, refer to [the SendGrid docs](https://docs.sendgrid.com/for-developers/sending-email/personalizations).
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
require 'sendgrid-ruby'
|
|
7
|
+
include SendGrid
|
|
8
|
+
|
|
9
|
+
# Note that the domain for all From addresses must match
|
|
10
|
+
mail = Mail.new
|
|
11
|
+
mail.from = Email.new(email: 'test@example.com')
|
|
12
|
+
mail.add_content(Content.new(type: 'text/plain', value: 'Some test text'))
|
|
13
|
+
mail.subject = 'Personalized Test Email'
|
|
14
|
+
|
|
15
|
+
personalization = Personalization.new
|
|
16
|
+
personalization.add_to(Email.new(email: 'test1@example.com'))
|
|
17
|
+
mail.add_personalization(personalization)
|
|
18
|
+
|
|
19
|
+
personalization2 = Personalization.new
|
|
20
|
+
personalization2.add_to(Email.new(email: 'test2@example.com'))
|
|
21
|
+
personalization2.add_from(Email.new(email: 'test3@example.com'))
|
|
22
|
+
mail.add_personalization(personalization2)
|
|
23
|
+
|
|
24
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
|
25
|
+
begin
|
|
26
|
+
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
|
27
|
+
rescue Exception => e
|
|
28
|
+
puts e.message
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
puts response.status_code
|
|
32
|
+
puts response.body
|
|
33
|
+
puts response.headers
|
|
34
|
+
```
|
data/use-cases/sms.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
First, follow the [Twilio Setup](twilio-setup.md) guide for creating a Twilio account and setting up environment variables with the proper credentials.
|
|
2
|
+
|
|
3
|
+
Then, install the Twilio Helper Library. Add this line to your application's Gemfile:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
gem 'twilio-ruby'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
And then execute:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bundle
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or install it yourself using:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install twilio-ruby
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Finally, send a message.
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require 'twilio-ruby'
|
|
25
|
+
|
|
26
|
+
# put your own credentials here
|
|
27
|
+
account_sid = ENV['TWILIO_ACCOUNT_SID']
|
|
28
|
+
auth_token = ENV['TWILIO_AUTH_TOKEN']
|
|
29
|
+
|
|
30
|
+
# set up a client to talk to the Twilio REST API
|
|
31
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
|
32
|
+
@client.api.account.messages.create(
|
|
33
|
+
from: '+14159341234',
|
|
34
|
+
to: '+16105557069',
|
|
35
|
+
body: 'Hey there!'
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
For more information, please visit the [Twilio SMS Ruby documentation](https://www.twilio.com/docs/sms/quickstart/ruby).
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Transactional Templates
|
|
2
|
+
|
|
3
|
+
For this example, we assume you have created a [dynamic transactional template](https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/) in the UI or via the API. Following is the template content we used for testing.
|
|
4
|
+
|
|
5
|
+
Template ID (replace with your own):
|
|
6
|
+
```text
|
|
7
|
+
d-2c214ac919e84170b21855cc129b4a5f
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Email Subject:
|
|
11
|
+
```text
|
|
12
|
+
{{subject}}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Template Body:
|
|
16
|
+
```html
|
|
17
|
+
<html>
|
|
18
|
+
<head>
|
|
19
|
+
<title></title>
|
|
20
|
+
</head>
|
|
21
|
+
<body>
|
|
22
|
+
Hello {{name}},
|
|
23
|
+
<br/><br/>
|
|
24
|
+
I'm glad you are trying out the template feature!
|
|
25
|
+
<br/><br/>
|
|
26
|
+
I hope you are having a great day in {{city}} :)
|
|
27
|
+
<br/><br/>
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## With Mail Helper Class
|
|
33
|
+
```ruby
|
|
34
|
+
require 'sendgrid-ruby'
|
|
35
|
+
include SendGrid
|
|
36
|
+
|
|
37
|
+
mail = Mail.new
|
|
38
|
+
mail.from = Email.new(email: 'test@example.com')
|
|
39
|
+
personalization = Personalization.new
|
|
40
|
+
personalization.add_to(Email.new(email: 'test@example.com'))
|
|
41
|
+
personalization.add_dynamic_template_data({
|
|
42
|
+
"subject" => "Testing Templates",
|
|
43
|
+
"name" => "Example User",
|
|
44
|
+
"city" => "Denver"
|
|
45
|
+
})
|
|
46
|
+
mail.add_personalization(personalization)
|
|
47
|
+
mail.template_id = 'd-2c214ac919e84170b21855cc129b4a5f'
|
|
48
|
+
|
|
49
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
|
50
|
+
begin
|
|
51
|
+
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
|
52
|
+
rescue Exception => e
|
|
53
|
+
puts e.message
|
|
54
|
+
end
|
|
55
|
+
puts response.status_code
|
|
56
|
+
puts response.body
|
|
57
|
+
puts response.parsed_body
|
|
58
|
+
puts response.headers
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Without Mail Helper Class
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
require 'sendgrid-ruby'
|
|
65
|
+
include SendGrid
|
|
66
|
+
|
|
67
|
+
data = JSON.parse('{
|
|
68
|
+
"personalizations": [
|
|
69
|
+
{
|
|
70
|
+
"to": [
|
|
71
|
+
{
|
|
72
|
+
"email": "test@example.com"
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
"dynamic_template_data": {
|
|
76
|
+
"subject": "Testing Templates",
|
|
77
|
+
"name": "Example User",
|
|
78
|
+
"city": "Denver"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
"from": {
|
|
83
|
+
"email": "test@example.com"
|
|
84
|
+
},
|
|
85
|
+
"template_id": "d-2c214ac919e84170b21855cc129b4a5f"
|
|
86
|
+
}')
|
|
87
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
|
88
|
+
begin
|
|
89
|
+
response = sg.client.mail._("send").post(request_body: data)
|
|
90
|
+
rescue Exception => e
|
|
91
|
+
puts e.message
|
|
92
|
+
end
|
|
93
|
+
puts response.status_code
|
|
94
|
+
puts response.body
|
|
95
|
+
puts response.parsed_body
|
|
96
|
+
puts response.headers
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Adding Attachments
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
attachment = Attachment.new
|
|
103
|
+
attachment.content = Base64.strict_encode64(File.open(fpath, 'rb').read)
|
|
104
|
+
attachment.type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
105
|
+
attachment.filename = fname
|
|
106
|
+
attachment.disposition = 'attachment'
|
|
107
|
+
attachment.content_id = 'Reports Sheet'
|
|
108
|
+
mail.add_attachment(attachment)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Attachments must be base64 encoded, using Base64's strict_encode64 where no line feeds are added.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
First, follow the [Twilio Setup](twilio-setup.md) guide for creating a Twilio account and setting up environment variables with the proper credentials.
|
|
2
|
+
|
|
3
|
+
Then, initialize the Twilio Email Client.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
mail_client = TwilioEmail::API(username: ENV['TWILIO_API_KEY'], password: ENV['TWILIO_API_SECRET'])
|
|
7
|
+
|
|
8
|
+
# or
|
|
9
|
+
|
|
10
|
+
mail_client = TwilioEmail::API(username: ENV['TWILIO_ACCOUNT_SID'], password: ENV['TWILIO_AUTH_TOKEN'])
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This client has the same interface as the `SendGrid::API` client.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
## 1. Obtain a Free Twilio Account
|
|
2
|
+
|
|
3
|
+
Sign up for a free Twilio account [here](https://www.twilio.com/try-twilio?source=sendgrid-ruby).
|
|
4
|
+
|
|
5
|
+
## 2. Set Up Your Environment Variables
|
|
6
|
+
|
|
7
|
+
The Twilio API allows for authentication using with either an API key/secret or your Account SID/Auth Token. You can create an API key [here](https://twil.io/get-api-key) or obtain your Account SID and Auth Token [here](https://twil.io/console).
|
|
8
|
+
|
|
9
|
+
Once you have those, follow the steps below based on your operating system.
|
|
10
|
+
|
|
11
|
+
### Linux/Mac
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
echo "export TWILIO_API_KEY='YOUR_TWILIO_API_KEY'" > twilio.env
|
|
15
|
+
echo "export TWILIO_API_SECRET='YOUR_TWILIO_API_SECRET'" >> twilio.env
|
|
16
|
+
|
|
17
|
+
# or
|
|
18
|
+
|
|
19
|
+
echo "export TWILIO_ACCOUNT_SID='YOUR_TWILIO_ACCOUNT_SID'" > twilio.env
|
|
20
|
+
echo "export TWILIO_AUTH_TOKEN='YOUR_TWILIO_AUTH_TOKEN'" >> twilio.env
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
echo "twilio.env" >> .gitignore
|
|
27
|
+
source ./twilio.env
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Windows
|
|
31
|
+
|
|
32
|
+
Temporarily set the environment variable (accessible only during the current CLI session):
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
set TWILIO_API_KEY=YOUR_TWILIO_API_KEY
|
|
36
|
+
set TWILIO_API_SECRET=YOUR_TWILIO_API_SECRET
|
|
37
|
+
|
|
38
|
+
: or
|
|
39
|
+
|
|
40
|
+
set TWILIO_ACCOUNT_SID=YOUR_TWILIO_ACCOUNT_SID
|
|
41
|
+
set TWILIO_AUTH_TOKEN=YOUR_TWILIO_AUTH_TOKEN
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or permanently set the environment variable (accessible in all subsequent CLI sessions):
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
setx TWILIO_API_KEY "YOUR_TWILIO_API_KEY"
|
|
48
|
+
setx TWILIO_API_SECRET "YOUR_TWILIO_API_SECRET"
|
|
49
|
+
|
|
50
|
+
: or
|
|
51
|
+
|
|
52
|
+
setx TWILIO_ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
|
|
53
|
+
setx TWILIO_AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
|
|
54
|
+
```
|