wj-mailgun-ruby 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rubocop.yml +8 -0
  4. data/.rubocop_todo.yml +22 -0
  5. data/.ruby-env.yml.example +12 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +24 -0
  8. data/Gemfile +6 -0
  9. data/LICENSE +191 -0
  10. data/README.md +241 -0
  11. data/Rakefile +35 -0
  12. data/docs/Domains.md +54 -0
  13. data/docs/Events.md +46 -0
  14. data/docs/MessageBuilder.md +105 -0
  15. data/docs/Messages.md +107 -0
  16. data/docs/OptInHandler.md +103 -0
  17. data/docs/Snippets.md +526 -0
  18. data/docs/Suppressions.md +82 -0
  19. data/docs/Webhooks.md +40 -0
  20. data/lib/mailgun-ruby.rb +2 -0
  21. data/lib/mailgun.rb +39 -0
  22. data/lib/mailgun/address.rb +45 -0
  23. data/lib/mailgun/chains.rb +16 -0
  24. data/lib/mailgun/client.rb +199 -0
  25. data/lib/mailgun/domains/domains.rb +84 -0
  26. data/lib/mailgun/events/events.rb +120 -0
  27. data/lib/mailgun/exceptions/exceptions.rb +65 -0
  28. data/lib/mailgun/lists/opt_in_handler.rb +58 -0
  29. data/lib/mailgun/messages/batch_message.rb +125 -0
  30. data/lib/mailgun/messages/message_builder.rb +413 -0
  31. data/lib/mailgun/response.rb +62 -0
  32. data/lib/mailgun/suppressions.rb +270 -0
  33. data/lib/mailgun/version.rb +4 -0
  34. data/lib/mailgun/webhooks/webhooks.rb +101 -0
  35. data/lib/railgun.rb +8 -0
  36. data/lib/railgun/attachment.rb +56 -0
  37. data/lib/railgun/errors.rb +27 -0
  38. data/lib/railgun/mailer.rb +161 -0
  39. data/lib/railgun/message.rb +17 -0
  40. data/lib/railgun/railtie.rb +9 -0
  41. data/mailgun.gemspec +37 -0
  42. data/spec/integration/bounces_spec.rb +44 -0
  43. data/spec/integration/campaign_spec.rb +60 -0
  44. data/spec/integration/complaints_spec.rb +38 -0
  45. data/spec/integration/domains_spec.rb +39 -0
  46. data/spec/integration/email_validation_spec.rb +57 -0
  47. data/spec/integration/events_spec.rb +28 -0
  48. data/spec/integration/list_members_spec.rb +63 -0
  49. data/spec/integration/list_spec.rb +58 -0
  50. data/spec/integration/mailgun_spec.rb +121 -0
  51. data/spec/integration/messages/sample_data/mime.txt +38 -0
  52. data/spec/integration/routes_spec.rb +74 -0
  53. data/spec/integration/stats_spec.rb +15 -0
  54. data/spec/integration/suppressions_spec.rb +126 -0
  55. data/spec/integration/unsubscribes_spec.rb +42 -0
  56. data/spec/integration/webhook_spec.rb +54 -0
  57. data/spec/spec_helper.rb +45 -0
  58. data/spec/unit/connection/test_client.rb +99 -0
  59. data/spec/unit/events/events_spec.rb +50 -0
  60. data/spec/unit/lists/opt_in_handler_spec.rb +24 -0
  61. data/spec/unit/mailgun_spec.rb +127 -0
  62. data/spec/unit/messages/batch_message_spec.rb +131 -0
  63. data/spec/unit/messages/message_builder_spec.rb +584 -0
  64. data/spec/unit/messages/sample_data/mailgun_icon.png +0 -0
  65. data/spec/unit/messages/sample_data/mime.txt +38 -0
  66. data/spec/unit/messages/sample_data/rackspace_logo.jpg +0 -0
  67. data/vcr_cassettes/bounces.yml +175 -0
  68. data/vcr_cassettes/complaints.yml +175 -0
  69. data/vcr_cassettes/domains.todo.yml +42 -0
  70. data/vcr_cassettes/domains.yml +360 -0
  71. data/vcr_cassettes/email_validation.yml +167 -0
  72. data/vcr_cassettes/events.yml +108 -0
  73. data/vcr_cassettes/exceptions.yml +45 -0
  74. data/vcr_cassettes/list_members.yml +320 -0
  75. data/vcr_cassettes/mailing_list.todo.yml +43 -0
  76. data/vcr_cassettes/mailing_list.yml +390 -0
  77. data/vcr_cassettes/routes.yml +359 -0
  78. data/vcr_cassettes/send_message.yml +107 -0
  79. data/vcr_cassettes/stats.yml +44 -0
  80. data/vcr_cassettes/suppressions.yml +676 -0
  81. data/vcr_cassettes/unsubscribes.yml +191 -0
  82. data/vcr_cassettes/webhooks.yml +276 -0
  83. metadata +263 -0
@@ -0,0 +1,526 @@
1
+ Mailgun-Ruby Snippets
2
+ =====================
3
+
4
+ This page is filled with snippets that cover almost every API endpoint and action. Copy, Paste, Go! There will be little inline documentation in these samples, please consult Mailgun's official documentation for detailed information.
5
+
6
+ If you haven't already installed the SDK, go to the README and follow the installation instructions.
7
+
8
+ These snippets require that you pipe in a few parameters. "mg_client" is instantiated like so:
9
+
10
+ ```ruby
11
+ require 'mailgun'
12
+ mg_client = Mailgun::Client.new "your-api-key"
13
+ ```
14
+
15
+ ### Messages:
16
+ ____________________________________________________
17
+ **Send a basic message: **
18
+
19
+ ```ruby
20
+ data = {:from => 'bob@sending_domain.com',
21
+ :to => 'sally@example.com',
22
+ :subject => 'The Ruby SDK is awesome!',
23
+ :text => 'It is really easy to send a message!'}
24
+
25
+ mg_client.send_message "sending_domain.com", data
26
+ ```
27
+
28
+ **Send a multipart text/html message:**
29
+
30
+ ```ruby
31
+ data = {:from => 'bob@sending_domain.com',
32
+ :to => 'sally@example.com',
33
+ :subject => 'The Ruby SDK is awesome!',
34
+ :text => 'This is the text part.',
35
+ :html => '<html><body><p>This is the HTML part.</p></body></html>'}
36
+
37
+ mg_client.send_message "sending_domain.com", data
38
+ ```
39
+
40
+ **Send a custom MIME message:**
41
+
42
+ ```ruby
43
+ # Don't include a file, pull the file to a string
44
+ mime_string = 'From: Bob Sample <example@example.com>
45
+ MIME-Version: 1.0
46
+ Content-Type: multipart/mixed;
47
+ boundary="--boundary-goes-here--"
48
+
49
+ Your Multipart MIME body
50
+
51
+ --boundary-goes-here--
52
+ Content-Type: text/plain
53
+
54
+ This is the text part of your message.
55
+
56
+ --boundary-goes-here--'
57
+
58
+ # The envelope recipient is defined here, as well as your MIME string
59
+ data = {:to => 'sally@example.com', :message => mime_string}
60
+
61
+ mg_client.send_message "sending_domain.com", data
62
+
63
+ ```
64
+
65
+ **Build a message, part by part, with Message Builder:**
66
+
67
+ ```ruby
68
+ mb_obj = Mailgun::MessageBuilder.new
69
+
70
+ mb_obj.set_from_address "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
71
+ mb_obj.add_recipient :to, "recipient@example.com", {'first' => 'Recipient', 'last' => 'User'}
72
+ mb_obj.set_subject "This is the subject!"
73
+ mb_obj.set_text_body "This is the text body."
74
+
75
+ mg_client.send_message "sending_domain.com", mb_obj
76
+ ```
77
+
78
+ **Build a message with attachments, part by part, with Message Builder:**
79
+
80
+ ```ruby
81
+ mb_obj = Mailgun::MessageBuilder.new
82
+
83
+ mb_obj.set_from_address "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
84
+ mb_obj.add_recipient :to, "recipient@example.com", {'first' => 'Recipient', 'last' => 'User'}
85
+ mb_obj.set_subject "This is the subject!"
86
+ mb_obj.set_text_body "This is the text body."
87
+
88
+ # Add separate attachments
89
+ mb_obj.add_attachment "/path/to/file/invoice_8675309.pdf", "Order Invoice - 8675309.pdf"
90
+ mb_obj.add_attachment "/path/to/file/datasheet_00001.pdf", "Product Datasheet - 00001.pdf"
91
+
92
+ # Attach inline image to message
93
+ mb_obj.add_inline_image "/path/to/file/product_image_00001.png"
94
+
95
+ mg_client.send_message "sending_domain.com", mb_obj
96
+ ```
97
+
98
+ **Send batches of 1000 messages per post:**
99
+
100
+ ```ruby
101
+ bm_obj = Mailgun::BatchMessage.new
102
+
103
+ # Build message using Message Builder
104
+ bm_obj.set_from_address "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
105
+ bm_obj.set_message_id("<20141014000000.11111.11111@example.com>") # optional
106
+ bm_obj.set_subject "This is the subject!"
107
+ bm_obj.set_text_body "This is the text body."
108
+
109
+ # Loop and add unlimited recipients (batch jobs will fire when thresholds reached)
110
+ bm_obj.add_recipient :to, "a_user@example.com"
111
+
112
+ # All message IDs returned in finalize method return
113
+ message_ids = @bm_obj.finalize
114
+ ```
115
+
116
+ ### Domains:
117
+ ____________________________________________________
118
+ **Get a list of all domains:**
119
+
120
+ ```ruby
121
+ result = @mg_client.get "domains", {:limit => 5, :skip => 0}
122
+ ```
123
+
124
+ **Get a single domain:**
125
+
126
+ ```ruby
127
+ result = @mg_client.get "domains/#{domain}"
128
+ ```
129
+
130
+ **Add a domain:**
131
+
132
+ ```ruby
133
+ result = @mg_client.post "domains", {:name => 'anothersample.mailgun.org',
134
+ :smtp_password => 'super_secret',
135
+ :spam_action => 'tag'}
136
+ ```
137
+ **Delete a Domain: **
138
+
139
+ ```ruby
140
+ result = @mg_client.delete "domains/#{domain}"
141
+ ```
142
+ ### Unsubscribes:
143
+ ____________________________________________________
144
+ **Get List of Unsubscribes: **
145
+
146
+ ```ruby
147
+ result = @mg_client.get "#{domain}/unsubscribes", {:limit => 50, :skip => 10}
148
+ ```
149
+
150
+ **Get Single Unsubscribe: **
151
+
152
+ ```ruby
153
+ result = @mg_client.get "#{domain}/unsubscribes/#{email_address}"
154
+ ```
155
+
156
+ **Unsubscribe a Recipient: **
157
+
158
+ ```ruby
159
+ result = @mg_client.post "#{domain}/unsubscribes", {:address => 'bob@example.com', :tag => 'mypromotion'}
160
+ ```
161
+
162
+ **Unsubscribe from all messages for a domain: **
163
+
164
+ ```ruby
165
+ result = @mg_client.post "#{domain}/unsubscribes", {:address => 'bob@example.com', :tag => '*'}
166
+ ```
167
+
168
+ **Remove an unsubscribe: **
169
+
170
+ ```ruby
171
+ result = @mg_client.delete "#{domain}/unsubscribes/#{email_address}"
172
+ ```
173
+
174
+ ### Complaints:
175
+ ____________________________________________________
176
+ **Get List of Complaints: **
177
+
178
+ ```ruby
179
+ result = @mg_client.get "#{domain}/complaints", {:limit => 50, :skip => 10}
180
+ ```
181
+
182
+ **Get a Single Complaint: **
183
+
184
+ ```ruby
185
+ result = @mg_client.get "#{domain}/complaints/#{email_address}"
186
+ ```
187
+ **Create a complaint: **
188
+
189
+ ```ruby
190
+ result = @mg_client.post "#{domain}/complaint", {:address => 'bob@example.com'}
191
+ ```
192
+
193
+ **Remove a complaint: **
194
+
195
+ ```ruby
196
+ result = @mg_client.delete "#{domain}/complaint/#{email_address}"
197
+ ```
198
+
199
+ ### Bounces:
200
+ ____________________________________________________
201
+ **Get List of Bounces: **
202
+
203
+ ```ruby
204
+ result = @mg_client.get "#{domain}/bounces", {:limit => 50, :skip => 10}
205
+ ```
206
+
207
+ **Get a Single Bounce Event: **
208
+
209
+ ```ruby
210
+ result = @mg_client.get "#{domain}/bounces/#{email_address}"
211
+ ```
212
+
213
+ **Create a Bounce: **
214
+
215
+ ```ruby
216
+ result = @mg_client.post "#{domain}/bounces", {:address => 'bob@example.com',
217
+ :code => 550,
218
+ :error => 'Mailbox does not exist.'}
219
+ ```
220
+
221
+ **Remove a Bounced Address: **
222
+
223
+ ```ruby
224
+ result = @mg_client.delete "#{domain}/bounces/#{email_address}"
225
+ ```
226
+
227
+ ### Statistics:
228
+ ____________________________________________________
229
+ **Get Statistics: **
230
+
231
+ ```ruby
232
+ result = @mg_client.get "#{domain}/stats", {:limit => 50,
233
+ :skip => 10,
234
+ :event => 'sent',
235
+ "start-date" => 'Mon, 13 Feb 2015 00:00:00 GMT'}
236
+ ```
237
+
238
+ **Remove a Tag: **
239
+
240
+ ```ruby
241
+ result = @mg_client.delete "#{domain}/tags/#{tag}"
242
+ ```
243
+ ### Events:
244
+ ____________________________________________________
245
+ **Get Event: **
246
+
247
+ ```ruby
248
+ result = @mg_client.get "#{domain}/events", {:event => 'rejected'}
249
+ ```
250
+
251
+ ### Routes:
252
+ ____________________________________________________
253
+ **Get List of Routes: **
254
+
255
+ ```ruby
256
+ result = @mg_client.get "routes", {:limit => 50, :skip => 10}
257
+ ```
258
+
259
+ **Get a Single Route by ID: **
260
+
261
+ ```ruby
262
+ result = @mg_client.get "routes/#{route_id}"
263
+ ```
264
+ **Create a Route: **
265
+
266
+ ```ruby
267
+ result = @mg_client.post "routes", {:priority => 10,
268
+ :description => 'This is a test route',
269
+ :expression => 'match_recipient(".*@gmail.com")',
270
+ :action => 'forward("alice@example.com")'}
271
+ ```
272
+ **Update a Route: **
273
+
274
+ ```ruby
275
+ result = @mg_client.put "routes/#{route_id}", {:priority => 10,
276
+ :description => 'This is a test route',
277
+ :expression => 'match_recipient(".*@gmail.com")',
278
+ :action => 'forward("alice@example.com")'}
279
+ ```
280
+ **Remove a Route: **
281
+
282
+ ```ruby
283
+ result = @mg_client.delete "routes/#{route_id}"
284
+ ```
285
+ ### Campaigns:
286
+ ____________________________________________________
287
+ **Get List of Campaigns: **
288
+
289
+ ```ruby
290
+ result = @mg_client.get "#{domain}/campaigns", {:limit => 50, :skip => 10}
291
+ ```
292
+
293
+ **Get a Single Campaign: **
294
+
295
+ ```ruby
296
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}"
297
+ ```
298
+
299
+ **Create a Campaign: **
300
+
301
+ ```ruby
302
+ result = @mg_client.post "#{domain}/campaigns", {:name => 'My Campaign',
303
+ :id => 'campaign_123_2014'}
304
+ ```
305
+
306
+ **Update a Campaign: **
307
+
308
+ ```ruby
309
+ result = @mg_client.put "#{domain}/campaigns/#{campaign_id}", {:name => 'My Campaign',
310
+ :id => 'campaign_123_2014'}
311
+ ```
312
+
313
+ **Remove a Campaign: **
314
+
315
+ ```ruby
316
+ result = @mg_client.delete "#{domain}/campaigns/#{campaign_id}"
317
+ ```
318
+
319
+ **Get Campaign Events: **
320
+
321
+ ```ruby
322
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/events", {:event => 'clicked',
323
+ :recipient => 'test@example.com',
324
+ :country => 'US',
325
+ :region => 'TX',
326
+ :limit => 100,
327
+ :page => 1,
328
+ :count => true}
329
+ ```
330
+
331
+ **Get a Single Campaign's Stats: **
332
+
333
+ ```ruby
334
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/stats", {:groupby => 'domain'}
335
+ ```
336
+
337
+ **Get a Single Campaign's Click Stats: **
338
+
339
+ ```ruby
340
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/clicks", {:groupby => 'hour',
341
+ :country => 'US',
342
+ :region => 'TX',
343
+ :city => 'Austin',
344
+ :limit => 100,
345
+ :page => 1,
346
+ :count => true}
347
+ ```
348
+
349
+ **Get a Single Campaign's Click Opens: **
350
+
351
+ ```ruby
352
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/opens", {:groupby => 'hour',
353
+ :country => 'US',
354
+ :region => 'TX',
355
+ :city => 'Austin',
356
+ :limit => 100,
357
+ :page => 1,
358
+ :count => true}
359
+ ```
360
+
361
+ **Get a Single Campaign's Click Unsubscribes: **
362
+
363
+ ```ruby
364
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/unsubscribes", {:groupby => 'hour',
365
+ :country => 'US',
366
+ :region => 'TX',
367
+ :city => 'Austin',
368
+ :limit => 100,
369
+ :page => 1,
370
+ :count => true}
371
+ ```
372
+
373
+ **Get a Single Campaign's Click Complaints: **
374
+
375
+ ```ruby
376
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/complaints", {:groupby => 'hour',
377
+ :limit => 100,
378
+ :page => 1,
379
+ :count => true}
380
+ ```
381
+
382
+ ### Webhooks:
383
+ ____________________________________________________
384
+
385
+ **Get List of Webhooks: **
386
+
387
+ ```ruby
388
+ result = @mg_client.get "domains/#{domain}/webhooks"
389
+ ```
390
+
391
+ **Get a Webhook Properties: **
392
+
393
+ ```ruby
394
+ result = @mg_client.get "domains/#{domain}/webhooks/#{webhook_id}"
395
+ ```
396
+
397
+ **Create a Webhook: **
398
+
399
+ ```ruby
400
+ result = @mg_client.post "domains/#{domain}/webhooks", {:id => 'bounce',
401
+ :url => 'http://example.com/mailgun/events/bounce'}
402
+ ```
403
+
404
+ **Update a Webhook: **
405
+
406
+ ```ruby
407
+ result = @mg_client.put "domains/#{domain}/webhooks/#{webhook_id}", {:id => 'bounce',
408
+ :url => 'http://example.com/mailgun/events/bounce'}
409
+ ```
410
+
411
+ **Remove a Webhook: **
412
+
413
+ ```ruby
414
+ result = @mg_client.delete "domains/#{domain}/webhooks/#{webhook_id}"
415
+ ```
416
+
417
+ ### Mailing Lists:
418
+ ____________________________________________________
419
+
420
+ **Get list of Lists: **
421
+
422
+ ```ruby
423
+ result = @mg_client.get "lists"
424
+ ```
425
+
426
+ **Get List Properties: **
427
+
428
+ ```ruby
429
+ result = @mg_client.get "lists/#{list_address}"
430
+ ```
431
+
432
+ **Create a List: **
433
+
434
+ ```ruby
435
+ result = @mg_client.post "lists", {:address => 'dev.group@samples.mailgun.org',
436
+ :name => 'Development Group List',
437
+ :description => 'List of all developers.',
438
+ :access_level => 'members'}
439
+ ```
440
+
441
+ **Update a List: **
442
+
443
+ ```ruby
444
+ result = @mg_client.put "lists/#{list_address}", {:address => 'dev.group@samples.mailgun.org',
445
+ :name => 'Development Group List',
446
+ :description => 'List of all developers.',
447
+ :access_level => 'members'}
448
+ ```
449
+
450
+ **Remove a List: **
451
+
452
+ ```ruby
453
+ result = @mg_client.delete "lists/#{list_address}"
454
+ ```
455
+
456
+ **Get List Members: **
457
+
458
+ ```ruby
459
+ result = @mg_client.get "lists/#{list_address}/members"
460
+ ```
461
+
462
+ **Get List Member Properties: **
463
+
464
+ ```ruby
465
+ result = @mg_client.get "lists/#{list_address}/members/#{member_address}"
466
+ ```
467
+
468
+ **Add Member to List: **
469
+
470
+ ```ruby
471
+ result = @mg_client.post "lists/#{list_address}/members/#{member_address}", {:address => 'jane@samples.mailgun.org',
472
+ :name => 'Jane Doe',
473
+ :vars => '{"first": "Jane", "last": "Doe"}',
474
+ :subscribed => true,
475
+ :upsert => 'no'}
476
+ ```
477
+
478
+ **Update Member on List: **
479
+
480
+ ```ruby
481
+ result = @mg_client.put "lists/#{list_address}/members/#{member_address}", {:address => 'jane@samples.mailgun.org',
482
+ :name => 'Jane Doe',
483
+ :vars => '{"first": "Jane", "last": "Doe"}',
484
+ :subscribed => true}
485
+ ```
486
+
487
+ **Delete a Member from List: **
488
+
489
+ ```ruby
490
+ result = @mg_client.delete "lists/#{list_address}/members/#{member_address}"
491
+ ```
492
+
493
+ **Get Stats for List: **
494
+
495
+ ```ruby
496
+ result = @mg_client.get "lists/#{list_address}/stats"
497
+ ```
498
+
499
+ ### Email Validation:
500
+ ____________________________________________________
501
+ **Validate Single Address: **
502
+
503
+ ```ruby
504
+ result = @mg_client.get "address/validate", {:address => 'test@example.com'}
505
+ ```
506
+
507
+ **Parse Addresses: **
508
+
509
+ ```ruby
510
+ result = @mg_client.get "address/parse", {:addresses => 'test@example.com, "First Last <first.last@example.com>',
511
+ :syntax_only => true}
512
+ ```
513
+
514
+
515
+ Support and Feedback
516
+ --------------------
517
+
518
+ Be sure to visit the Mailgun official
519
+ [documentation website](https://documentation.mailgun.com/) for additional
520
+ information about our API.
521
+
522
+ If you find a bug, please submit the issue in Github directly.
523
+ [Mailgun-Ruby Issues](https://github.com/mailgun/Mailgun-PHP/issues)
524
+
525
+ As always, if you need additional assistance, drop us a note at
526
+ [support@mailgun.com](mailto:support@mailgun.com).