postmark 1.14.0 → 1.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +0 -3
- data/CHANGELOG.rdoc +6 -0
- data/README.md +34 -781
- data/VERSION +1 -1
- data/lib/postmark/api_client.rb +42 -0
- data/lib/postmark/error.rb +10 -0
- data/lib/postmark/handlers/mail.rb +10 -4
- data/lib/postmark/mail_message_converter.rb +15 -5
- data/lib/postmark/message_extensions/mail.rb +45 -0
- data/lib/postmark/version.rb +1 -1
- data/postmark.gemspec +1 -1
- data/postmark.png +0 -0
- data/spec/support/custom_matchers.rb +4 -0
- data/spec/unit/postmark/api_client_spec.rb +103 -23
- data/spec/unit/postmark/error_spec.rb +13 -0
- data/spec/unit/postmark/handlers/mail_spec.rb +59 -27
- data/spec/unit/postmark/mail_message_converter_spec.rb +17 -0
- data/spec/unit/postmark/message_extensions/mail_spec.rb +114 -0
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1832c6626bce3fc8f5a8a6147a5a855d99f723d726ceb8c8478991dd6dad29a4
|
4
|
+
data.tar.gz: e22122605081b3ab53e6f1d1ba23767c5b6f2d5932b4ac1ca69b2bf1618ed0b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51dcf2d0964a2361e6679e3bf5c20225b3e9a7a30fdac96da974b8d7226ae9b8f8c67e918a6268f6f0d95169c43f608b4dc45a04192f7b07d3b7a9daff865860
|
7
|
+
data.tar.gz: ddc1ddb44493d6c3bd75d6175d502407ad810dc814c03e847d61400526c4f3f478475f60f2ddb2d3a682d05ffa19c7236ad46a157936c5e128e23a34a339c75a
|
data/.travis.yml
CHANGED
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
= Changelog
|
2
2
|
|
3
|
+
== 1.15.0
|
4
|
+
|
5
|
+
* Extended Mail::Message objects with support for Postmark templates.
|
6
|
+
* Added ApiClient#deliver_message_with_template and ApiClient#deliver_messages_with_templates
|
7
|
+
* Removed Rake from dependencies.
|
8
|
+
|
3
9
|
== 1.14.0
|
4
10
|
|
5
11
|
* Added support for verifying DKIM/Return-Path.
|
data/README.md
CHANGED
@@ -1,812 +1,65 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
This gem is the official wrapper for the [Postmark HTTP API](http://postmarkapp.com). Postmark allows you to send your application's emails with high delivery rates, including bounce/spam processing and detailed statistics. In addition, Postmark can parse incoming emails which are forwarded back to your application.
|
5
|
-
|
6
|
-
## Install the gem
|
7
|
-
|
8
|
-
With Bundler:
|
9
|
-
|
10
|
-
``` ruby
|
11
|
-
gem 'postmark'
|
12
|
-
```
|
13
|
-
|
14
|
-
Without Bundler:
|
15
|
-
|
16
|
-
``` bash
|
17
|
-
gem install postmark
|
18
|
-
```
|
19
|
-
|
20
|
-
## Get a Postmark API token
|
21
|
-
|
22
|
-
In order to send emails using Postmark ruby gem, you will need a
|
23
|
-
[Postmark](http://postmarkapp.com) account. If you don't have one please
|
24
|
-
register at https://postmarkapp.com/sign_up.
|
25
|
-
|
26
|
-
If you didn’t create any servers yet, please create one, proceed to the
|
27
|
-
`Credentials` tab and copy an API token. API tokens should be frequently rotated for
|
28
|
-
security reasons.
|
29
|
-
|
30
|
-
## Communicating with the API
|
31
|
-
|
32
|
-
Make sure you have a [sender signature](https://postmarkapp.com/signatures) for
|
33
|
-
every From email address you specify.
|
34
|
-
|
35
|
-
Create an instance of `Postmark::ApiClient` to start sending emails.
|
36
|
-
|
37
|
-
``` ruby
|
38
|
-
your_api_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
39
|
-
client = Postmark::ApiClient.new(your_api_token)
|
40
|
-
```
|
41
|
-
|
42
|
-
`Postmark::ApiClient` accepts various options:
|
43
|
-
|
44
|
-
``` ruby
|
45
|
-
client = Postmark::ApiClient.new(your_api_token, http_open_timeout: 15)
|
46
|
-
```
|
47
|
-
|
48
|
-
Some useful options are:
|
49
|
-
|
50
|
-
* `secure` (`true` or `false`): set to false to disable SSL connection.
|
51
|
-
* `http_read_timeout` (positive number): limit HTTP read time to `n` seconds.
|
52
|
-
* `http_open_timeout` (positive number): limit HTTP open time to `n` seconds.
|
53
|
-
* `proxy_host` (string): proxy address to use.
|
54
|
-
* `proxy_port` (positive number): proxy port to use.
|
55
|
-
* `proxy_user` (string): proxy user.
|
56
|
-
* `proxy_pass` (string): proxy password.
|
57
|
-
|
58
|
-
## Sending a plain text message
|
59
|
-
|
60
|
-
``` ruby
|
61
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
62
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
63
|
-
subject: 'Re: Come on, Sheldon. It will be fun.',
|
64
|
-
text_body: 'That\'s what you said about the Green Lantern ' \
|
65
|
-
'movie. You were 114 minutes of wrong.')
|
66
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:45:16.2059023-04:00", :message_id=>"b2b268e3-6a70-xxxx-b897-49c9eb8b1d2e", :error_code=>0, :message=>"OK"}
|
67
|
-
```
|
68
|
-
|
69
|
-
## Sending an HTML message with open tracking
|
70
|
-
|
71
|
-
Simply pass an HTML document as html_body parameter to `#deliver`. You can also enable open tracking by setting `track_opens` to `true`.
|
72
|
-
|
73
|
-
``` ruby
|
74
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
75
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
76
|
-
subject: 'Re: What, to you, is a large crowd?',
|
77
|
-
html_body: '<p>Any group big enough to trample me to death. ' \
|
78
|
-
'General rule of thumb is 36 adults or 70 ' \
|
79
|
-
'children.</p>',
|
80
|
-
track_opens: true)
|
81
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:51:08.8789433-04:00", :message_id=>"75c28987-564e-xxxx-b6eb-e8071873ac06", :error_code=>0, :message=>"OK"}
|
82
|
-
```
|
83
|
-
## Sending a message with link tracking
|
84
|
-
|
85
|
-
To track visited links for emails you send, make sure to have links in html_body, text_body or both when passing them to `#deliver`. You need to enable link tracking by setting `track_links` parameter to one of the following options: `:html_only`, `:text_only`, `:html_and_text` or `:none`.
|
86
|
-
Depending on parameter you set, link tracking will be enabled on plain text body, html body, both or none. Optionally you can also use string values as parameters 'HtmlOnly', 'TextOnly', 'HtmlAndText' or 'None'.
|
87
|
-
|
88
|
-
``` ruby
|
89
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
90
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
91
|
-
subject: 'Re: What, to you, is a large crowd?',
|
92
|
-
html_body: '<p>Any group big enough to trample me to death. ' \
|
93
|
-
'General <a href="http://www.example.com">rule of thumb</a> is 36 adults or 70 ' \
|
94
|
-
'children.</p>',
|
95
|
-
text_body: 'Any group big enough to trample me to death. General rule of thumb is 36 adults or 70 children - http://www.example.com.',
|
96
|
-
track_links: :html_and_text)
|
97
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:51:08.8789433-04:00", :message_id=>"75c28987-564e-xxxx-b6eb-e8071873ac06", :error_code=>0, :message=>"OK"}
|
98
|
-
```
|
99
|
-
|
100
|
-
## Sending a message with metadata
|
101
|
-
|
102
|
-
To send metadata for emails you send, all you need to do is provide metadata for your messages in form of hash map.
|
103
|
-
|
104
|
-
``` ruby
|
105
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
106
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
107
|
-
subject: 'Re: What, to you, is a large crowd?',
|
108
|
-
html_body: '<p>Any group big enough to trample me to death. ' \
|
109
|
-
'General <a href="http://www.example.com">rule of thumb</a> is 36 adults or 70 ' \
|
110
|
-
'children.</p>',
|
111
|
-
text_body: 'Any group big enough to trample me to death. General rule of thumb is 36 adults or 70 children - http://www.example.com.',
|
112
|
-
metadata: {'Example1' => 'value', 'Example2' => 'value'})
|
113
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:51:08.8789433-04:00", :message_id=>"75c28987-564e-xxxx-b6eb-e8071873ac06", :error_code=>0, :message=>"OK"}
|
114
|
-
```
|
115
|
-
|
116
|
-
## Sending a message with attachments
|
117
|
-
|
118
|
-
You can add
|
119
|
-
[attachments](http://developer.postmarkapp.com/developer-build.html#attachments)
|
120
|
-
to your messages. Keep in mind message size limit (contents and attachment) is currently 10 MB. For inline attachments it is possible to specify content IDs via the `content_id` attribute.
|
121
|
-
|
122
|
-
``` ruby
|
123
|
-
client.deliver(from: 'leonard@bigbangtheory.com',
|
124
|
-
to: 'Dr. Sheldon Cooper <sheldon@bigbangtheory.com>',
|
125
|
-
subject: 'Have you seen these pictures of yours?',
|
126
|
-
text_body: 'You look like a real geek!',
|
127
|
-
html_body: '<p>You look like a real geek!</p><center><img src="cid:42"></center>',
|
128
|
-
attachments: [File.open('1.jpeg'),
|
129
|
-
{name: 'sheldon.jpeg',
|
130
|
-
content: [File.read('2.jpeg')].pack('m'),
|
131
|
-
content_type: 'image/jpeg'},
|
132
|
-
{name: 'logo.png',
|
133
|
-
content: [File.read('1.png')].pack('m'),
|
134
|
-
content_type: 'image/png',
|
135
|
-
content_id: 'cid:42'}])
|
136
|
-
|
137
|
-
# => {:to=>"Dr. Sheldon Cooper <sheldon@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:56:12.2828813-04:00", :message_id=>"8ec0d283-8b93-xxxx-9d65-241d1777cf0f", :error_code=>0, :message=>"OK"}
|
138
|
-
```
|
139
|
-
|
140
|
-
## Sending a multipart message
|
141
|
-
|
142
|
-
``` ruby
|
143
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
144
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
145
|
-
subject: 'Re: Anything Can Happen Thursday',
|
146
|
-
text_body: 'Apparently the news didn\'t reach my digestive ' \
|
147
|
-
'system, which when startled has it\'s own version ' \
|
148
|
-
'of "Anything Can Happen Thursday"',
|
149
|
-
html_body: '<p>Apparently the news didn’t reach my ' \
|
150
|
-
'digestive system, which when startled has ' \
|
151
|
-
'it’s own version of “Anything Can '\
|
152
|
-
'Happen Thursday”</p>')
|
153
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T02:58:00.089828-04:00", :message_id=>"bc973458-1315-xxxx-b295-6aa0a2b631ac", :error_code=>0, :message=>"OK"}
|
154
|
-
```
|
155
|
-
|
156
|
-
## Tagging messages
|
157
|
-
|
158
|
-
You can categorize outgoing email using the optional `:tag` property. If you use
|
159
|
-
different tags for the different types of emails your application generates,
|
160
|
-
you will be able to get detailed statistics for them through the Postmark user
|
161
|
-
interface.
|
162
|
-
|
163
|
-
``` ruby
|
164
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
165
|
-
to: 'Penny <penny@bigbangtheory.com>',
|
166
|
-
subject: 'Re: You cleaned my apartment???',
|
167
|
-
text_body: 'I couldn\'t sleep knowing that just outside my ' \
|
168
|
-
'bedroom is our living room and just outside our ' \
|
169
|
-
'living room is that hallway and immediately adjacent ' \
|
170
|
-
'to that hallway is this!',
|
171
|
-
tag: 'confidential')
|
172
|
-
|
173
|
-
# => {:to=>"Penny <penny@bigbangtheory.com>", :submitted_at=>"2013-05-09T03:00:55.4454938-04:00", :message_id=>"34aed4b3-3a95-xxxx-bd1d-88064909cc93", :error_code=>0, :message=>"OK"}
|
174
|
-
```
|
175
|
-
|
176
|
-
## Sending to multiple recipients
|
177
|
-
|
178
|
-
You can pass multiple recipient addresses in the `:to` field and the optional
|
179
|
-
`:cc` and `:bcc` fields. Note that Postmark has a limit of 50 recipients
|
180
|
-
per message in total. You need to take care not to exceed that limit.
|
181
|
-
Otherwise, you will get an error.
|
182
|
-
|
183
|
-
``` ruby
|
184
|
-
client.deliver(from: 'sheldon@bigbangtheory.com',
|
185
|
-
to: ['Leonard Hofstadter <leonard@bigbangtheory.com>',
|
186
|
-
'Penny <penny@bigbangtheory.com>'],
|
187
|
-
cc: ['Dr. Koothrappali <raj@bigbangtheory.com>'],
|
188
|
-
bcc: 'secretsheldonstorage@bigbangtheory.com',
|
189
|
-
subject: 'Re: Come on, Sheldon. It will be fun.',
|
190
|
-
text_body: 'That\'s what you said about the Green Lantern ' \
|
191
|
-
'movie. You were 114 minutes of wrong.')
|
192
|
-
# => {:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>, Penny <penny@bigbangtheory.com>", :submitted_at=>"2013-05-09T05:04:16.3247488-04:00", :message_id=>"d647c5d6-xxxx-466d-9411-557dcd5c2297", :error_code=>0, :message=>"OK"}
|
193
|
-
```
|
194
|
-
|
195
|
-
## Sending a templated email
|
196
|
-
|
197
|
-
If you have a [template created](https://github.com/wildbit/postmark-gem/wiki/The-Templates-API-support) in Postmark you can send an email using that template.
|
198
|
-
For the purpose of sending emails, you can reference a template by its alias or ID.
|
199
|
-
|
200
|
-
``` ruby
|
201
|
-
client.deliver_with_template(from: 'sheldon@bigbangtheory.com',
|
202
|
-
to: 'Penny <penny@bigbangtheory.com>',
|
203
|
-
template_id: 123,
|
204
|
-
template_model: {
|
205
|
-
name: 'Penny',
|
206
|
-
message: 'Bazinga!'
|
207
|
-
})
|
208
|
-
|
209
|
-
# => {:to=>"Penny <penny@bigbangtheory.com>", :submitted_at=>"2013-05-09T03:00:55.4454938-04:00", :message_id=>"34aed4b3-3a95-xxxx-bd1d-88064909cc93", :error_code=>0, :message=>"OK"}
|
210
|
-
```
|
211
|
-
|
212
|
-
Following example shows how to send email using template alias:
|
213
|
-
|
214
|
-
``` ruby
|
215
|
-
client.deliver_with_template(from: 'sheldon@bigbangtheory.com',
|
216
|
-
to: 'Penny <penny@bigbangtheory.com>',
|
217
|
-
template_alias: 'bigbangtheory',
|
218
|
-
template_model: {
|
219
|
-
name: 'Penny',
|
220
|
-
message: 'Bazinga!'
|
221
|
-
})
|
222
|
-
|
223
|
-
# => {:to=>"Penny <penny@bigbangtheory.com>", :submitted_at=>"2013-05-09T03:00:55.4454938-04:00", :message_id=>"34aed4b3-3a95-xxxx-bd1d-88064909cc93", :error_code=>0, :message=>"OK"}
|
224
|
-
```
|
225
|
-
|
226
|
-
## Sending a templated email in batches
|
227
|
-
|
228
|
-
``` ruby
|
229
|
-
client.deliver_with_template([{from: 'sheldon@bigbangtheory.com',
|
230
|
-
to: 'Penny <penny@bigbangtheory.com>',
|
231
|
-
template_alias: 'bigbangtheory',
|
232
|
-
template_model: {
|
233
|
-
name: 'Penny',
|
234
|
-
message: 'Bazinga!'
|
235
|
-
}
|
236
|
-
},
|
237
|
-
{from: 'tom@bigbangtheory.com',
|
238
|
-
to: '<sheldon@bigbangtheory.com>',
|
239
|
-
template_id: 53,
|
240
|
-
template_model: {}
|
241
|
-
}])
|
242
|
-
```
|
1
|
+
<a href="https://postmarkapp.com">
|
2
|
+
<img src="postmark.png" alt="Postmark Logo" title="Postmark" width="120" height="120" align="right">
|
3
|
+
</a>
|
243
4
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
in batches. To facilitate this we provide a batching endpoint that permits you
|
249
|
-
to send up to 500 well-formed Postmark messages in a single API call.
|
250
|
-
|
251
|
-
``` ruby
|
252
|
-
messages = []
|
253
|
-
|
254
|
-
messages << {from: 'sheldon@bigbangtheory.com',
|
255
|
-
to: 'Leonard Hofstadter <leonard@bigbangtheory.com>',
|
256
|
-
subject: 'Re: Come on, Sheldon. It will be fun.',
|
257
|
-
text_body: 'That\'s what you said about the Green Lantern ' \
|
258
|
-
'movie. You were 114 minutes of wrong.'}
|
259
|
-
|
260
|
-
messages << {from: 'sheldon@bigbangtheory.com',
|
261
|
-
to: 'Penny <penny@bigbangtheory.com>',
|
262
|
-
subject: 'Re: You cleaned my apartment???',
|
263
|
-
text_body: 'I couldn\'t sleep knowing that just outside my ' \
|
264
|
-
'bedroom is our living room and just outside our ' \
|
265
|
-
'living room is that hallway and immediately ' \
|
266
|
-
'adjacent to that hallway is this!',
|
267
|
-
tag: 'confidential'}
|
268
|
-
|
269
|
-
client.deliver_in_batches(messages)
|
270
|
-
# => [{:to=>"Leonard Hofstadter <leonard@bigbangtheory.com>", :submitted_at=>"2013-05-09T05:19:16.3361118-04:00", :message_id=>"247e43a9-6b0d-4914-a87f-7b74bf76b5cb", :error_code=>0, :message=>"OK"}, {:to=>"Penny <penny@bigbangtheory.com>", :submitted_at=>"2013-05-09T05:19:16.3517099-04:00", :message_id=>"26467642-f169-4da8-87a8-b89154067dfb", :error_code=>0, :message=>"OK"}]
|
271
|
-
```
|
272
|
-
|
273
|
-
## Parsing inbound
|
274
|
-
|
275
|
-
Inbound processing allows you (or your users) to send emails to Postmark, which we then
|
276
|
-
process and deliver to you via a web hook in a nicely formatted JSON document.
|
277
|
-
|
278
|
-
Here is a simple Ruby/Sinatra application that does basic inbound processing.
|
279
|
-
|
280
|
-
``` ruby
|
281
|
-
logger = Logger.new(STDOUT)
|
282
|
-
|
283
|
-
class Comment
|
284
|
-
attr_accessor :attributes
|
285
|
-
|
286
|
-
def self.create_from_inbound_hook(message)
|
287
|
-
self.new(:text => message["TextBody"],
|
288
|
-
:user_email => message["From"],
|
289
|
-
:discussion_id => message["MailboxHash"])
|
290
|
-
end
|
291
|
-
|
292
|
-
def initialize(attributes={})
|
293
|
-
@attributes = attributes
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
post '/inbound' do
|
298
|
-
request.body.rewind
|
299
|
-
comment = Comment.create_from_inbound_hook(Postmark::Json.decode(request.body.read))
|
300
|
-
logger.info comment.inspect
|
301
|
-
end
|
302
|
-
```
|
303
|
-
|
304
|
-
If you don’t like that the fields of the Inbound JSON document are all in CamelCase, you
|
305
|
-
can use the `Postmark::Inbound.to_ruby_hash` method to give it some Ruby flavor.
|
306
|
-
|
307
|
-
```
|
308
|
-
postmark_hash = Postmark::Json.decode(request.body.read)
|
309
|
-
ruby_hash = Postmark::Inbound.to_ruby_hash(postmark_hash)
|
310
|
-
# => {:from=>"myUser@theirDomain.com", :from_full=>{:email=>"myUser@theirDomain.com", :name=>"John Doe"}, :to=>"451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com", :to_full=>[{:email=>"451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com", :name=>""}], :cc=>"\"Full name\" <sample.cc@emailDomain.com>, \"Another Cc\" <another.cc@emailDomain.com>", :cc_full=>[{:email=>"sample.cc@emailDomain.com", :name=>"Full name"}, {:email=>"another.cc@emailDomain.com", :name=>"Another Cc"}], :reply_to=>"myUsersReplyAddress@theirDomain.com", :subject=>"This is an inbound message", :message_id=>"22c74902-a0c1-4511-804f2-341342852c90", :date=>"Thu, 5 Apr 2012 16:59:01 +0200", :mailbox_hash=>"ahoy", :text_body=>"[ASCII]", :html_body=>"[HTML(encoded)]", :tag=>"", :headers=>[{:name=>"X-Spam-Checker-Version", :value=>"SpamAssassin 3.3.1 (2010-03-16) onrs-ord-pm-inbound1.wildbit.com"}, {:name=>"X-Spam-Status", :value=>"No"}, {:name=>"X-Spam-Score", :value=>"-0.1"}, {:name=>"X-Spam-Tests", :value=>"DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS"}, {:name=>"Received-SPF", :value=>"Pass (sender SPF authorized) identity=mailfrom; client-ip=209.85.160.180; helo=mail-gy0-f180.google.com; envelope-from=myUser@theirDomain.com; receiver=451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com"}, {:name=>"DKIM-Signature", :value=>"v=1; a=rsa-sha256; c=relaxed/relaxed; d=wildbit.com; s=google; h=mime-version:reply-to:date:message-id:subject:from:to:cc :content-type; bh=cYr/+oQiklaYbBJOQU3CdAnyhCTuvemrU36WT7cPNt0=; b=QsegXXbTbC4CMirl7A3VjDHyXbEsbCUTPL5vEHa7hNkkUTxXOK+dQA0JwgBHq5C+1u iuAJMz+SNBoTqEDqte2ckDvG2SeFR+Edip10p80TFGLp5RucaYvkwJTyuwsA7xd78NKT Q9ou6L1hgy/MbKChnp2kxHOtYNOrrszY3JfQM="}, {:name=>"MIME-Version", :value=>"1.0"}, {:name=>"Message-ID", :value=>"<CAGXpo2WKfxHWZ5UFYCR3H_J9SNMG+5AXUovfEFL6DjWBJSyZaA@mail.gmail.com>"}], :attachments=>[{:name=>"myimage.png", :content=>"[BASE64-ENCODED CONTENT]", :content_type=>"image/png", :content_length=>4096}, {:name=>"mypaper.doc", :content=>"[BASE64-ENCODED CONTENT]", :content_type=>"application/msword", :content_length=>16384}]}
|
311
|
-
```
|
312
|
-
|
313
|
-
## Working with bounces
|
314
|
-
|
315
|
-
Use `#get_bounces` to retrieve a list of bounces (use `:count` and `:offset`
|
316
|
-
parameters to control pagination).
|
317
|
-
|
318
|
-
``` ruby
|
319
|
-
client.get_bounces(count: 1, offset: 0)
|
320
|
-
# => [{:id=>654714902, :type=>"Transient", :type_code=>2, :name=>"Message delayed", :message_id=>"1fdf3729-xxxx-4d5c-8a7b-96da7a23268b", :description=>"The server could not temporarily deliver your message (ex: Message is delayed due to network troubles).", :details=>"action: failed\r\n", :email=>"tema@wildbit.org", :bounced_at=>"2013-04-10T01:01:35.0965184-04:00", :dump_available=>true, :inactive=>false, :can_activate=>true, :subject=>"bounce test"}]
|
321
|
-
```
|
322
|
-
|
323
|
-
Use `#get_bounced_tags` to retrieve a list of tags used for bounced emails.
|
324
|
-
|
325
|
-
``` ruby
|
326
|
-
client.get_bounced_tags
|
327
|
-
# => ["confidential"]
|
328
|
-
```
|
329
|
-
|
330
|
-
Use `#get_bounce` to get info for a specific bounce using ID:
|
331
|
-
|
332
|
-
``` ruby
|
333
|
-
client.get_bounce(654714902)
|
334
|
-
# => {:id=>654714902, :type=>"Transient", :type_code=>2, :name=>"Message delayed", :message_id=>"1fdf3729-xxxx-xxxx-8a7b-96da7a23268b", :description=>"The server could not temporarily deliver your message (ex: Message is delayed due to network troubles).", :details=>"action: failed\r\n", :email=>"tema@wildbit.com", :bounced_at=>"2013-04-10T01:01:35.0965184-04:00", :dump_available=>true, :inactive=>false, :can_activate=>true, :subject=>"bounce test", :content=>"..."}
|
335
|
-
```
|
5
|
+
# Postmark Ruby Gem
|
6
|
+
[![Build Status](https://travis-ci.org/wildbit/postmark-gem.svg?branch=master)](https://travis-ci.org/wildbit/postmark-gem) [![Code Climate](https://codeclimate.com/github/wildbit/postmark-gem/badges/gpa.svg)](https://codeclimate.com/github/wildbit/postmark-gem)
|
7
|
+
[![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](http://www.opensource.org/licenses/MIT)
|
8
|
+
[![Gem Version](https://badge.fury.io/rb/postmark.svg)](https://badge.fury.io/rb/postmark)
|
336
9
|
|
337
|
-
|
10
|
+
Postmark allows you to send your emails with high delivery rates. It also includes detailed statistics. In addition, Postmark can parse incoming emails which are forwarded back to your application.
|
338
11
|
|
339
|
-
|
340
|
-
client.dump_bounce(654714902)
|
341
|
-
# => {:body=>"Return-Path: <>\r\nReceived: from m1.mtasv.net (74.205.19.136) by sc-ord-mail2.mtasv.net id hcjov61jk5ko for <pm_bounces@pm.mtasv.net>; Wed, 10 Apr 2013 01:00:35 -0400 (envelope-from <>)\r\nDate: Wed, 10 Apr 2013 01:00:48 -0400\r\nFrom: postmaster@m1.mtasv.net\r\n..."}
|
342
|
-
```
|
12
|
+
This gem is the official wrapper for the [Postmark HTTP API](http://postmarkapp.com).
|
343
13
|
|
344
|
-
|
14
|
+
## Usage
|
345
15
|
|
346
|
-
|
347
|
-
|
348
|
-
# => [{...}, {...}]
|
349
|
-
```
|
16
|
+
Please see the [wiki](https://github.com/wildbit/postmark-gem/wiki) for detailed instructions about sending email, using the bounce api and other Postmark API features.
|
17
|
+
For details about Postmark API in general, please check out [Postmark developer docs](https://postmarkapp.com/developer).
|
350
18
|
|
351
|
-
|
19
|
+
## Requirements
|
352
20
|
|
353
|
-
|
354
|
-
client.activate_bounce(654714902)
|
355
|
-
# => {:id=>654714902, :type=>"Transient", :type_code=>2, :name=>"Message delayed", :message_id=>"1fdf3729-xxxx-xxxx-xxxx-96da7a23268b", :description=>"The server could not temporarily deliver your message (ex: Message is delayed due to network troubles).", :details=>"action: failed\r\n", :email=>"tema@wildbit.com", :bounced_at=>"2013-04-10T01:01:35.0965184-04:00", :dump_available=>true, :inactive=>false, :can_activate=>true, :subject=>"bounce test"}
|
356
|
-
```
|
21
|
+
You will need a Postmark account, server and sender signature (or verified domain) set up to use it. For details about setup, check out [wiki pages](https://github.com/wildbit/postmark-gem/wiki/Getting-Started).
|
357
22
|
|
358
|
-
|
23
|
+
If you plan using the library in a Rails project, check out the [postmark-rails](https://github.com/wildbit/postmark-rails/) gem, which
|
24
|
+
is meant to integrate with ActionMailer. The plugin will try to use ActiveSupport JSon if it is already included. If not,
|
25
|
+
it will attempt to use the built-in Ruby JSon library.
|
359
26
|
|
360
|
-
|
361
|
-
by type.
|
27
|
+
You can also explicitly specify which one to be used, using following code:
|
362
28
|
|
363
29
|
``` ruby
|
364
|
-
|
365
|
-
# => {:inactive_mails=>1, :bounces=>[{:name=>"All", :count=>3}, {:type=>"HardBounce", :name=>"Hard bounce", :count=>2}, {:type=>"Transient", :name=>"Message delayed", :count=>1}]}
|
30
|
+
Postmark.response_parser_class = :Json # :ActiveSupport or :Yajl are also supported.
|
366
31
|
```
|
367
32
|
|
368
|
-
##
|
369
|
-
|
370
|
-
The gem also allows you to read and update the server info:
|
33
|
+
## Installation
|
371
34
|
|
372
|
-
|
373
|
-
client.server_info
|
374
|
-
# => {:name=>"Testing", :color=>"blue", :bounce_hook_url=>"", :inbound_hash=>"c2ffffff74f8643e5f6086c81", :inbound_hook_url=>"", :smtp_api_activated=>true}
|
375
|
-
```
|
35
|
+
You can use the library with or without a Bundler.
|
376
36
|
|
377
|
-
|
37
|
+
With Bundler:
|
378
38
|
|
379
39
|
``` ruby
|
380
|
-
|
40
|
+
gem 'postmark'
|
381
41
|
```
|
382
42
|
|
383
|
-
|
384
|
-
|
385
|
-
You can use Postmark with the `mail` gem.
|
43
|
+
Without Bundler:
|
386
44
|
|
387
45
|
``` bash
|
388
|
-
gem install
|
389
|
-
```
|
390
|
-
|
391
|
-
Make sure you have a [sender signature](https://postmarkapp.com/signatures) for
|
392
|
-
every `From` email address you specify.
|
393
|
-
|
394
|
-
To send a `Mail::Message` via Postmark you’ll need to specify `Mail::Postmark` as
|
395
|
-
a delivery method for the message:
|
396
|
-
|
397
|
-
``` ruby
|
398
|
-
message = Mail.new do
|
399
|
-
# ...
|
400
|
-
delivery_method Mail::Postmark, api_token: 'your-postmark-api-token'
|
401
|
-
end
|
402
|
-
```
|
403
|
-
|
404
|
-
Delivery method accepts all options supported by `Postmark::ApiClient`
|
405
|
-
documented above. A new instance of `Postmark::ApiClient` is created every time
|
406
|
-
you deliver a message to preserve thread safety.
|
407
|
-
|
408
|
-
If you would prefer to use Postmark as the default delivery method for all
|
409
|
-
`Mail::Message` instances, you can call `Mail.defaults` method during the initialization
|
410
|
-
step of your app:
|
411
|
-
|
412
|
-
``` ruby
|
413
|
-
Mail.defaults do
|
414
|
-
delivery_method Mail::Postmark, api_token: 'your-postmark-api-token'
|
415
|
-
end
|
416
|
-
```
|
417
|
-
|
418
|
-
## Plain text message
|
419
|
-
|
420
|
-
``` ruby
|
421
|
-
require 'rubygems'
|
422
|
-
require 'postmark'
|
423
|
-
require 'mail'
|
424
|
-
require 'json'
|
425
|
-
|
426
|
-
message = Mail.new do
|
427
|
-
from 'sheldon@bigbangtheory.com'
|
428
|
-
to 'Leonard Hofstadter <leonard@bigbangtheory.com>'
|
429
|
-
subject 'Re: Come on, Sheldon. It will be fun.'
|
430
|
-
body 'That\'s what you said about the Green Lantern movie. You' \
|
431
|
-
'were 114 minutes of wrong.'
|
432
|
-
|
433
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
434
|
-
end
|
435
|
-
|
436
|
-
message.deliver
|
437
|
-
# => #<Mail::Message:70355890541720, Multipart: false, Headers: <From: sheldon@bigbangtheory.com>, <To: leonard@bigbangtheory.com>, <Message-ID: e439fec0-4c89-475b-b3fc-eb446249a051>, <Subject: Re: Come on, Sheldon. It will be fun.>>
|
438
|
-
```
|
439
|
-
|
440
|
-
## HTML message (with open tracking)
|
441
|
-
|
442
|
-
Notice that we set `track_opens` field to `true`, to enable open tracking for this message.
|
443
|
-
|
444
|
-
``` ruby
|
445
|
-
require 'rubygems'
|
446
|
-
require 'postmark'
|
447
|
-
require 'mail'
|
448
|
-
require 'json'
|
449
|
-
|
450
|
-
message = Mail.new do
|
451
|
-
from 'sheldon@bigbangtheory.com'
|
452
|
-
to 'Leonard Hofstadter <leonard@bigbangtheory.com>'
|
453
|
-
subject 'Re: What, to you, is a large crowd?'
|
454
|
-
|
455
|
-
content_type 'text/html; charset=UTF-8'
|
456
|
-
body '<p>Any group big enough to trample me to death. General ' \
|
457
|
-
'rule of thumb is 36 adults or 70 children.</p>'
|
458
|
-
|
459
|
-
track_opens true
|
460
|
-
|
461
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
462
|
-
end
|
463
|
-
|
464
|
-
message.deliver
|
465
|
-
# => #<Mail::Message:70355902117460, Multipart: false, Headers: <From: sheldon@bigbangtheory.com>, <To: leonard@bigbangtheory.com>, <Message-ID: 3a9370a2-6c24-4304-a03c-320a54cc59f7>, <Subject: Re: What, to you, is a large crowd?>, <Content-Type: text/html; charset=UTF-8>>
|
466
|
-
```
|
467
|
-
|
468
|
-
## Multipart message (with link tracking)
|
469
|
-
|
470
|
-
Notice that we set `track_links` field to `:html_and_text`, to enable link tracking for both plain text and html parts for this message.
|
471
|
-
|
472
|
-
``` ruby
|
473
|
-
require 'rubygems'
|
474
|
-
require 'postmark'
|
475
|
-
require 'mail'
|
476
|
-
require 'json'
|
477
|
-
|
478
|
-
message = Mail.new do
|
479
|
-
from 'sheldon@bigbangtheory.com'
|
480
|
-
to 'Leonard Hofstadter <leonard@bigbangtheory.com>'
|
481
|
-
subject 'Re: What, to you, is a large crowd?'
|
482
|
-
|
483
|
-
text_part do
|
484
|
-
body 'Any group big enough to trample me to death. General rule of thumb is 36 adults or 70 children - http://www.example.com.'
|
485
|
-
end
|
486
|
-
|
487
|
-
html_part do
|
488
|
-
content_type 'text/html; charset=UTF-8'
|
489
|
-
body '<p>Any group big enough to trample me to death. ' \
|
490
|
-
'General <a href="http://www.example.com">rule of thumb</a> is 36 adults or 70 ' \
|
491
|
-
'children.</p>'
|
492
|
-
end
|
493
|
-
|
494
|
-
track_links :html_and_text
|
495
|
-
|
496
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
497
|
-
end
|
498
|
-
|
499
|
-
message.deliver
|
500
|
-
# => #<Mail::Message:70355902117460, Multipart: true, Headers: <From: sheldon@bigbangtheory.com>, <To: leonard@bigbangtheory.com>, <Message-ID: 1a1370a1-6c21-4304-a03c-320a54cc59f7>, <Subject: Re: What, to you, is a large crowd?>, <Content-Type: multipart/alternative; boundary=--==_mimepart_58380d6029b17_20543fd48543fa14977a>, <TRACK-LINKS: HtmlAndText>>
|
501
|
-
```
|
502
|
-
|
503
|
-
## Message with metadata
|
504
|
-
|
505
|
-
``` ruby
|
506
|
-
message = Mail.new do
|
507
|
-
from 'leonard@bigbangtheory.com'
|
508
|
-
to 'Dr. Sheldon Cooper <sheldon@bigbangtheory.com>'
|
509
|
-
subject 'Have you seen these pictures of yours?'
|
510
|
-
body 'You look like a real geek!'
|
511
|
-
add_file '1.jpeg'
|
512
|
-
metadata {"Example1"=>"value","Example2"=>"value"}
|
513
|
-
|
514
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
515
|
-
end
|
516
|
-
|
517
|
-
message.deliver
|
518
|
-
# => #<Mail::Message:70185826686240, Multipart: true, Headers: <From: leonard@bigbangtheory.com>, <To: sheldon@bigbangtheory.com>, <Message-ID: ba644cc1-b5b1-4bcb-aaf8-2f290b5aad80>, <Subject: Have you seen these pictures of yours?>, <Content-Type: multipart/mixed; boundary=--==_mimepart_5121f9f1ec653_12c53fd569035ad817726>>
|
519
|
-
```
|
520
|
-
|
521
|
-
## Message with attachments
|
522
|
-
|
523
|
-
``` ruby
|
524
|
-
message = Mail.new do
|
525
|
-
from 'leonard@bigbangtheory.com'
|
526
|
-
to 'Dr. Sheldon Cooper <sheldon@bigbangtheory.com>'
|
527
|
-
subject 'Have you seen these pictures of yours?'
|
528
|
-
body 'You look like a real geek!'
|
529
|
-
add_file '1.jpeg'
|
530
|
-
|
531
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
532
|
-
end
|
533
|
-
|
534
|
-
message.attachments['sheldon.jpeg'] = File.read('2.jpeg')
|
535
|
-
|
536
|
-
message.deliver
|
537
|
-
# => #<Mail::Message:70185826686240, Multipart: true, Headers: <From: leonard@bigbangtheory.com>, <To: sheldon@bigbangtheory.com>, <Message-ID: ba644cc1-b5b1-4bcb-aaf8-2f290b5aad80>, <Subject: Have you seen these pictures of yours?>, <Content-Type: multipart/mixed; boundary=--==_mimepart_5121f9f1ec653_12c53fd569035ad817726>>
|
538
|
-
```
|
539
|
-
|
540
|
-
You can also make an attachment inline:
|
541
|
-
|
542
|
-
``` ruby
|
543
|
-
message.attachments.inline['sheldon.jpeg'] = File.read('2.jpeg')
|
544
|
-
```
|
545
|
-
|
546
|
-
Then simply use `Mail::Part#url` method to reference it from your email body.
|
547
|
-
|
548
|
-
``` erb
|
549
|
-
<p><img src="<%= message.attachments['sheldon.jpeg'].url %>" alt="Dr. Sheldon Cooper"></p>
|
550
|
-
```
|
551
|
-
|
552
|
-
## Multipart message
|
553
|
-
|
554
|
-
You can send multipart messages containing both text and HTML using the Postmark gem.
|
555
|
-
|
556
|
-
``` ruby
|
557
|
-
require 'rubygems'
|
558
|
-
require 'postmark'
|
559
|
-
require 'mail'
|
560
|
-
require 'json'
|
561
|
-
|
562
|
-
message = Mail.new do
|
563
|
-
from 'sheldon@bigbangtheory.com'
|
564
|
-
to 'Leonard Hofstadter <leonard@bigbangtheory.com>'
|
565
|
-
subject 'Re: Anything Can Happen Thursday'
|
566
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
567
|
-
|
568
|
-
text_part do
|
569
|
-
body 'Apparently the news didn\'t reach my digestive system,' \
|
570
|
-
' which when startled has it\'s own version of "Anything' \
|
571
|
-
' Can Happen Thursday"'
|
572
|
-
end
|
573
|
-
|
574
|
-
html_part do
|
575
|
-
content_type 'text/html; charset=UTF-8'
|
576
|
-
body '<p>Apparently the news didn’t reach my digestive ' \
|
577
|
-
'system, which when startled has it’s own version ' \
|
578
|
-
'of “Anything Can Happen Thursday”</p>'
|
579
|
-
end
|
580
|
-
end
|
581
|
-
|
582
|
-
message.deliver
|
583
|
-
# => #<Mail::Message:70355901588620, Multipart: true, Headers: <From: sheldon@bigbangtheory.com>, <To: leonard@bigbangtheory.com>, <Message-ID: cadba131-f6d6-4cfc-9892-16ee738ba54c>, <Subject: Re: Anything Can Happen Thursday>, <Content-Type: multipart/alternative; boundary=--==_mimepart_50ef7a6234a69_a4c73ffd01035adc207b8>>
|
584
|
-
```
|
585
|
-
|
586
|
-
## Tagged message
|
587
|
-
|
588
|
-
Postmark also lets you tag your messages.
|
589
|
-
|
590
|
-
``` ruby
|
591
|
-
require 'rubygems'
|
592
|
-
require 'postmark'
|
593
|
-
require 'mail'
|
594
|
-
require 'json'
|
595
|
-
|
596
|
-
message = Mail.new do
|
597
|
-
from 'sheldon@bigbangtheory.com'
|
598
|
-
to 'Penny <penny@bigbangtheory.com>'
|
599
|
-
subject 'Re: You cleaned my apartment???'
|
600
|
-
body 'I couldn\'t sleep knowing that just outside my bedroom is ' \
|
601
|
-
'our living room and just outside our living room is that ' \
|
602
|
-
'hallway and immediately adjacent to that hallway is this!'
|
603
|
-
tag 'confidential'
|
604
|
-
|
605
|
-
delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
|
606
|
-
end
|
607
|
-
|
608
|
-
message.deliver
|
609
|
-
# => #<Mail::Message:70168327829580, Multipart: false, Headers: <From: sheldon@bigbangtheory.com>, <To: penny@bigbangtheory.com>, <Message-ID: af2570fd-3481-4b45-8b27-a249806d891a>, <Subject: Re: You cleaned my apartment???>, <TAG: confidential>>
|
610
|
-
```
|
611
|
-
|
612
|
-
## Sending in batches
|
613
|
-
|
614
|
-
You can also send `Mail::Message` objects in batches. Create an instance of
|
615
|
-
`Postmark::ApiClient` as described in "Communicating with the API" section.
|
616
|
-
|
617
|
-
``` ruby
|
618
|
-
messages = []
|
619
|
-
|
620
|
-
messages << Mail.new do
|
621
|
-
from 'sheldon@bigbangtheory.com'
|
622
|
-
to 'Leonard Hofstadter <leonard@bigbangtheory.com>'
|
623
|
-
subject 'Re: Come on, Sheldon. It will be fun.'
|
624
|
-
body 'That\'s what you said about the Green Lantern movie. You' \
|
625
|
-
'were 114 minutes of wrong.'
|
626
|
-
end
|
627
|
-
|
628
|
-
messages << Mail.new do
|
629
|
-
from 'sheldon@bigbangtheory.com'
|
630
|
-
to 'Penny <penny@bigbangtheory.com>'
|
631
|
-
subject 'Re: You cleaned my apartment???'
|
632
|
-
body 'I couldn\'t sleep knowing that just outside my bedroom is ' \
|
633
|
-
'our living room and just outside our living room is that ' \
|
634
|
-
'hallway and immediately adjacent to that hallway is this!'
|
635
|
-
tag 'confidential'
|
636
|
-
end
|
637
|
-
|
638
|
-
client.deliver_messages(messages)
|
639
|
-
# => [{:to=>"leonard@bigbangtheory.com", :submitted_at=>"2013-05-10T01:59:29.830486-04:00", :message_id=>"8ad0e8b0-xxxx-xxxx-951d-223c581bb467", :error_code=>0, :message=>"OK"}, {:to=>"penny@bigbangtheory.com", :submitted_at=>"2013-05-10T01:59:29.830486-04:00", :message_id=>"33c6240c-xxxx-xxxx-b0df-40bdfcf4e0f7", :error_code=>0, :message=>"OK"}]
|
640
|
-
```
|
641
|
-
|
642
|
-
After delivering a batch you can check on each message’s delivery status:
|
643
|
-
|
644
|
-
``` ruby
|
645
|
-
messages.first.delivered?
|
646
|
-
# => true
|
647
|
-
|
648
|
-
messages.all?(&:delivered)
|
649
|
-
# => true
|
650
|
-
```
|
651
|
-
|
652
|
-
Or even get a related Postmark response:
|
653
|
-
|
654
|
-
``` ruby
|
655
|
-
messages.first.postmark_response
|
656
|
-
# => {"To"=>"leonard@bigbangtheory.com", "SubmittedAt"=>"2013-05-10T01:59:29.830486-04:00", "MessageID"=>"8ad0e8b0-xxxx-xxxx-951d-223c581bb467", "ErrorCode"=>0, "Message"=>"OK"}
|
657
|
-
```
|
658
|
-
|
659
|
-
## Accessing Postmark Message-ID
|
660
|
-
|
661
|
-
You might want to save identifiers of messages you send. Postmark provides you
|
662
|
-
with a unique Message-ID, which you can
|
663
|
-
[use to retrieve bounces](http://blog.postmarkapp.com/post/24970994681/using-messageid-to-retrieve-bounces)
|
664
|
-
later. This example shows you how to access the Message-ID of a sent email message.
|
665
|
-
|
666
|
-
``` ruby
|
667
|
-
message = Mail.new
|
668
|
-
# ...
|
669
|
-
message.deliver
|
670
|
-
|
671
|
-
message['Message-ID']
|
672
|
-
# => cadba131-f6d6-4cfc-9892-16ee738ba54c
|
673
|
-
message.message_id
|
674
|
-
# => "cadba131-f6d6-4cfc-9892-16ee738ba54c"
|
675
|
-
```
|
676
|
-
|
677
|
-
# Exploring Other Gem Features
|
678
|
-
|
679
|
-
## The Account API Support
|
680
|
-
|
681
|
-
Postmark allows you to automatically scale your sending infrastructure with the Account API. Learn how in the [Account API Support](https://github.com/wildbit/postmark-gem/wiki/The-Account-API-Support) guide.
|
682
|
-
|
683
|
-
## The Triggers API Support
|
684
|
-
|
685
|
-
[The Triggers API](https://github.com/wildbit/postmark-gem/wiki/The-Triggers-API-Support) can be used to tell Postmark to automatically track opens for all messages with a certain tag.
|
686
|
-
|
687
|
-
## The Messages API Support
|
688
|
-
|
689
|
-
If you ever need to access your messages or their metadata (i.e. open tracking info), [the Messages API](https://github.com/wildbit/postmark-gem/wiki/The-Messages-API-support) is a great place to start.
|
690
|
-
|
691
|
-
## The Templates API Support
|
692
|
-
|
693
|
-
[The Templates API](https://github.com/wildbit/postmark-gem/wiki/The-Templates-API-support) can be used to fully manage your templates.
|
694
|
-
|
695
|
-
## The Stats API Support
|
696
|
-
|
697
|
-
[The Stats API](https://github.com/wildbit/postmark-gem/wiki/The-Stats-API-support) can be used to access statistics on your emails sent by date and tag.
|
698
|
-
|
699
|
-
|
700
|
-
## ActiveModel-like Interface For Bounces
|
701
|
-
|
702
|
-
To provide an interface similar to ActiveModel for bounces, the Postmark gem adds
|
703
|
-
`Postmark::Bounce` class. This class uses the shared `Postmark::ApiClient` instance
|
704
|
-
configured through the Postmark module.
|
705
|
-
|
706
|
-
``` ruby
|
707
|
-
require 'rubygems'
|
708
|
-
require 'postmark'
|
709
|
-
require 'mail'
|
710
|
-
require 'json'
|
711
|
-
|
712
|
-
Postmark.response_parser_class = :Json
|
713
|
-
Postmark.api_token = 'your-postmark-api-token'
|
714
|
-
|
715
|
-
# Get bounces information: (array of bounce objects)
|
716
|
-
Postmark::Bounce.all
|
717
|
-
# => [#<Postmark::Bounce:0x007ff09c04ae18 @id=580516117, @email="sheldon@bigbangtheory.com", @bounced_at=2012-10-21 00:01:56 +0800, @type="HardBounce", @name=nil, @details="smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 c13si5382730vcw.23", @tag=nil, @dump_available=false, @inactive=true, @can_activate=true, @message_id="876d40fe-ab2a-4925-9d6f-8d5e4f4926f5", @subject="Re: What, to you, is a large crowd?">]
|
718
|
-
|
719
|
-
# Find specific bounce by id:
|
720
|
-
bounce = Postmark::Bounce.find(5)
|
721
|
-
# => #<Postmark::Bounce:0x007ff09c04ae18 @id=580516117, @email="sheldon@bigbangtheory.com", @bounced_at=2012-10-21 00:01:56 +0800, @type="HardBounce", @name=nil, @details="smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 c13si5382730vcw.23", @tag=nil, @dump_available=false, @inactive=true, @can_activate=true, @message_id="876d40fe-ab2a-4925-9d6f-8d5e4f4926f5", @subject="Re: What, to you, is a large crowd?">
|
722
|
-
|
723
|
-
bounce.dump # string, containing raw SMTP data
|
724
|
-
# => "Return-Path: <>\r\nDate: Sun, 21 Oct 2012 01:00:04 -0400\r\nFrom: postmaster@p1.mtasv.net\r\n..."
|
725
|
-
|
726
|
-
bounce.activate # reactivate hard bounce
|
727
|
-
# => #<Postmark::Bounce:0x007ff09c04ae18 @id=580516117, @email="sheldon@bigbangtheory.com", @bounced_at=2012-10-21 00:01:56 +0800, @type="HardBounce", @name=nil, @details="smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 c13si5382730vcw.23", @tag=nil, @dump_available=false, @inactive=true, @can_activate=true, @message_id="876d40fe-ab2a-4925-9d6f-8d5e4f4926f5", @subject="Re: What, to you, is a large crowd?">
|
728
|
-
```
|
729
|
-
|
730
|
-
## Error handling
|
731
|
-
|
732
|
-
For the gem version `1.11.0` and above, use the following template to handle the errors you care about:
|
733
|
-
|
734
|
-
``` ruby
|
735
|
-
def handle_postmark_errors
|
736
|
-
# Any Postmark request
|
737
|
-
yield
|
738
|
-
error
|
739
|
-
rescue Postmark::InvalidApiKeyError => error
|
740
|
-
# Authentication error
|
741
|
-
# TODO: Make sure your API token is correct
|
742
|
-
puts error
|
743
|
-
error
|
744
|
-
rescue Postmark::TimeoutError => error
|
745
|
-
# Network timeout, auto-retried :max_retries times
|
746
|
-
# TODO: Save message locally, try again once the network issues are resolved
|
747
|
-
# Consider increasing `http_open_timeout` and `http_read_timeout`.
|
748
|
-
puts error
|
749
|
-
error
|
750
|
-
rescue Postmark::InternalServerError => error
|
751
|
-
# Postmark server error, auto-retried :max_retries times
|
752
|
-
# TODO: Save message locally, try again later.
|
753
|
-
puts error
|
754
|
-
error
|
755
|
-
rescue Postmark::HttpClientError => error
|
756
|
-
# Corrupted response from Postmark, auto-retried :max_retries times
|
757
|
-
# TODO: Save message locally, try again later.
|
758
|
-
puts error
|
759
|
-
error
|
760
|
-
rescue Postmark::InactiveRecipientError => error
|
761
|
-
# You tried to send to one or more recipients marked as inactive in
|
762
|
-
# Postmark
|
763
|
-
# TODO: Mark listed recipients as inactive in your local db or reactivate
|
764
|
-
# using the Bounces API
|
765
|
-
puts "Inactive recipients: #{error.recipients.join(', ')}"
|
766
|
-
puts error
|
767
|
-
error
|
768
|
-
rescue Postmark::ApiInputError => error
|
769
|
-
# Postmark rejected your request as invalid
|
770
|
-
# TODO: Look up the error code and resolve the problem in your app
|
771
|
-
# List of supported error codes:
|
772
|
-
# https://postmarkapp.com/developer/api/overview#error-codes
|
773
|
-
puts "#{error.error_code} #{error.message}"
|
774
|
-
puts error
|
775
|
-
error
|
776
|
-
rescue Postmark::Error => error
|
777
|
-
# All other Postmark errors
|
778
|
-
# TODO: Log and review as needed
|
779
|
-
puts error
|
780
|
-
error
|
781
|
-
rescue Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED,
|
782
|
-
EOFError, Net::ProtocolError, SocketError => error
|
783
|
-
# Standard Ruby network errors, auto-retried :max_reties times
|
784
|
-
# TODO: Save message locally, resolve network issues, try again.
|
785
|
-
puts error
|
786
|
-
error
|
787
|
-
end
|
46
|
+
gem install postmark
|
788
47
|
```
|
789
48
|
|
790
|
-
##
|
791
|
-
|
792
|
-
You will need a Postmark account, server and sender signature set up to use it.
|
793
|
-
If you plan using it in a Rails project, check out the
|
794
|
-
[postmark-rails](https://github.com/wildbit/postmark-rails/) gem, which
|
795
|
-
is meant to integrate with ActionMailer.
|
49
|
+
## Note on Patches/Pull Requests
|
796
50
|
|
797
|
-
|
798
|
-
it will attempt using the built-in Ruby Json library.
|
51
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) file for details.
|
799
52
|
|
800
|
-
|
53
|
+
## Issues & Comments
|
801
54
|
|
802
|
-
|
803
|
-
|
804
|
-
```
|
55
|
+
Feel free to contact us if you encounter any issues with the library or Postmark API.
|
56
|
+
Please leave all comments, bugs, requests and issues on the Issues page.
|
805
57
|
|
806
|
-
##
|
58
|
+
## License
|
807
59
|
|
808
|
-
|
60
|
+
The Postmark Ruby library is licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) license.
|
61
|
+
Refer to the [LICENSE](https://github.com/wildbit/postmark-gem/blob/master/LICENSE) file for more information.
|
809
62
|
|
810
63
|
## Copyright
|
811
64
|
|
812
|
-
Copyright © 2018 Wildbit LLC.
|
65
|
+
Copyright © 2018 Wildbit LLC.
|