sendgrid-ruby 6.2.0 → 6.3.4
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 +4 -4
- data/.codeclimate.yml +1 -1
- data/.gitignore +2 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +12 -21
- data/CHANGELOG.md +57 -8
- data/CONTRIBUTING.md +10 -17
- data/Dockerfile +14 -0
- data/FIRST_TIMERS.md +79 -0
- data/Gemfile +0 -1
- data/ISSUE_TEMPLATE.md +5 -1
- data/Makefile +9 -2
- data/PULL_REQUEST_TEMPLATE.md +1 -1
- data/README.md +22 -21
- data/TROUBLESHOOTING.md +5 -5
- data/USAGE.md +35 -36
- data/examples/helpers/eventwebhook/example.rb +16 -0
- data/examples/helpers/mail/example.rb +11 -4
- data/examples/mail/mail.rb +1 -1
- data/lib/rack/sendgrid_webhook_verification.rb +52 -0
- data/lib/sendgrid-ruby.rb +2 -0
- data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +52 -0
- data/lib/sendgrid/helpers/inbound/README.md +5 -5
- data/lib/sendgrid/helpers/inbound/public/index.html +1 -1
- data/lib/sendgrid/helpers/mail/README.md +3 -3
- data/lib/sendgrid/helpers/settings/README.md +2 -2
- data/lib/sendgrid/version.rb +1 -1
- data/mail_helper_v3.md +9 -9
- data/sendgrid-ruby.gemspec +2 -0
- data/spec/fixtures/event_webhook.rb +16 -0
- data/spec/rack/sendgrid_webhook_verification_spec.rb +116 -0
- data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +103 -0
- data/spec/spec_helper.rb +2 -0
- data/static/img/github-fork.png +0 -0
- data/static/img/github-sign-up.png +0 -0
- data/test/sendgrid/helpers/mail/test_mail.rb +1 -1
- data/test/sendgrid/test_sendgrid-ruby.rb +24 -59
- data/twilio_sendgrid_logo.png +0 -0
- data/use-cases/README.md +16 -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/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 +54 -8
- data/USE_CASES.md +0 -405
- data/docker/Dockerfile +0 -12
- data/docker/README.md +0 -30
- data/test/prism.sh +0 -42
@@ -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
|
+
```
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sendgrid-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elmer Thomas
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-08-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: ruby_http_client
|
@@ -130,6 +130,34 @@ dependencies:
|
|
130
130
|
- - "~>"
|
131
131
|
- !ruby/object:Gem::Version
|
132
132
|
version: '5.9'
|
133
|
+
- !ruby/object:Gem::Dependency
|
134
|
+
name: rack
|
135
|
+
requirement: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
type: :development
|
141
|
+
prerelease: false
|
142
|
+
version_requirements: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
- !ruby/object:Gem::Dependency
|
148
|
+
name: simplecov
|
149
|
+
requirement: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - "~>"
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: 0.18.5
|
154
|
+
type: :development
|
155
|
+
prerelease: false
|
156
|
+
version_requirements: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - "~>"
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: 0.18.5
|
133
161
|
description: Official Twilio SendGrid Gem to Interact with Twilio SendGrids API in
|
134
162
|
native Ruby
|
135
163
|
email: help@twilio.com
|
@@ -140,10 +168,13 @@ files:
|
|
140
168
|
- ".codeclimate.yml"
|
141
169
|
- ".env_sample"
|
142
170
|
- ".gitignore"
|
171
|
+
- ".rubocop.yml"
|
143
172
|
- ".travis.yml"
|
144
173
|
- CHANGELOG.md
|
145
174
|
- CODE_OF_CONDUCT.md
|
146
175
|
- CONTRIBUTING.md
|
176
|
+
- Dockerfile
|
177
|
+
- FIRST_TIMERS.md
|
147
178
|
- Gemfile
|
148
179
|
- ISSUE_TEMPLATE.md
|
149
180
|
- LICENSE.md
|
@@ -154,10 +185,7 @@ files:
|
|
154
185
|
- TROUBLESHOOTING.md
|
155
186
|
- UPGRADE.md
|
156
187
|
- USAGE.md
|
157
|
-
- USE_CASES.md
|
158
188
|
- config.ru
|
159
|
-
- docker/Dockerfile
|
160
|
-
- docker/README.md
|
161
189
|
- examples/accesssettings/accesssettings.rb
|
162
190
|
- examples/alerts/alerts.rb
|
163
191
|
- examples/apikeys/apikeys.rb
|
@@ -169,6 +197,7 @@ files:
|
|
169
197
|
- examples/contactdb/contactdb.rb
|
170
198
|
- examples/devices/devices.rb
|
171
199
|
- examples/geo/geo.rb
|
200
|
+
- examples/helpers/eventwebhook/example.rb
|
172
201
|
- examples/helpers/mail/example.rb
|
173
202
|
- examples/helpers/settings/example.rb
|
174
203
|
- examples/helpers/stats/example.rb
|
@@ -188,8 +217,10 @@ files:
|
|
188
217
|
- examples/user/user.rb
|
189
218
|
- gemfiles/Sinatra_1.gemfile
|
190
219
|
- gemfiles/Sinatra_2.gemfile
|
220
|
+
- lib/rack/sendgrid_webhook_verification.rb
|
191
221
|
- lib/sendgrid-ruby.rb
|
192
222
|
- lib/sendgrid/base_interface.rb
|
223
|
+
- lib/sendgrid/helpers/eventwebhook/eventwebhook.rb
|
193
224
|
- lib/sendgrid/helpers/inbound/README.md
|
194
225
|
- lib/sendgrid/helpers/inbound/app.rb
|
195
226
|
- lib/sendgrid/helpers/inbound/config.yml
|
@@ -237,6 +268,9 @@ files:
|
|
237
268
|
- lib/sendgrid/version.rb
|
238
269
|
- mail_helper_v3.md
|
239
270
|
- sendgrid-ruby.gemspec
|
271
|
+
- spec/fixtures/event_webhook.rb
|
272
|
+
- spec/rack/sendgrid_webhook_verification_spec.rb
|
273
|
+
- spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb
|
240
274
|
- spec/sendgrid/helpers/ip_management/ip_management_spec.rb
|
241
275
|
- spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb
|
242
276
|
- spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb
|
@@ -249,7 +283,8 @@ files:
|
|
249
283
|
- spec/sendgrid/sendgrid_spec.rb
|
250
284
|
- spec/sendgrid/twilio_email_spec.rb
|
251
285
|
- spec/spec_helper.rb
|
252
|
-
-
|
286
|
+
- static/img/github-fork.png
|
287
|
+
- static/img/github-sign-up.png
|
253
288
|
- test/sendgrid/helpers/mail/test_attachment.rb
|
254
289
|
- test/sendgrid/helpers/mail/test_category.rb
|
255
290
|
- test/sendgrid/helpers/mail/test_email.rb
|
@@ -257,6 +292,15 @@ files:
|
|
257
292
|
- test/sendgrid/helpers/mail/test_personalizations.rb
|
258
293
|
- test/sendgrid/permissions/test_scopes.rb
|
259
294
|
- test/sendgrid/test_sendgrid-ruby.rb
|
295
|
+
- twilio_sendgrid_logo.png
|
296
|
+
- use-cases/README.md
|
297
|
+
- use-cases/domain-authentication.md
|
298
|
+
- use-cases/email-statistics.md
|
299
|
+
- use-cases/legacy-templates.md
|
300
|
+
- use-cases/sms.md
|
301
|
+
- use-cases/transactional-templates.md
|
302
|
+
- use-cases/twilio-email.md
|
303
|
+
- use-cases/twilio-setup.md
|
260
304
|
homepage: http://github.com/sendgrid/sendgrid-ruby
|
261
305
|
licenses:
|
262
306
|
- MIT
|
@@ -276,11 +320,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
276
320
|
- !ruby/object:Gem::Version
|
277
321
|
version: '0'
|
278
322
|
requirements: []
|
279
|
-
rubygems_version: 3.0.
|
323
|
+
rubygems_version: 3.0.8
|
280
324
|
signing_key:
|
281
325
|
specification_version: 4
|
282
326
|
summary: Official Twilio SendGrid Gem
|
283
327
|
test_files:
|
328
|
+
- spec/fixtures/event_webhook.rb
|
329
|
+
- spec/rack/sendgrid_webhook_verification_spec.rb
|
330
|
+
- spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb
|
284
331
|
- spec/sendgrid/helpers/ip_management/ip_management_spec.rb
|
285
332
|
- spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb
|
286
333
|
- spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb
|
@@ -293,7 +340,6 @@ test_files:
|
|
293
340
|
- spec/sendgrid/sendgrid_spec.rb
|
294
341
|
- spec/sendgrid/twilio_email_spec.rb
|
295
342
|
- spec/spec_helper.rb
|
296
|
-
- test/prism.sh
|
297
343
|
- test/sendgrid/helpers/mail/test_attachment.rb
|
298
344
|
- test/sendgrid/helpers/mail/test_category.rb
|
299
345
|
- test/sendgrid/helpers/mail/test_email.rb
|
data/USE_CASES.md
DELETED
@@ -1,405 +0,0 @@
|
|
1
|
-
This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-ruby/issues) or make a pull request for any use cases you would like us to document here. Thank you!
|
2
|
-
|
3
|
-
# Table of Contents
|
4
|
-
|
5
|
-
* [Transactional Templates](#transactional-templates)
|
6
|
-
* [With Mail Helper Class](#with-mail-helper-class)
|
7
|
-
* [Without Mail Helper Class](#without-mail-helper-class)
|
8
|
-
* [Legacy Templates](#legacy-templates)
|
9
|
-
* [With Mail Helper Class](#with-mail-helper-class-1)
|
10
|
-
* [Without Mail Helper Class](#without-mail-helper-class-1)
|
11
|
-
* [Adding Attachments](#adding-attachments)
|
12
|
-
* [How to Setup a Domain Authentication](#how-to-setup-a-domain-authentication)
|
13
|
-
* [How to View Email Statistics](#how-to-view-email-statistics)
|
14
|
-
* [Send an Email With Twilio Email (Pilot)](#send-an-email-with-twilio-email-pilot)
|
15
|
-
* [Send an SMS Message](#send-an-sms-message)
|
16
|
-
|
17
|
-
<a name="transactional-templates"></a>
|
18
|
-
# Transactional Templates
|
19
|
-
|
20
|
-
For this example, we assume you have created a [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.
|
21
|
-
|
22
|
-
Template ID (replace with your own):
|
23
|
-
|
24
|
-
```text
|
25
|
-
d-2c214ac919e84170b21855cc129b4a5f
|
26
|
-
```
|
27
|
-
Email Subject:
|
28
|
-
```text
|
29
|
-
{{subject}}
|
30
|
-
```
|
31
|
-
|
32
|
-
Template Body:
|
33
|
-
|
34
|
-
```html
|
35
|
-
<html>
|
36
|
-
<head>
|
37
|
-
<title></title>
|
38
|
-
</head>
|
39
|
-
<body>
|
40
|
-
Hello {{name}},
|
41
|
-
<br/><br/>
|
42
|
-
I'm glad you are trying out the template feature!
|
43
|
-
<br/><br/>
|
44
|
-
I hope you are having a great day in {{city}} :)
|
45
|
-
<br/><br/>
|
46
|
-
</body>
|
47
|
-
</html>
|
48
|
-
```
|
49
|
-
|
50
|
-
## With Mail Helper Class
|
51
|
-
```ruby
|
52
|
-
require 'sendgrid-ruby'
|
53
|
-
include SendGrid
|
54
|
-
|
55
|
-
mail = Mail.new
|
56
|
-
mail.from = Email.new(email: 'test@example.com')
|
57
|
-
personalization = Personalization.new
|
58
|
-
personalization.add_to(Email.new(email: 'test@example.com'))
|
59
|
-
personalization.add_dynamic_template_data({
|
60
|
-
"subject" => "Testing Templates",
|
61
|
-
"name" => "Example User",
|
62
|
-
"city" => "Denver"
|
63
|
-
})
|
64
|
-
mail.add_personalization(personalization)
|
65
|
-
mail.template_id = 'd-2c214ac919e84170b21855cc129b4a5f'
|
66
|
-
|
67
|
-
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
68
|
-
begin
|
69
|
-
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
70
|
-
rescue Exception => e
|
71
|
-
puts e.message
|
72
|
-
end
|
73
|
-
puts response.status_code
|
74
|
-
puts response.body
|
75
|
-
puts response.parsed_body
|
76
|
-
puts response.headers
|
77
|
-
```
|
78
|
-
|
79
|
-
## Without Mail Helper Class
|
80
|
-
|
81
|
-
```ruby
|
82
|
-
require 'sendgrid-ruby'
|
83
|
-
include SendGrid
|
84
|
-
|
85
|
-
data = JSON.parse('{
|
86
|
-
"personalizations": [
|
87
|
-
{
|
88
|
-
"to": [
|
89
|
-
{
|
90
|
-
"email": "test@example.com"
|
91
|
-
}
|
92
|
-
],
|
93
|
-
"dynamic_template_data": {
|
94
|
-
"subject": "Testing Templates",
|
95
|
-
"name": "Example User",
|
96
|
-
"city": "Denver"
|
97
|
-
}
|
98
|
-
}
|
99
|
-
],
|
100
|
-
"from": {
|
101
|
-
"email": "test@example.com"
|
102
|
-
},
|
103
|
-
"template_id": "d-2c214ac919e84170b21855cc129b4a5f"
|
104
|
-
}')
|
105
|
-
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
106
|
-
begin
|
107
|
-
response = sg.client.mail._("send").post(request_body: data)
|
108
|
-
rescue Exception => e
|
109
|
-
puts e.message
|
110
|
-
end
|
111
|
-
puts response.status_code
|
112
|
-
puts response.body
|
113
|
-
puts response.parsed_body
|
114
|
-
puts response.headers
|
115
|
-
```
|
116
|
-
|
117
|
-
<a name="legacy-templates"></a>
|
118
|
-
# Legacy Templates
|
119
|
-
|
120
|
-
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.
|
121
|
-
|
122
|
-
Template ID (replace with your own):
|
123
|
-
|
124
|
-
```text
|
125
|
-
13b8f94f-bcae-4ec6-b752-70d6cb59f932
|
126
|
-
```
|
127
|
-
|
128
|
-
Email Subject:
|
129
|
-
|
130
|
-
```text
|
131
|
-
<%subject%>
|
132
|
-
```
|
133
|
-
|
134
|
-
Template Body:
|
135
|
-
|
136
|
-
```html
|
137
|
-
<html>
|
138
|
-
<head>
|
139
|
-
<title></title>
|
140
|
-
</head>
|
141
|
-
<body>
|
142
|
-
Hello -name-,
|
143
|
-
<br /><br/>
|
144
|
-
I'm glad you are trying out the template feature!
|
145
|
-
<br /><br/>
|
146
|
-
<%body%>
|
147
|
-
<br /><br/>
|
148
|
-
I hope you are having a great day in -city- :)
|
149
|
-
<br /><br/>
|
150
|
-
</body>
|
151
|
-
</html>
|
152
|
-
```
|
153
|
-
|
154
|
-
## With Mail Helper Class
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
require 'sendgrid-ruby'
|
158
|
-
include SendGrid
|
159
|
-
|
160
|
-
mail = SendGrid::Mail.new
|
161
|
-
mail.from = Email.new(email: 'test@example.com')
|
162
|
-
mail.subject = 'I\'m replacing the subject tag'
|
163
|
-
personalization = Personalization.new
|
164
|
-
personalization.add_to(Email.new(email: 'test@example.com'))
|
165
|
-
personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User'))
|
166
|
-
personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver'))
|
167
|
-
mail.add_personalization(personalization)
|
168
|
-
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
|
169
|
-
|
170
|
-
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
171
|
-
begin
|
172
|
-
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
173
|
-
rescue Exception => e
|
174
|
-
puts e.message
|
175
|
-
end
|
176
|
-
puts response.status_code
|
177
|
-
puts response.body
|
178
|
-
puts response.parsed_body
|
179
|
-
puts response.headers
|
180
|
-
```
|
181
|
-
|
182
|
-
## Without Mail Helper Class
|
183
|
-
|
184
|
-
```ruby
|
185
|
-
require 'sendgrid-ruby'
|
186
|
-
include SendGrid
|
187
|
-
|
188
|
-
data = JSON.parse('{
|
189
|
-
"personalizations": [
|
190
|
-
{
|
191
|
-
"to": [
|
192
|
-
{
|
193
|
-
"email": "test@example.com"
|
194
|
-
}
|
195
|
-
],
|
196
|
-
"substitutions": {
|
197
|
-
"-name-": "Example User",
|
198
|
-
"-city-": "Denver"
|
199
|
-
},
|
200
|
-
"subject": "I\'m replacing the subject tag"
|
201
|
-
}
|
202
|
-
],
|
203
|
-
"from": {
|
204
|
-
"email": "test@example.com"
|
205
|
-
},
|
206
|
-
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
|
207
|
-
}')
|
208
|
-
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
209
|
-
begin
|
210
|
-
response = sg.client.mail._("send").post(request_body: data)
|
211
|
-
rescue Exception => e
|
212
|
-
puts e.message
|
213
|
-
end
|
214
|
-
puts response.status_code
|
215
|
-
puts response.body
|
216
|
-
puts response.parsed_body
|
217
|
-
puts response.headers
|
218
|
-
```
|
219
|
-
|
220
|
-
## Adding Attachments
|
221
|
-
|
222
|
-
```ruby
|
223
|
-
attachment = Attachment.new
|
224
|
-
attachment.content = Base64.strict_encode64(File.open(fpath, 'rb').read)
|
225
|
-
attachment.type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
226
|
-
attachment.filename = fname
|
227
|
-
attachment.disposition = 'attachment'
|
228
|
-
attachment.content_id = 'Reports Sheet'
|
229
|
-
mail.add_attachment(attachment)
|
230
|
-
|
231
|
-
```
|
232
|
-
Attachments must be base64 encoded, using Base64's strict_encode64 where no line feeds are added.
|
233
|
-
|
234
|
-
<a name="domain-authentication"></a>
|
235
|
-
# How to Setup a Domain Authentication
|
236
|
-
|
237
|
-
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](https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/client/USAGE.md#sender-authentication).
|
238
|
-
|
239
|
-
Find more information about all of SendGrid's authentication related documentation [here](https://sendgrid.com/docs/ui/account-and-settings/).
|
240
|
-
|
241
|
-
<a name="email-statistics"></a>
|
242
|
-
# How to View Email Statistics
|
243
|
-
|
244
|
-
You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](https://github.com/sendgrid/sendgrid-ruby/blob/master/USAGE.md#stats).
|
245
|
-
|
246
|
-
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.
|
247
|
-
|
248
|
-
You can also use the email statistics helper to make it easier to interact with the API.
|
249
|
-
|
250
|
-
```ruby
|
251
|
-
require 'sendgrid-ruby'
|
252
|
-
require 'date'
|
253
|
-
|
254
|
-
include SendGrid
|
255
|
-
|
256
|
-
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
|
257
|
-
stats = SendGrid::EmailStats.new(sendgrid_client: sg_client)
|
258
|
-
|
259
|
-
# Fetch stats by day, between 2 dates
|
260
|
-
from = Date.new(2017, 10, 01)
|
261
|
-
to = Date.new(2017, 10, 12)
|
262
|
-
|
263
|
-
email_stats = stats.by_day(from, to)
|
264
|
-
|
265
|
-
email_stats.metrics
|
266
|
-
|
267
|
-
if !email_stats.error?
|
268
|
-
email_stats.metrics.each do |metric|
|
269
|
-
puts "Date - #{metric.date}"
|
270
|
-
puts "Number of Requests - #{metric.requests}"
|
271
|
-
puts "Bounces - #{metric.bounces}"
|
272
|
-
puts "Opens - #{metric.opens}"
|
273
|
-
puts "Clicks - #{metric.clicks}"
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
# Fetch stats by week, between 2 dates for a category
|
278
|
-
from = Date.new(2017, 10, 01)
|
279
|
-
to = Date.new(2017, 10, 12)
|
280
|
-
category = 'abcd'
|
281
|
-
|
282
|
-
email_stats = stats.by_week(from, to, category)
|
283
|
-
|
284
|
-
if !email_stats.error?
|
285
|
-
email_stats.metrics.each do |metric|
|
286
|
-
puts "Date - #{metric.date}"
|
287
|
-
puts "Number of Requests - #{metric.requests}"
|
288
|
-
puts "Bounces - #{metric.bounces}"
|
289
|
-
puts "Opens - #{metric.opens}"
|
290
|
-
puts "Clicks - #{metric.clicks}"
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
```
|
295
|
-
|
296
|
-
# Send an Email With Twilio Email (Pilot)
|
297
|
-
|
298
|
-
### 1. Obtain a Free Twilio Account
|
299
|
-
|
300
|
-
Sign up for a free Twilio account [here](https://www.twilio.com/try-twilio?source=sendgrid-ruby).
|
301
|
-
|
302
|
-
### 2. Set Up Your Environment Variables
|
303
|
-
|
304
|
-
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).
|
305
|
-
|
306
|
-
Once you have those, follow the steps below based on your operating system.
|
307
|
-
|
308
|
-
#### Linux/Mac
|
309
|
-
|
310
|
-
```bash
|
311
|
-
echo "export TWILIO_API_KEY='YOUR_TWILIO_API_KEY'" > twilio.env
|
312
|
-
echo "export TWILIO_API_SECRET='YOUR_TWILIO_API_SECRET'" >> twilio.env
|
313
|
-
|
314
|
-
# or
|
315
|
-
|
316
|
-
echo "export TWILIO_ACCOUNT_SID='YOUR_TWILIO_ACCOUNT_SID'" > twilio.env
|
317
|
-
echo "export TWILIO_AUTH_TOKEN='YOUR_TWILIO_AUTH_TOKEN'" >> twilio.env
|
318
|
-
```
|
319
|
-
|
320
|
-
Then:
|
321
|
-
|
322
|
-
```bash
|
323
|
-
echo "twilio.env" >> .gitignore
|
324
|
-
source ./twilio.env
|
325
|
-
```
|
326
|
-
|
327
|
-
#### Windows
|
328
|
-
|
329
|
-
Temporarily set the environment variable (accessible only during the current CLI session):
|
330
|
-
|
331
|
-
```bash
|
332
|
-
set TWILIO_API_KEY=YOUR_TWILIO_API_KEY
|
333
|
-
set TWILIO_API_SECRET=YOUR_TWILIO_API_SECRET
|
334
|
-
|
335
|
-
: or
|
336
|
-
|
337
|
-
set TWILIO_ACCOUNT_SID=YOUR_TWILIO_ACCOUNT_SID
|
338
|
-
set TWILIO_AUTH_TOKEN=YOUR_TWILIO_AUTH_TOKEN
|
339
|
-
```
|
340
|
-
|
341
|
-
Or permanently set the environment variable (accessible in all subsequent CLI sessions):
|
342
|
-
|
343
|
-
```bash
|
344
|
-
setx TWILIO_API_KEY "YOUR_TWILIO_API_KEY"
|
345
|
-
setx TWILIO_API_SECRET "YOUR_TWILIO_API_SECRET"
|
346
|
-
|
347
|
-
: or
|
348
|
-
|
349
|
-
setx TWILIO_ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
|
350
|
-
setx TWILIO_AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
|
351
|
-
```
|
352
|
-
|
353
|
-
### 3. Initialize the Twilio Email Client
|
354
|
-
|
355
|
-
```ruby
|
356
|
-
mail_client = TwilioEmail::API(username: ENV['TWILIO_API_KEY'], password: ENV['TWILIO_API_SECRET'])
|
357
|
-
|
358
|
-
# or
|
359
|
-
|
360
|
-
mail_client = TwilioEmail::API(username: ENV['TWILIO_ACCOUNT_SID'], password: ENV['TWILIO_AUTH_TOKEN'])
|
361
|
-
```
|
362
|
-
|
363
|
-
This client has the same interface as the `SendGrid::API` client.
|
364
|
-
|
365
|
-
# Send an SMS Message
|
366
|
-
|
367
|
-
First, follow the above steps for creating a Twilio account and setting up environment variables with the proper credentials.
|
368
|
-
|
369
|
-
Then, install the Twilio Helper Library. Add this line to your application's Gemfile:
|
370
|
-
|
371
|
-
```bash
|
372
|
-
gem 'twilio-ruby'
|
373
|
-
```
|
374
|
-
|
375
|
-
And then execute:
|
376
|
-
|
377
|
-
```bash
|
378
|
-
bundle
|
379
|
-
```
|
380
|
-
|
381
|
-
Or install it yourself using:
|
382
|
-
|
383
|
-
```bash
|
384
|
-
gem install twilio-ruby
|
385
|
-
```
|
386
|
-
|
387
|
-
Finally, send a message.
|
388
|
-
|
389
|
-
```ruby
|
390
|
-
require 'twilio-ruby'
|
391
|
-
|
392
|
-
# put your own credentials here
|
393
|
-
account_sid = ENV['TWILIO_ACCOUNT_SID']
|
394
|
-
auth_token = ENV['TWILIO_AUTH_TOKEN']
|
395
|
-
|
396
|
-
# set up a client to talk to the Twilio REST API
|
397
|
-
@client = Twilio::REST::Client.new account_sid, auth_token
|
398
|
-
@client.api.account.messages.create(
|
399
|
-
from: '+14159341234',
|
400
|
-
to: '+16105557069',
|
401
|
-
body: 'Hey there!'
|
402
|
-
)
|
403
|
-
```
|
404
|
-
|
405
|
-
For more information, please visit the [Twilio SMS Ruby documentation](https://www.twilio.com/docs/sms/quickstart/ruby).
|