mailjet 1.3.8 → 1.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -15
- data/lib/mailjet/connection.rb +4 -4
- data/lib/mailjet/mailer.rb +87 -32
- data/lib/mailjet/message_delivery.rb +1 -1
- data/lib/mailjet/resource.rb +7 -2
- data/lib/mailjet/resources/newsletter_detailcontent.rb +3 -3
- data/lib/mailjet/resources/template.rb +12 -0
- data/lib/mailjet/resources/template_detailcontent.rb +13 -0
- data/lib/mailjet/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62dc8ec008afcbcb8dbc6fa2f114c9137b346770
|
4
|
+
data.tar.gz: 25ed9aef33563a4235e8e3e70136d2984c46e856
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d916e5f2c30e29b5e7a90dd9ecf24d7212a87543eee25dea1705f0c6332492e5de08e5a92edf2bf7a9418eaaaef6ceff995aa267b997f359717d0aa5bb7b9dc
|
7
|
+
data.tar.gz: 440fe3b06ec6792f7897d3d7949e0ef0a759e83de238db62a856a20e941a5cd539e3f4184056ad60b7639bbac1c38326f8278d7ce0575ef0551ce79ccede7689
|
data/README.md
CHANGED
@@ -49,7 +49,7 @@ Rails ActionMailer integration designed for Rails 3.X and 4.X
|
|
49
49
|
|
50
50
|
IMPORTANT: Mailjet gem switched to API v3, the new API provided by Mailjet. For the wrapper for API v1, check the [v1 branch][v1-branch].
|
51
51
|
|
52
|
-
Every code
|
52
|
+
Every code example can be found in the [Mailjet Documentation][mailjet_doc]
|
53
53
|
|
54
54
|
(Please refer to the [Mailjet Documentation Repository][api_doc] to contribute to the documentation examples)
|
55
55
|
|
@@ -124,24 +124,47 @@ p test.attributes['Sent']
|
|
124
124
|
```
|
125
125
|
|
126
126
|
### Send emails with ActionMailer
|
127
|
-
A quick walkthrough to
|
128
|
-
|
129
|
-
First set your delivery method:
|
127
|
+
A quick walkthrough to use Rails Action Mailer [here](http://guides.rubyonrails.org/action_mailer_basics.html)
|
130
128
|
|
129
|
+
First set your delivery method (here Mailjet SMTP relay servers):
|
131
130
|
```ruby
|
132
131
|
# application.rb or config/environments specific settings, which take precedence
|
133
132
|
config.action_mailer.delivery_method = :mailjet
|
134
133
|
|
135
134
|
```
|
136
135
|
|
137
|
-
Or if you prefer sending messages through
|
138
|
-
|
136
|
+
Or if you prefer sending messages through [Mailjet Send API](http://dev.mailjet.com/guides/#send-transactional-email):
|
139
137
|
```ruby
|
140
138
|
# application.rb
|
141
139
|
config.action_mailer.delivery_method = :mailjet_api
|
142
140
|
```
|
143
141
|
|
144
|
-
You can use mailjet specific options with `delivery_method_options` as detailed in the official [ActionMailer doc][
|
142
|
+
You can use mailjet specific options with `delivery_method_options` as detailed in the official [ActionMailer doc][http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options].
|
143
|
+
|
144
|
+
Supported options are:
|
145
|
+
```ruby
|
146
|
+
* :'mj-prio'
|
147
|
+
* :'mj-campaign'
|
148
|
+
* :'mj-deduplicatecampaign'
|
149
|
+
* :'mj-templatelanguage'
|
150
|
+
* :'mj-templateerrorreporting'
|
151
|
+
* :'mj-templateerrordeliver'
|
152
|
+
* :'mj-templateid'
|
153
|
+
* :'mj-trackopen'
|
154
|
+
* :'mj-trackclick'
|
155
|
+
* :'mj-customid'
|
156
|
+
* :'mj-eventpayload'
|
157
|
+
* :'vars'
|
158
|
+
* :'headers'
|
159
|
+
* :'recipients'
|
160
|
+
```
|
161
|
+
|
162
|
+
Otherwise, you can pass the custom Mailjet SMTP headers directly:
|
163
|
+
```ruby
|
164
|
+
headers['X-MJ-CustomID'] = 'rubyPR_Test_ID_1469790724'
|
165
|
+
headers['X-MJ-EventPayload'] = 'rubyPR_Test_Payload'
|
166
|
+
headers['X-MJ-TemplateLanguage'] = '1'
|
167
|
+
```
|
145
168
|
|
146
169
|
Creating a Mailer:
|
147
170
|
```ruby
|
@@ -167,28 +190,40 @@ class UserMailer < ApplicationMailer
|
|
167
190
|
subject: "This is a nice welcome email")
|
168
191
|
end
|
169
192
|
end
|
193
|
+
```
|
194
|
+
|
195
|
+
Next, create your templates in the views folder:
|
196
|
+
```ruby
|
197
|
+
#app/views/user_mailer/welcome_email.html.erb
|
198
|
+
Hello world in HTML!
|
170
199
|
|
200
|
+
#app/views/user_mailer/welcome_email.text.erb
|
201
|
+
Hello world in plain text!
|
171
202
|
```
|
172
|
-
|
203
|
+
|
204
|
+
There's also the ability to set [Mailjet custom headers](http://dev.mailjet.com/guides/#send-api-json-properties)
|
173
205
|
```ruby
|
174
206
|
#app/mailers/user_mailer.rb
|
175
207
|
class UserMailer < ApplicationMailer
|
176
208
|
def welcome_email()
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
209
|
+
headers['X-MJ-CustomID'] = 'custom value'
|
210
|
+
headers['X-MJ-EventPayload'] = 'custom payload'
|
211
|
+
|
212
|
+
mail(
|
213
|
+
from: "me@mailjet.com",
|
214
|
+
to: "you@mailjet.com",
|
215
|
+
subject: "This is a nice welcome email"
|
216
|
+
)
|
181
217
|
end
|
182
218
|
end
|
183
219
|
```
|
184
220
|
For sending email, you can call the method with a variety of `MessageDelivery` priorities:
|
185
221
|
```ruby
|
186
|
-
#In this example, we are sending immediately
|
222
|
+
# In this example, we are sending the email immediately
|
187
223
|
UserMailer.welcome_email.deliver_now!
|
188
224
|
```
|
189
|
-
For more information on `ActionMailer::MessageDeilvery`, see the documentation [HERE](http://edgeapi.rubyonrails.org/classes/ActionMailer/MessageDelivery.html)
|
190
|
-
|
191
225
|
|
226
|
+
For more information on `ActionMailer::MessageDeilvery`, see the documentation [HERE](http://edgeapi.rubyonrails.org/classes/ActionMailer/MessageDelivery.html)
|
192
227
|
|
193
228
|
## Manage your campaigns
|
194
229
|
|
data/lib/mailjet/connection.rb
CHANGED
@@ -21,17 +21,17 @@ module Mailjet
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def initialize(end_point, api_key, secret_key, options = {})
|
24
|
-
|
24
|
+
# #charles proxy
|
25
25
|
# RestClient.proxy = "http://127.0.0.1:8888"
|
26
|
-
|
27
|
-
|
26
|
+
# #
|
27
|
+
# #Output for debugging
|
28
28
|
# RestClient.log =
|
29
29
|
# Object.new.tap do |proxy|
|
30
30
|
# def proxy.<<(message)
|
31
31
|
# Rails.logger.info message
|
32
32
|
# end
|
33
33
|
# end
|
34
|
-
|
34
|
+
# #
|
35
35
|
adapter_class = options[:adapter_class] || RestClient::Resource
|
36
36
|
self.public_operations = options[:public_operations] || []
|
37
37
|
self.read_only = options[:read_only]
|
data/lib/mailjet/mailer.rb
CHANGED
@@ -1,55 +1,110 @@
|
|
1
1
|
require 'action_mailer'
|
2
2
|
require 'mail'
|
3
|
+
require 'base64'
|
4
|
+
require 'json'
|
3
5
|
|
6
|
+
# Mailjet::Mailer enables to send a Mail::Message via Mailjet SMTP relay servers
|
7
|
+
# User is the API key, and password the API secret
|
4
8
|
class Mailjet::Mailer < ::Mail::SMTP
|
5
9
|
def initialize(options = {})
|
6
|
-
ActionMailer::Base.default(:
|
10
|
+
ActionMailer::Base.default(from: Mailjet.config.default_from) if Mailjet.config.default_from.present?
|
7
11
|
super({
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
12
|
+
address: 'in-v3.mailjet.com',
|
13
|
+
port: 587,
|
14
|
+
authentication: 'plain',
|
15
|
+
user_name: Mailjet.config.api_key,
|
16
|
+
password: Mailjet.config.secret_key,
|
17
|
+
enable_starttls_auto: true
|
14
18
|
}.merge(options))
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
22
|
ActionMailer::Base.add_delivery_method :mailjet, Mailjet::Mailer
|
19
23
|
|
20
|
-
|
21
|
-
|
24
|
+
# Mailjet::APIMailer maps a Mail::Message coming from ActionMailer
|
25
|
+
# To Mailjet Send API (see full documentation here dev.mailjet.com/guides/#send-transactional-email)
|
26
|
+
# Mailjet sends API expects a JSON payload as the input.
|
27
|
+
# The deliver methods maps the Mail::Message attributes to the MailjetSend API JSON expected structure
|
22
28
|
class Mailjet::APIMailer
|
23
29
|
def initialize(options = {})
|
24
|
-
|
25
|
-
|
30
|
+
@delivery_method_options = options.slice(
|
31
|
+
:recipients, :'mj-prio', :'mj-campaign', :'mj-deduplicatecampaign',
|
32
|
+
:'mj-templatelanguage', :'mj-templateerrorreporting', :'mj-templateerrordeliver', :'mj-templateid',
|
33
|
+
:'mj-trackopen', :'mj-trackclick',
|
34
|
+
:'mj-customid', :'mj-eventpayload', :vars, :headers
|
35
|
+
)
|
26
36
|
end
|
27
37
|
|
28
38
|
def deliver!(mail)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
content =
|
39
|
+
content = {}
|
40
|
+
|
41
|
+
if mail.text_part
|
42
|
+
content[:text_part] = mail.text_part.try(:decoded)
|
43
|
+
content[:text] = mail.text_part.try(:decoded)
|
44
|
+
end
|
45
|
+
|
46
|
+
if mail.html_part
|
47
|
+
content[:html_part] = mail.html_part.try(:decoded)
|
48
|
+
content[:html] = mail.html_part.try(:decoded)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Formatting attachments (inline + regular)
|
52
|
+
unless mail.attachments.empty?
|
53
|
+
content[:attachments] = []
|
54
|
+
content[:inline_attachments] = []
|
55
|
+
|
56
|
+
mail.attachments.each do |attachment|
|
57
|
+
mailjet_attachment = {
|
58
|
+
'Content-Type' => attachment.content_type.split(';')[0],
|
59
|
+
'Filename' => attachment.filename,
|
60
|
+
'content' => Base64.encode64(attachment.body.decoded)
|
61
|
+
}
|
62
|
+
|
63
|
+
if attachment.inline?
|
64
|
+
content[:inline_attachments].push(mailjet_attachment)
|
65
|
+
else
|
66
|
+
content[:attachments].push(mailjet_attachment)
|
67
|
+
end
|
68
|
+
end
|
38
69
|
end
|
39
70
|
|
71
|
+
if mail.header && !mail.header.fields.empty?
|
72
|
+
content[:headers] = {}
|
73
|
+
mail.header.fields.each do |header|
|
74
|
+
if header.name.start_with?('X-MJ') || header.name.start_with?('X-Mailjet')
|
75
|
+
content[:headers][header.name] = header.value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Reply-To is not a property in Mailjet Send API
|
81
|
+
# Passing it as an header
|
82
|
+
content[:headers]['Reply-To'] = mail.reply_to.join(', ') if mail.reply_to
|
83
|
+
|
84
|
+
# Mailjet Send API does not support full from. Splitting the from field into two: name and email address
|
85
|
+
if Mailjet.config.default_from.present?
|
86
|
+
# from_address = Mail::AddressList.new(Mailjet.config.default_from).addresses[0]
|
87
|
+
mail[:from] = Mailjet.config.default_from
|
88
|
+
end
|
89
|
+
|
90
|
+
base_from = { from_name: mail[:from].display_names.first,
|
91
|
+
from_email: mail[:from].addresses.first }
|
92
|
+
|
40
93
|
payload = {
|
41
|
-
:
|
42
|
-
:
|
43
|
-
:
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
94
|
+
to: mail[:to].formatted.join(', '),
|
95
|
+
sender: mail[:sender],
|
96
|
+
subject: mail.subject
|
97
|
+
}
|
98
|
+
.merge(content)
|
99
|
+
.merge(base_from)
|
100
|
+
.merge(@delivery_method_options)
|
101
|
+
|
102
|
+
payload[:cc] = mail[:cc].formatted.join(', ') if mail[:cc]
|
103
|
+
payload[:reply_to] = mail[:reply_to].formatted.join(', ') if mail[:reply_to]
|
104
|
+
payload[:bcc] = mail[:bcc].formatted.join(', ') if mail[:bcc]
|
105
|
+
|
106
|
+
# Send the final payload to Mailjet Send API
|
107
|
+
Mailjet::Send.create(payload)
|
53
108
|
end
|
54
109
|
end
|
55
110
|
|
@@ -5,6 +5,6 @@ module Mailjet
|
|
5
5
|
include Mailjet::Resource
|
6
6
|
self.resource_path = 'v3/send/message'
|
7
7
|
self.public_operations = [:post]
|
8
|
-
self.resourceprop = [:from, :sender, :to, :cc, :bcc, :subject, :text, :html, :attachment, :
|
8
|
+
self.resourceprop = [:from, :sender, :to, :cc, :bcc, :subject, :text, :html, :attachment, :inline_attachments, :headers]
|
9
9
|
end
|
10
10
|
end
|
data/lib/mailjet/resource.rb
CHANGED
@@ -7,6 +7,7 @@ require 'active_support/core_ext/string'
|
|
7
7
|
require 'active_support/core_ext/module/delegation'
|
8
8
|
require 'active_support/concern'
|
9
9
|
require 'active_support/json/decoding'
|
10
|
+
require 'mail'
|
10
11
|
require 'json'
|
11
12
|
|
12
13
|
|
@@ -72,6 +73,7 @@ module Mailjet
|
|
72
73
|
#
|
73
74
|
attributes = parse_api_json(connection[id].get(default_headers)).first
|
74
75
|
instanciate_from_api(attributes)
|
76
|
+
|
75
77
|
rescue Mailjet::ApiError => e
|
76
78
|
if e.code == 404
|
77
79
|
nil
|
@@ -86,7 +88,8 @@ module Mailjet
|
|
86
88
|
attributes.tap { |hs| hs.delete(:id) }
|
87
89
|
|
88
90
|
if Mailjet.config.default_from and self.resource_path == 'v3/send/'
|
89
|
-
|
91
|
+
address = Mail::AddressList.new(Mailjet.config.default_from).addresses[0]
|
92
|
+
default_attributes = { :from_email => address.address, :from_name => address.display_name}
|
90
93
|
else
|
91
94
|
default_attributes = {}
|
92
95
|
end
|
@@ -189,6 +192,8 @@ module Mailjet
|
|
189
192
|
'Text-part'
|
190
193
|
elsif key == "html_part"
|
191
194
|
'Html-part'
|
195
|
+
elsif key == "inline_attachments"
|
196
|
+
'Inline_attachments'
|
192
197
|
else
|
193
198
|
key.to_s.send(method)
|
194
199
|
end
|
@@ -210,7 +215,7 @@ module Mailjet
|
|
210
215
|
|
211
216
|
def save
|
212
217
|
if persisted?
|
213
|
-
response = connection[id].put(formatted_payload, default_headers)
|
218
|
+
response = connection[attributes[:id]].put(formatted_payload, default_headers)
|
214
219
|
else
|
215
220
|
response = connection.post(formatted_payload, default_headers)
|
216
221
|
end
|
@@ -4,10 +4,10 @@ module Mailjet
|
|
4
4
|
class Newsletter_detailcontent
|
5
5
|
include Mailjet::Resource
|
6
6
|
self.action = "detailcontent"
|
7
|
-
self.resource_path = "v3/REST/
|
8
|
-
self.public_operations = [:get, :
|
7
|
+
self.resource_path = "v3/REST/template/id/#{self.action}"
|
8
|
+
self.public_operations = [:get, :post]
|
9
9
|
self.filters = []
|
10
|
-
self.resourceprop = [:text_part, :html_part]
|
10
|
+
self.resourceprop = [:text_part, :html_part, :headers, :mjml_content]
|
11
11
|
|
12
12
|
end
|
13
13
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mailjet/resource'
|
2
|
+
|
3
|
+
module Mailjet
|
4
|
+
class Template
|
5
|
+
include Mailjet::Resource
|
6
|
+
self.resource_path = 'v3/REST/template'
|
7
|
+
self.public_operations = [:get, :put, :post, :delete]
|
8
|
+
self.filters = [:api_key, :categories, :categories_selection_method, :edit_mode, :name, :owner_type, :purposes, :purposes_selection_method, :user]
|
9
|
+
self.resourceprop = [:author, :categories, :copyright, :description, :edit_mode, :is_starred, :name, :owner_type, :presets, :purposes]
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
require 'mailjet/resource'
|
3
|
+
|
4
|
+
module Mailjet
|
5
|
+
class Template_detailcontent
|
6
|
+
include Mailjet::Resource
|
7
|
+
self.resource_path = 'v3/REST/template/id/${self.action}'
|
8
|
+
self.public_operations = [:get, :put, :post, :delete]
|
9
|
+
self.filters = [:api_key, :categories, :categories_selection_method, :edit_mode, :name, :owner_type, :purposes, :purposes_selection_method, :user]
|
10
|
+
self.resourceprop = [:author, :categories, :copyright, :description, :edit_mode, :is_starred, :name, :owner_type, :presets, :purposes]
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
data/lib/mailjet/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailjet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Nappy
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2016-
|
14
|
+
date: 2016-08-02 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -356,6 +356,8 @@ files:
|
|
356
356
|
- lib/mailjet/resources/sender.rb
|
357
357
|
- lib/mailjet/resources/sender_validate.rb
|
358
358
|
- lib/mailjet/resources/senderstatistics.rb
|
359
|
+
- lib/mailjet/resources/template.rb
|
360
|
+
- lib/mailjet/resources/template_detailcontent.rb
|
359
361
|
- lib/mailjet/resources/toplinkclicked.rb
|
360
362
|
- lib/mailjet/resources/trigger.rb
|
361
363
|
- lib/mailjet/resources/user.rb
|
@@ -382,7 +384,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
382
384
|
version: '0'
|
383
385
|
requirements: []
|
384
386
|
rubyforge_project:
|
385
|
-
rubygems_version: 2.4.
|
387
|
+
rubygems_version: 2.4.5.1
|
386
388
|
signing_key:
|
387
389
|
specification_version: 4
|
388
390
|
summary: Mailjet a powerful all-in-one email service provider clients can use to get
|