sendgrid-ruby 4.0.8 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +21 -0
- data/.env_sample +1 -0
- data/.github/PULL_REQUEST_TEMPLATE +26 -0
- data/.gitignore +1 -0
- data/.travis.yml +11 -2
- data/CHANGELOG.md +119 -3
- data/CODE_OF_CONDUCT.md +41 -0
- data/CONTRIBUTING.md +34 -30
- data/Gemfile +4 -1
- data/LICENSE.txt +1 -1
- data/README.md +53 -24
- data/Rakefile +7 -2
- data/TROUBLESHOOTING.md +41 -15
- data/UPGRADE.md +5 -0
- data/USAGE.md +1130 -1129
- data/USE_CASES.md +280 -16
- data/config.ru +4 -0
- data/docker/Dockerfile +12 -0
- data/docker/README.md +30 -0
- data/examples/helpers/mail/example.rb +62 -45
- data/examples/helpers/settings/example.rb +23 -0
- data/examples/helpers/stats/example.rb +42 -0
- data/examples/ips/ips.rb +13 -0
- data/examples/mail/mail.rb +2 -2
- data/examples/scopes/scopes.rb +49 -3
- data/examples/{whitelabel/whitelabel.rb → senderauthentication/senderauthentication.rb} +27 -27
- data/examples/suppression/suppression.rb +10 -10
- data/gemfiles/Sinatra_1.gemfile +6 -0
- data/gemfiles/Sinatra_2.gemfile +6 -0
- data/lib/sendgrid-ruby.rb +27 -1
- data/lib/sendgrid/client.rb +12 -9
- 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 +1 -1
- data/lib/sendgrid/helpers/mail/asm.rb +33 -0
- data/lib/sendgrid/helpers/mail/attachment.rb +86 -0
- data/lib/sendgrid/helpers/mail/bcc_settings.rb +33 -0
- data/lib/sendgrid/helpers/mail/bypass_list_management.rb +43 -0
- data/lib/sendgrid/helpers/mail/category.rb +20 -0
- data/lib/sendgrid/helpers/mail/click_tracking.rb +33 -0
- data/lib/sendgrid/helpers/mail/content.rb +20 -0
- data/lib/sendgrid/helpers/mail/custom_arg.rb +24 -0
- data/lib/sendgrid/helpers/mail/email.rb +29 -0
- data/lib/sendgrid/helpers/mail/footer.rb +43 -0
- data/lib/sendgrid/helpers/mail/ganalytics.rb +74 -0
- data/lib/sendgrid/helpers/mail/header.rb +24 -0
- data/lib/sendgrid/helpers/mail/mail.rb +36 -895
- data/lib/sendgrid/helpers/mail/mail_settings.rb +63 -0
- data/lib/sendgrid/helpers/mail/open_tracking.rb +33 -0
- data/lib/sendgrid/helpers/mail/personalization.rb +82 -0
- data/lib/sendgrid/helpers/mail/section.rb +24 -0
- data/lib/sendgrid/helpers/mail/spam_check.rb +43 -0
- data/lib/sendgrid/helpers/mail/subscription_tracking.rb +53 -0
- data/lib/sendgrid/helpers/mail/substitution.rb +24 -0
- data/lib/sendgrid/helpers/mail/tracking_settings.rb +53 -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 +24 -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 +35 -0
- data/lib/sendgrid/helpers/stats/stats_response.rb +31 -0
- data/lib/sendgrid/version.rb +2 -2
- data/mail_helper_v3.md +390 -0
- data/sendgrid-ruby.gemspec +12 -5
- 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 +112 -0
- data/spec/sendgrid/helpers/stats/metrics_spec.rb +46 -0
- data/spec/sendgrid/helpers/stats/stats_response_spec.rb +76 -0
- data/spec/spec_helper.rb +10 -0
- data/test/sendgrid/helpers/mail/test_attachment.rb +35 -0
- data/test/sendgrid/helpers/mail/test_category.rb +27 -0
- data/test/sendgrid/helpers/mail/test_email.rb +34 -0
- data/test/sendgrid/helpers/mail/test_mail.rb +198 -59
- data/test/sendgrid/helpers/mail/test_personalizations.rb +161 -0
- data/test/sendgrid/permissions/test_scopes.rb +38 -0
- data/test/sendgrid/test_sendgrid-ruby.rb +90 -9
- metadata +170 -10
@@ -0,0 +1,14 @@
|
|
1
|
+
**This module allows you to quickly and easily build a Settings object for retrieving or updating your Twilio SendGrid Settings.**
|
2
|
+
|
3
|
+
# Quick Start
|
4
|
+
|
5
|
+
Run the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) (make sure you have set your environment variable to include your SENDGRID_API_KEY).
|
6
|
+
|
7
|
+
```bash
|
8
|
+
ruby examples/helpers/settings/example.rb
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
- See the [example](https://github.com/sendgrid/sendgrid-ruby/tree/master/examples/helpers/settings) for a complete working example.
|
14
|
+
- [Documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/Settings/index.html)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SendGrid
|
2
|
+
class MailSettingsDto
|
3
|
+
attr_reader :bcc, :address_whitelist, :bounce_purge, :footer, :forward_spam, :forward_bounce, :plain_content, :spam_check, :template
|
4
|
+
|
5
|
+
def self.fetch(sendgrid_client:, name:, query_params:)
|
6
|
+
sendgrid_client.mail_settings.public_send(name).get(query_params: query_params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.update(sendgrid_client:, name:, request_body:)
|
10
|
+
sendgrid_client.mail_settings.public_send(name).patch(request_body: request_body)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SendGrid
|
2
|
+
class PartnerSettingsDto
|
3
|
+
attr_reader :new_relic
|
4
|
+
|
5
|
+
def self.fetch(sendgrid_client:, name:, query_params:)
|
6
|
+
sendgrid_client.partner_settings.public_send(name).get(query_params: query_params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.update(sendgrid_client:, name:, request_body:)
|
10
|
+
sendgrid_client.partner_settings.public_send(name).patch(request_body: request_body)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'mail_settings_dto'
|
2
|
+
require_relative 'partner_settings_dto'
|
3
|
+
require_relative 'tracking_settings_dto'
|
4
|
+
require_relative 'user_settings_dto'
|
5
|
+
|
6
|
+
module SendGrid
|
7
|
+
class Settings
|
8
|
+
attr_accessor :sendgrid_client
|
9
|
+
|
10
|
+
SETTING_TYPES = [SendGrid::MailSettingsDto, SendGrid::TrackingSettingsDto,
|
11
|
+
SendGrid::PartnerSettingsDto, SendGrid::UserSettingsDto]
|
12
|
+
|
13
|
+
def initialize(sendgrid_client:)
|
14
|
+
@sendgrid_client = sendgrid_client
|
15
|
+
end
|
16
|
+
|
17
|
+
SETTING_TYPES.each do |setting_type|
|
18
|
+
setting_type.instance_methods(false).each do |name|
|
19
|
+
define_method(name) do |**args|
|
20
|
+
setting_type.fetch(sendgrid_client: sendgrid_client, name: name, query_params: args)
|
21
|
+
end
|
22
|
+
define_method("update_#{name}") do |**args|
|
23
|
+
setting_type.update(sendgrid_client: sendgrid_client, name: name, request_body: args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SendGrid
|
2
|
+
class TrackingSettingsDto
|
3
|
+
attr_reader :open, :click, :google_analytics, :subscription
|
4
|
+
alias :click_tracking :click
|
5
|
+
alias :open_tracking :open
|
6
|
+
alias :subscription_tracking :subscription
|
7
|
+
|
8
|
+
def self.fetch(sendgrid_client:, name:, query_params:)
|
9
|
+
name = scrub_alias_names(name.to_s)
|
10
|
+
sendgrid_client.tracking_settings.public_send(name).get(query_params: query_params)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.update(sendgrid_client:, name:, request_body:)
|
14
|
+
name = scrub_alias_names(name.to_s)
|
15
|
+
sendgrid_client.tracking_settings.public_send(name).patch(request_body: request_body)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def self.scrub_alias_names(name)
|
21
|
+
name.gsub(/_tracking/, '')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SendGrid
|
2
|
+
class UserSettingsDto
|
3
|
+
attr_reader :enforced_tls
|
4
|
+
|
5
|
+
def self.fetch(sendgrid_client:, name:, query_params:)
|
6
|
+
sendgrid_client.user.settings.public_send(name).get(query_params: query_params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.update(sendgrid_client:, name:, request_body:)
|
10
|
+
sendgrid_client.user.settings.public_send(name).patch(request_body: request_body)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
class EmailStats
|
5
|
+
def initialize(args)
|
6
|
+
@sendgrid_client = args[:sendgrid_client]
|
7
|
+
end
|
8
|
+
|
9
|
+
def by_day(start_date, end_date, categories = nil, subusers = nil)
|
10
|
+
get('day', start_date, end_date, categories, subusers)
|
11
|
+
end
|
12
|
+
|
13
|
+
def by_week(start_date, end_date, categories = nil, subusers = nil)
|
14
|
+
get('week', start_date, end_date, categories, subusers)
|
15
|
+
end
|
16
|
+
|
17
|
+
def by_month(start_date, end_date, categories = nil, subusers = nil)
|
18
|
+
get('month', start_date, end_date, categories, subusers)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(aggregated_by, start_date, end_date, categories = nil, subusers = nil)
|
22
|
+
params = query_params(aggregated_by, start_date, end_date, categories, subusers)
|
23
|
+
|
24
|
+
response_body = @sendgrid_client.stats.get(query_params: params).body
|
25
|
+
build_response(response_body)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def query_params(aggregated_by, start_date, end_date, categories, subusers)
|
31
|
+
params = {
|
32
|
+
aggregated_by: aggregated_by,
|
33
|
+
start_date: start_date,
|
34
|
+
end_date: end_date
|
35
|
+
}
|
36
|
+
params.merge(categories: categories) if categories
|
37
|
+
params.merge(subusers: subusers) if subusers
|
38
|
+
params
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_response(response_body)
|
42
|
+
response_json = JSON.parse(response_body)
|
43
|
+
StatsResponse.new(response_json)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
class Metrics
|
5
|
+
attr_reader :blocks, :bounce_drops,
|
6
|
+
:bounces, :clicks, :deferred, :delivered,
|
7
|
+
:invalid_emails, :opens, :processed, :requests,
|
8
|
+
:spam_report_drops, :spam_reports, :unique_clicks,
|
9
|
+
:unique_opens, :unsubscribe_drops, :unsubscribes
|
10
|
+
|
11
|
+
def initialize(args={})
|
12
|
+
@date = args['date']
|
13
|
+
@blocks = args['blocks']
|
14
|
+
@bounce_drops = args['bounce_drops']
|
15
|
+
@bounces = args['bounces']
|
16
|
+
@clicks = args['clicks']
|
17
|
+
@deferred = args['deferred']
|
18
|
+
@delivered = args['delivered']
|
19
|
+
@invalid_emails = args['invalid_emails']
|
20
|
+
@opens = args['opens']
|
21
|
+
@processed = args['processed']
|
22
|
+
@requests = args['requests']
|
23
|
+
@spam_report_drops = args['spam_report_drops']
|
24
|
+
@spam_reports = args['spam_reports']
|
25
|
+
@unique_clicks = args['unique_clicks']
|
26
|
+
@unique_opens = args['unique_opens']
|
27
|
+
@unsubscribe_drops = args['unsubscribe_drops']
|
28
|
+
@unsubscribes = args['unsubscribes']
|
29
|
+
end
|
30
|
+
|
31
|
+
def date
|
32
|
+
Date.parse(@date)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
class StatsResponse
|
5
|
+
def initialize(args)
|
6
|
+
@errors = args['errors'] if args.is_a? Hash
|
7
|
+
@stats = args if args.is_a? Array
|
8
|
+
end
|
9
|
+
|
10
|
+
def errors
|
11
|
+
@errors.map do |error|
|
12
|
+
error['message']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def error?
|
17
|
+
!@errors.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def metrics
|
21
|
+
@stats.flat_map do |stat|
|
22
|
+
starting_date = stat['date']
|
23
|
+
all_stats_for_date = stat['stats']
|
24
|
+
|
25
|
+
all_stats_for_date.map do |metric|
|
26
|
+
Metrics.new(metric['metrics'].merge('date' => starting_date))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/sendgrid/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module SendGrid
|
2
|
-
VERSION = '
|
3
|
-
end
|
2
|
+
VERSION = '6.0.0'
|
3
|
+
end
|
data/mail_helper_v3.md
ADDED
@@ -0,0 +1,390 @@
|
|
1
|
+
Hello!
|
2
|
+
|
3
|
+
It is now time to implement the final piece of our v2 to v3 migration. Before we dig into writing the code, we would love to get feedback on the following proposed interfaces.
|
4
|
+
|
5
|
+
We are starting with the use cases below for the first iteration. (we have completed this work on [our C# library](https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md), you can check that out for a sneak peek of where we are heading).
|
6
|
+
|
7
|
+
# Send a Single Email to a Single Recipient
|
8
|
+
|
9
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'sendgrid-ruby'
|
13
|
+
|
14
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
15
|
+
to = SendGrid::Email.new('test@example.com', 'Example User')
|
16
|
+
subject = 'Sending with Twilio SendGrid is Fun'
|
17
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
18
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
19
|
+
msg = SendGrid::Mail.create(from: from,
|
20
|
+
tos: to,
|
21
|
+
subject: subject,
|
22
|
+
plain_text_content: plain_text_content,
|
23
|
+
html_content: html_content,
|
24
|
+
substitutions: {})
|
25
|
+
|
26
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
27
|
+
|
28
|
+
begin
|
29
|
+
response = client.send_email(msg)
|
30
|
+
rescue Exception => e
|
31
|
+
puts e.message
|
32
|
+
end
|
33
|
+
puts response.status_code
|
34
|
+
puts response.body
|
35
|
+
puts response.headers
|
36
|
+
```
|
37
|
+
|
38
|
+
# Send a Single Email to Multiple Recipients
|
39
|
+
|
40
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'sendgrid-ruby'
|
44
|
+
|
45
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
46
|
+
tos = [
|
47
|
+
SendGrid::Email.new('test1@example.com', 'Example User1'),
|
48
|
+
SendGrid::Email.new('test2@example.com', 'Example User2'),
|
49
|
+
SendGrid::Email.new('test3@example.com', 'Example User3')
|
50
|
+
];
|
51
|
+
subject = 'Sending with Twilio SendGrid is Fun'
|
52
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
53
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
54
|
+
msg = SendGrid::Mail.create(from: from,
|
55
|
+
tos: tos,
|
56
|
+
subject: subject,
|
57
|
+
plain_text_content: plain_text_content,
|
58
|
+
html_content: html_content,
|
59
|
+
substitutions: {})
|
60
|
+
|
61
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
62
|
+
|
63
|
+
begin
|
64
|
+
response = client.send_email(msg)
|
65
|
+
rescue Exception => e
|
66
|
+
puts e.message
|
67
|
+
end
|
68
|
+
puts response.status_code
|
69
|
+
puts response.body
|
70
|
+
puts response.headers
|
71
|
+
```
|
72
|
+
|
73
|
+
# Send Multiple Emails to Multiple Recipients
|
74
|
+
|
75
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
require 'sendgrid-ruby'
|
79
|
+
|
80
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
81
|
+
tos = [
|
82
|
+
SendGrid::Email.new('test1@example.com', 'Example User1'),
|
83
|
+
SendGrid::Email.new('test2@example.com', 'Example User2'),
|
84
|
+
SendGrid::Email.new('test3@example.com', 'Example User3')
|
85
|
+
];
|
86
|
+
subjects = [
|
87
|
+
'Sending with Twilio SendGrid is Fun',
|
88
|
+
'Sending with Twilio SendGrid is Super Fun',
|
89
|
+
'Sending with Twilio SendGrid is Super Duper Fun'
|
90
|
+
];
|
91
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
92
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
93
|
+
values = [
|
94
|
+
'Name 1',
|
95
|
+
'Name 2',
|
96
|
+
'Name 3'
|
97
|
+
]
|
98
|
+
substitutions = {
|
99
|
+
'-name1-' => values
|
100
|
+
}
|
101
|
+
msg = SendGrid::Mail.create(from: from,
|
102
|
+
tos: tos,
|
103
|
+
subject: subjects,
|
104
|
+
plain_text_content: plain_text_content,
|
105
|
+
html_content: html_content,
|
106
|
+
substitutions: substitutions)
|
107
|
+
|
108
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
109
|
+
|
110
|
+
begin
|
111
|
+
response = client.send_email(msg)
|
112
|
+
rescue Exception => e
|
113
|
+
puts e.message
|
114
|
+
end
|
115
|
+
puts response.status_code
|
116
|
+
puts response.body
|
117
|
+
puts response.headers
|
118
|
+
```
|
119
|
+
|
120
|
+
# Kitchen Sink - an example with all settings used
|
121
|
+
|
122
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
126
|
+
|
127
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
128
|
+
to = SendGrid::Email.new('test@example.com', 'Example User')
|
129
|
+
subject = 'Sending with Twilio SendGrid is Fun'
|
130
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
131
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
132
|
+
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
|
133
|
+
|
134
|
+
# For a detailed description of each of these settings, please see the [documentation](https://sendgrid.com/docs/API_Reference/api_v3.html).
|
135
|
+
|
136
|
+
msg.add_to(SendGrid::Email.new('test1@example.com', 'Example User1'))
|
137
|
+
to_emails = [
|
138
|
+
SendGrid::Email.new('test2@example.com', 'Example User2'),
|
139
|
+
SendGrid::Email.new('test3@example.com', 'Example User3')
|
140
|
+
];
|
141
|
+
msg.add_tos(to_emails)
|
142
|
+
|
143
|
+
msg.add_cc(SendGrid::Email.new('test4@example.com', 'Example User4'))
|
144
|
+
cc_emails = [
|
145
|
+
SendGrid::Email.new('test5@example.com', 'Example User5'),
|
146
|
+
SendGrid::Email.new('test6@example.com', 'Example User6')
|
147
|
+
];
|
148
|
+
msg.add_ccs(cc_emails)
|
149
|
+
|
150
|
+
msg.add_bcc(SendGrid::Email.new('test7@example.com', 'Example User7'))
|
151
|
+
bcc_emails = [
|
152
|
+
SendGrid::Email.new('test8@example.com', 'Example User8'),
|
153
|
+
SendGrid::Email.new('test9@example.com', 'Example User9')
|
154
|
+
];
|
155
|
+
msg.add_bccs(bcc_emails)
|
156
|
+
|
157
|
+
msg.add_header('X-Test1', 'Test1')
|
158
|
+
msg.add_header('X-Test2', 'Test2')
|
159
|
+
headers = [
|
160
|
+
'X-Test3' => 'Test3',
|
161
|
+
'X-Test4' => 'Test4'
|
162
|
+
]
|
163
|
+
msg.add_headers(headers)
|
164
|
+
|
165
|
+
msg.add_substitution('%name1%', 'Example Name 1');
|
166
|
+
msg.add_substitution('%city1%', 'Denver');
|
167
|
+
substitutions = [
|
168
|
+
'%name2%' => 'Example Name 2',
|
169
|
+
'%city2%' => 'Orange'
|
170
|
+
]
|
171
|
+
msg.add_substitutions(substitutions)
|
172
|
+
|
173
|
+
msg.add_custom_arg('marketing1', 'false')
|
174
|
+
msg.add_custom_arg('transactional1', 'true')
|
175
|
+
custom_args = [
|
176
|
+
'marketing2' => 'true',
|
177
|
+
'transactional2' => 'false'
|
178
|
+
]
|
179
|
+
msg.add_custom_args(custom_args)
|
180
|
+
|
181
|
+
msg.set_send_at(1461775051)
|
182
|
+
|
183
|
+
msg.set_subject('this subject overrides the Global Subject on the default Personalization')
|
184
|
+
|
185
|
+
# If you need to add more [Personalizations](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html), here is an example of adding another Personalization by passing in a personalization index.
|
186
|
+
|
187
|
+
msg.add_to(SendGrid::Email.new('test10@example.com', 'Example User10'), 1)
|
188
|
+
to_emails = [
|
189
|
+
SendGrid::Email.new('test11@example.com', 'Example User11'),
|
190
|
+
SendGrid::Email.new('test12@example.com', 'Example User12')
|
191
|
+
];
|
192
|
+
msg.add_tos(to_emails, 1)
|
193
|
+
|
194
|
+
msg.add_cc(SendGrid::Email.new('test13@example.com', 'Example User13'), 1)
|
195
|
+
cc_emails = [
|
196
|
+
SendGrid::Email.new('test14@example.com', 'Example User14'),
|
197
|
+
SendGrid::Email.new('test15@example.com', 'Example User15')
|
198
|
+
];
|
199
|
+
msg.add_ccs(cc_emails, 1)
|
200
|
+
|
201
|
+
msg.add_bcc(SendGrid::Email.new('test16@example.com', 'Example User16'), 1)
|
202
|
+
bcc_emails = [
|
203
|
+
SendGrid::Email.new('test17@example.com', 'Example User17'),
|
204
|
+
SendGrid::Email.new('test18@example.com', 'Example User18')
|
205
|
+
];
|
206
|
+
msg.add_bccs(bcc_emails, 1)
|
207
|
+
|
208
|
+
msg.add_header('X-Test5', 'Test5', 1)
|
209
|
+
msg.add_header('X-Test6', 'Test6', 1)
|
210
|
+
headers = [
|
211
|
+
'X-Test7' => 'Test7',
|
212
|
+
'X-Test8' => 'Test8'
|
213
|
+
]
|
214
|
+
msg.add_headers(headers, 1)
|
215
|
+
|
216
|
+
msg.add_substitution('%name3%', 'Example Name 3', 1);
|
217
|
+
msg.add_substitution('%city3%', 'Redwood City', 1);
|
218
|
+
substitutions = [
|
219
|
+
'%name4%' => 'Example Name 4',
|
220
|
+
'%city4%' => 'London'
|
221
|
+
]
|
222
|
+
msg.add_substitutions(substitutions, 1)
|
223
|
+
|
224
|
+
msg.add_custom_arg('marketing3', 'true', 1)
|
225
|
+
msg.add_custom_arg('transactional3', 'false', 1)
|
226
|
+
custom_args = [
|
227
|
+
'marketing4' => 'false',
|
228
|
+
'transactional4' => 'true'
|
229
|
+
]
|
230
|
+
msg.add_custom_args(custom_args, 1)
|
231
|
+
|
232
|
+
msg.set_send_at(1461775052, 1)
|
233
|
+
|
234
|
+
msg.set_subject('this subject overrides the Global Subject on the second Personalization', 1)
|
235
|
+
|
236
|
+
# The values below this comment are global to entire message
|
237
|
+
|
238
|
+
msg.set_from(SendGrid::Email.new('test0@example.com', 'Example User0'))
|
239
|
+
|
240
|
+
msg.set_global_subject('Sending with Twilio SendGrid is Fun');
|
241
|
+
|
242
|
+
msg.add_content(MimeType::Text, 'and easy to do anywhere, even with Ruby')
|
243
|
+
msg.add_content(MimeType::Html, '<strong>and easy to do anywhere, even with Ruby</strong>')
|
244
|
+
contents = [
|
245
|
+
SendGrid::Content.new('text/calendar', 'Party Time!!'),
|
246
|
+
SendGrid::Content.new('text/calendar2', 'Party Time 2!!')
|
247
|
+
]
|
248
|
+
msg.add_contents(contents)
|
249
|
+
|
250
|
+
msg.add_attachment('balance_001.pdf',
|
251
|
+
'base64 encoded content',
|
252
|
+
'application/pdf',
|
253
|
+
'attachment',
|
254
|
+
'Balance Sheet')
|
255
|
+
|
256
|
+
attachments = [
|
257
|
+
SendGrid::Attachment.new('banner.png',
|
258
|
+
'base64 encoded content',
|
259
|
+
'image/png',
|
260
|
+
'inline',
|
261
|
+
'Banner'),
|
262
|
+
SendGrid::Attachment.new('banner2.png',
|
263
|
+
'base64 encoded content',
|
264
|
+
'image/png',
|
265
|
+
'inline',
|
266
|
+
'Banner 2'),
|
267
|
+
]
|
268
|
+
msg.add_attachments(attachments)
|
269
|
+
|
270
|
+
msg.set_template_id('13b8f94f-bcae-4ec6-b752-70d6cb59f932')
|
271
|
+
|
272
|
+
msg.add_global_header('X-Day', 'Monday')
|
273
|
+
global_headers = [
|
274
|
+
'X-Month' => 'January',
|
275
|
+
'X-Year' => '2017'
|
276
|
+
]
|
277
|
+
msg.set_global_headers(global_headers)
|
278
|
+
|
279
|
+
msg.add_section('%section1%', 'Substitution for Section 1 Tag')
|
280
|
+
sections = [
|
281
|
+
'%section2%' => 'Substitution for Section 2 Tag',
|
282
|
+
'%section3%' => 'Substitution for Section 3 Tag'
|
283
|
+
]
|
284
|
+
msg.add_sections(sections)
|
285
|
+
|
286
|
+
begin
|
287
|
+
response = client.send_email(msg)
|
288
|
+
rescue Exception => e
|
289
|
+
puts e.message
|
290
|
+
end
|
291
|
+
puts response.status_code
|
292
|
+
puts response.body
|
293
|
+
puts response.headers
|
294
|
+
```
|
295
|
+
|
296
|
+
# Attachments
|
297
|
+
|
298
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
302
|
+
|
303
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
304
|
+
to = SendGrid::Email.new('test@example.com', 'Example User')
|
305
|
+
subject = 'Sending with Twilio SendGrid is Fun'
|
306
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
307
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
308
|
+
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
|
309
|
+
bytes = File.read('/path/to/the/attachment.pdf')
|
310
|
+
encoded = Base64.encode64(bytes)
|
311
|
+
msg.add_attachment('balance_001.pdf',
|
312
|
+
encoded,
|
313
|
+
'application/pdf',
|
314
|
+
'attachment',
|
315
|
+
'Balance Sheet')
|
316
|
+
|
317
|
+
begin
|
318
|
+
response = client.send_email(msg)
|
319
|
+
rescue Exception => e
|
320
|
+
puts e.message
|
321
|
+
end
|
322
|
+
puts response.status_code
|
323
|
+
puts response.body
|
324
|
+
puts response.headers
|
325
|
+
```
|
326
|
+
|
327
|
+
# Transactional Templates
|
328
|
+
|
329
|
+
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
|
330
|
+
|
331
|
+
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.
|
332
|
+
|
333
|
+
Template ID (replace with your own):
|
334
|
+
|
335
|
+
```text
|
336
|
+
13b8f94f-bcae-4ec6-b752-70d6cb59f932
|
337
|
+
```
|
338
|
+
|
339
|
+
Email Subject:
|
340
|
+
|
341
|
+
```text
|
342
|
+
<%subject%>
|
343
|
+
```
|
344
|
+
|
345
|
+
Template Body:
|
346
|
+
|
347
|
+
```html
|
348
|
+
<html>
|
349
|
+
<head>
|
350
|
+
<title></title>
|
351
|
+
</head>
|
352
|
+
<body>
|
353
|
+
Hello -name-,
|
354
|
+
<br /><br/>
|
355
|
+
I'm glad you are trying out the template feature!
|
356
|
+
<br /><br/>
|
357
|
+
<%body%>
|
358
|
+
<br /><br/>
|
359
|
+
I hope you are having a great day in -city- :)
|
360
|
+
<br /><br/>
|
361
|
+
</body>
|
362
|
+
</html>
|
363
|
+
```
|
364
|
+
|
365
|
+
```ruby
|
366
|
+
client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
|
367
|
+
|
368
|
+
from = SendGrid::Email.new('test@example.com', 'Example User')
|
369
|
+
to = SendGrid::Email.new('test@example.com', 'Example User')
|
370
|
+
subject = 'Sending with Twilio SendGrid is Fun'
|
371
|
+
plain_text_content = 'and easy to do anywhere, even with Ruby'
|
372
|
+
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
|
373
|
+
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
|
374
|
+
|
375
|
+
substitutions = [
|
376
|
+
'-name-' => 'Example User',
|
377
|
+
'-city-' => 'Denver'
|
378
|
+
]
|
379
|
+
msg.add_substitutions(substitutions)
|
380
|
+
msg.set_template_id('13b8f94f-bcae-4ec6-b752-70d6cb59f932')
|
381
|
+
|
382
|
+
begin
|
383
|
+
response = client.send_email(msg)
|
384
|
+
rescue Exception => e
|
385
|
+
puts e.message
|
386
|
+
end
|
387
|
+
puts response.status_code
|
388
|
+
puts response.body
|
389
|
+
puts response.headers
|
390
|
+
```
|