mailgun-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,150 @@
1
+ Mailgun-Ruby
2
+ ============
3
+
4
+ This is the Mailgun Ruby Library. This library contains methods for easily interacting
5
+ with the Mailgun API.
6
+ Below are examples to get you started. For additional examples, please see our
7
+ official documentation
8
+ at http://documentation.mailgun.com
9
+
10
+ Installation
11
+ ------------
12
+ Via RubyGems
13
+
14
+ ```ruby
15
+ gem install mailgun-ruby
16
+ ```
17
+
18
+ Gemfile:
19
+
20
+ ```ruby
21
+ gem 'mailgun-ruby', "~>1.0.0"
22
+ ```
23
+
24
+ Include
25
+ --------
26
+
27
+ ```ruby
28
+ require 'mailgun'
29
+ ```
30
+
31
+ Usage
32
+ -----
33
+ Here's how to send a message using the library:
34
+
35
+ ```ruby
36
+ # First, instantiate the Mailgun Client with your API key
37
+ mg_client = Mailgun::Client.new "your-api-key"
38
+
39
+ # Define your message parameters
40
+ message_params = {:from => 'bob@sending_domain.com',
41
+ :to => 'sally@example.com',
42
+ :subject => 'The Ruby SDK is awesome!',
43
+ :text => 'It is really easy to send a message!'}
44
+
45
+ # Send your message through the client
46
+ mg_client.send_message "sending_domain.com", message_params
47
+ ```
48
+
49
+ Or obtain the last couple log items:
50
+
51
+ ```ruby
52
+ # First, instantiate the Mailgun Client with your API key
53
+ mg_client = Mailgun::Client.new "your-api-key"
54
+
55
+ # Define the domain you wish to query
56
+ domain = "example.com"
57
+
58
+ # Issue the get request
59
+ result = mg_client.get("#{domain}/events", {:event => 'delivered'})
60
+ ```
61
+
62
+ Response
63
+ --------
64
+
65
+ The results are returned in a Response class:
66
+
67
+ ```ruby
68
+ result = mg_client.get("#{domain}/events", {:event => 'delivered'})
69
+
70
+ # To Ruby standard Hash.
71
+ result.to_h
72
+
73
+ # To YAML.
74
+ result.to_yaml
75
+
76
+ # Or raw JSON
77
+ result.body
78
+ ```
79
+
80
+ Here's an example, breaking out the response:
81
+
82
+ ```ruby
83
+ mg_client = Mailgun::Client.new("your-api-key")
84
+
85
+ message_params = {:from => 'bob@example.com',
86
+ :to => 'sally@example.com',
87
+ :subject => 'The Ruby SDK is awesome!',
88
+ :text => 'It is really easy to send a message!'}
89
+
90
+ result = mg_client.send_message("example.com", message_params).to_h!
91
+
92
+ message_id = result['id']
93
+ message = result['message']
94
+ ```
95
+
96
+ Debugging
97
+ ---------
98
+
99
+ Debugging the Ruby Library can be really helpful when things aren't working quite right.
100
+ To debug the library, here are some suggestions:
101
+
102
+ Set the endpoint to Mailgun's Postbin. A Postbin is a web service that allows you to
103
+ post data, which is then displayed through a browser. This allows you to quickly determine
104
+ what is actually being transmitted to Mailgun's API.
105
+
106
+ **Step 1 - Create a new Postbin.**
107
+ Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that URL.
108
+
109
+ **Step 2 - Instantiate the Mailgun client using Postbin.**
110
+
111
+ *Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers. For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".*
112
+
113
+ ```ruby
114
+ # First, instantiate the Mailgun Client with your API key
115
+ mg_client = Mailgun::Client.new("your-api-key", "bin.mailgun.net", "aecf68de", ssl = false)
116
+
117
+ # Define your message parameters
118
+ message_params = {:from => 'bob@sending_domain.com',
119
+ :to => 'sally@example.com',
120
+ :subject => 'The Ruby SDK is awesome!',
121
+ :text => 'It is really easy to send a message!'}
122
+
123
+ # Send your message through the client
124
+ mg_client.send_message("sending_domain.com", message_params)
125
+ ```
126
+
127
+ For usage examples on each API endpoint, head over to our official documentation
128
+ pages. Or the [Snippets](Snippets.md) file.
129
+
130
+ This SDK includes a [Message Builder](Messages.md),
131
+ [Batch Message](Messages.md) and [Opt-In Handler](OptInHandler.md) component.
132
+
133
+ Message Builder allows you to quickly create the array of parameters, required
134
+ to send a message, by calling a methods for each parameter.
135
+ Batch Message is an extension of Message Builder, and allows you to easily send
136
+ a batch message job within a few seconds. The complexity of
137
+ batch messaging is eliminated!
138
+
139
+ Support and Feedback
140
+ --------------------
141
+
142
+ Be sure to visit the Mailgun official
143
+ [documentation website](http://documentation.mailgun.com/) for additional
144
+ information about our API.
145
+
146
+ If you find a bug, please submit the issue in Github directly.
147
+ [Mailgun-Ruby Issues](https://github.com/mailgun/mailgun-ruby/issues)
148
+
149
+ As always, if you need additional assistance, drop us a note at
150
+ [support@mailgun.com](mailto:support@mailgun.com).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'bundler/gem_tasks'
3
+ rescue LoadError
4
+ end
5
+
6
+ require 'rake'
7
+ require 'rspec/core/rake_task'
8
+ require 'bundler/gem_tasks'
9
+
10
+ desc "Build Gem"
11
+ task :default do
12
+ system "gem build mailgun.gemspec"
13
+ end
14
+
15
+ desc "Run default unit specs"
16
+ RSpec::Core::RakeTask.new('spec') do |t|
17
+ t.rspec_opts = %w{--colour --format progress}
18
+ t.pattern = 'spec/unit/*_spec.rb', 'spec/unit/*/*_spec.rb'
19
+ end
20
+
21
+ desc "Run unit specs"
22
+ RSpec::Core::RakeTask.new('spec:unit') do |t|
23
+ t.rspec_opts = %w{--colour --format progress}
24
+ t.pattern = 'spec/unit/*_spec.rb', 'spec/unit/*/*_spec.rb'
25
+ end
26
+
27
+ desc "Run integration specs"
28
+ # Before running integration tests, you need to specify
29
+ # a valid API KEY in the spec/spec_helper.rb file.
30
+ RSpec::Core::RakeTask.new('spec:integration') do |t|
31
+ t.rspec_opts = %w{--colour --format progress}
32
+ t.pattern = 'spec/integration/*_spec.rb'
33
+ end
data/Snippets.md ADDED
@@ -0,0 +1,506 @@
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 = '
45
+ From: Bob Sample <example@example.com>
46
+ MIME-Version: 1.0
47
+ Content-Type: multipart/mixed;
48
+ boundary="--boundary-goes-here--"
49
+
50
+ Your Multipart MIME body
51
+
52
+ --boundary-goes-here--
53
+ Content-Type: text/plain
54
+
55
+ This is the text part of your message.
56
+
57
+ --boundary-goes-here--'
58
+
59
+ # The envelope recipient is defined here, as well as your MIME string
60
+ data = {:to => 'sally@example.com', :message => mime_string}
61
+
62
+ mg_client.send_message "sending_domain.com", data
63
+
64
+ ```
65
+
66
+ **Build a message, part by part, with Message Builder:**
67
+
68
+ ```ruby
69
+ mb_obj = Mailgun::MessageBuilder.new
70
+
71
+ mb_obj.set_from_address :from, "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
72
+ mb_obj.add_recipient :to, "recipient@example.com", {'first' => 'Recipient', 'last' => 'User'}
73
+ mb_obj.set_subject :subject, "This is the subject!"
74
+ mb_obj.set_text_body :text, "This is the text body."
75
+
76
+ mg_client.send_message "sending_domain.com", mb_obj
77
+ ```
78
+
79
+ **Send batches of 1000 messages per post:**
80
+
81
+ ```ruby
82
+ bm_obj = Mailgun::BatchMessage.new
83
+
84
+ # Build message using Message Builder
85
+ bm_obj.set_from_address :from, "sender@example.com", {'first' => 'Sending', 'last' => 'User'}
86
+ bm_obj.set_subject :subject, "This is the subject!"
87
+ bm_obj.set_text_body :text, "This is the text body."
88
+
89
+ # Loop and add unlimited recipients (batch jobs will fire when thresholds reached)
90
+ bm_obj.add_recipient :to, "a_user@example.com"
91
+
92
+ # All message IDs returned in finalize method return
93
+ message_ids = @bm_obj.finalize
94
+ ```
95
+
96
+ ### Domains:
97
+ ____________________________________________________
98
+ **Get a list of all domains:**
99
+
100
+ ```ruby
101
+ result = @mg_client.get "domains", {:limit => 5, :skip => 0}
102
+ ```
103
+
104
+ **Get a single domain:**
105
+
106
+ ```ruby
107
+ result = @mg_client.get "domains/#{domain}"
108
+ ```
109
+
110
+ **Add a domain:**
111
+
112
+ ```ruby
113
+ result = @mg_client.post "domains", {:name => 'anothersample.mailgun.org',
114
+ :smtp_password => 'super_secret',
115
+ :spam_action => 'tag'}
116
+ ```
117
+ **Delete a Domain: **
118
+
119
+ ```ruby
120
+ result = @mg_client.delete "domains/#{domain}"
121
+ ```
122
+ ### Unsubscribes:
123
+ ____________________________________________________
124
+ **Get List of Unsubscribes: **
125
+
126
+ ```ruby
127
+ result = @mg_client.get "#{domain}/unsubscribes", {:limit => 50, :skip => 10}
128
+ ```
129
+
130
+ **Get Single Unsubscribe: **
131
+
132
+ ```ruby
133
+ result = @mg_client.get "#{domain}/unsubscribes/#{email_address}"
134
+ ```
135
+
136
+ **Unsubscribe a Recipient: **
137
+
138
+ ```ruby
139
+ result = @mg_client.post "#{domain}/unsubscribes", {:address => 'bob@example.com', :tag => 'mypromotion'}
140
+ ```
141
+
142
+ **Unsubscribe from all messages for a domain: **
143
+
144
+ ```ruby
145
+ result = @mg_client.post "#{domain}/unsubscribes", {:address => 'bob@example.com', :tag => '*'}
146
+ ```
147
+
148
+ **Remove an unsubscribe: **
149
+
150
+ ```ruby
151
+ result = @mg_client.delete "#{domain}/unsubscribes/#{email_address}"
152
+ ```
153
+
154
+ ### Complaints:
155
+ ____________________________________________________
156
+ **Get List of Complaints: **
157
+
158
+ ```ruby
159
+ result = @mg_client.get "#{domain}/complaints", {:limit => 50, :skip => 10}
160
+ ```
161
+
162
+ **Get a Single Complaint: **
163
+
164
+ ```ruby
165
+ result = @mg_client.get "#{domain}/complaints/#{email_address}"
166
+ ```
167
+ **Create a complaint: **
168
+
169
+ ```ruby
170
+ result = @mg_client.post "#{domain}/complaint", {:address => 'bob@example.com'}
171
+ ```
172
+
173
+ **Remove a complaint: **
174
+
175
+ ```ruby
176
+ result = @mg_client.delete "#{domain}/complaint/#{email_address}"
177
+ ```
178
+
179
+ ### Bounces:
180
+ ____________________________________________________
181
+ **Get List of Bounces: **
182
+
183
+ ```ruby
184
+ result = @mg_client.get "#{domain}/bounces", {:limit => 50, :skip => 10}
185
+ ```
186
+
187
+ **Get a Single Bounce Event: **
188
+
189
+ ```ruby
190
+ result = @mg_client.get "#{domain}/bounces/#{email_address}"
191
+ ```
192
+
193
+ **Create a Bounce: **
194
+
195
+ ```ruby
196
+ result = @mg_client.post "#{domain}/bounces", {:address => 'bob@example.com',
197
+ :code => 550,
198
+ :error => 'Mailbox does not exist.'}
199
+ ```
200
+
201
+ **Remove a Bounced Address: **
202
+
203
+ ```ruby
204
+ result = @mg_client.delete "#{domain}/bounces/#{email_address}"
205
+ ```
206
+
207
+ ### Statistics:
208
+ ____________________________________________________
209
+ **Get Statistics: **
210
+
211
+ ```ruby
212
+ result = @mg_client.get "#{domain}/stats", {:limit => 50,
213
+ :skip => 10,
214
+ :event => 'sent',
215
+ "start-date" => 'Mon, 13 Feb 2015 00:00:00 GMT'}
216
+ ```
217
+
218
+ **Remove a Tag: **
219
+
220
+ ```ruby
221
+ result = @mg_client.delete "#{domain}/tags/#{tag}"
222
+ ```
223
+ ### Events:
224
+ ____________________________________________________
225
+ **Get Event: **
226
+
227
+ ```ruby
228
+ result = @mg_client.get "#{domain}/events", {:event => 'rejected'}
229
+ ```
230
+
231
+ ### Routes:
232
+ ____________________________________________________
233
+ **Get List of Routes: **
234
+
235
+ ```ruby
236
+ result = @mg_client.get "routes", {:limit => 50, :skip => 10}
237
+ ```
238
+
239
+ **Get a Single Route by ID: **
240
+
241
+ ```ruby
242
+ result = @mg_client.get "routes/#{route_id}"
243
+ ```
244
+ **Create a Route: **
245
+
246
+ ```ruby
247
+ result = @mg_client.post "routes", {:priority => 10,
248
+ :description => 'This is a test route',
249
+ :expression => 'match_recipient(".*@gmail.com")',
250
+ :action => 'forward("alice@example.com")'}
251
+ ```
252
+ **Update a Route: **
253
+
254
+ ```ruby
255
+ result = @mg_client.put "routes/#{route_id}", {:priority => 10,
256
+ :description => 'This is a test route',
257
+ :expression => 'match_recipient(".*@gmail.com")',
258
+ :action => 'forward("alice@example.com")'}
259
+ ```
260
+ **Remove a Route: **
261
+
262
+ ```ruby
263
+ result = @mg_client.delete "routes/#{route_id}"
264
+ ```
265
+ ### Campaigns:
266
+ ____________________________________________________
267
+ **Get List of Campaigns: **
268
+
269
+ ```ruby
270
+ result = @mg_client.get "#{domain}/campaigns", {:limit => 50, :skip => 10}
271
+ ```
272
+
273
+ **Get a Single Campaign: **
274
+
275
+ ```ruby
276
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}"
277
+ ```
278
+
279
+ **Create a Campaign: **
280
+
281
+ ```ruby
282
+ result = @mg_client.post "#{domain}/campaigns", {:name => 'My Campaign',
283
+ :id => 'campaign_123_2014'}
284
+ ```
285
+
286
+ **Update a Campaign: **
287
+
288
+ ```ruby
289
+ result = @mg_client.put "#{domain}/campaigns/#{campaign_id}", {:name => 'My Campaign',
290
+ :id => 'campaign_123_2014'}
291
+ ```
292
+
293
+ **Remove a Campaign: **
294
+
295
+ ```ruby
296
+ result = @mg_client.delete "#{domain}/campaigns/#{campaign_id}"
297
+ ```
298
+
299
+ **Get Campaign Events: **
300
+
301
+ ```ruby
302
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/events", {:event => 'clicked',
303
+ :recipient => 'test@example.com',
304
+ :country => 'US',
305
+ :region => 'TX',
306
+ :limit => 100,
307
+ :page => 1,
308
+ :count => true}
309
+ ```
310
+
311
+ **Get a Single Campaign's Stats: **
312
+
313
+ ```ruby
314
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/stats", {:groupby => 'domain'}
315
+ ```
316
+
317
+ **Get a Single Campaign's Click Stats: **
318
+
319
+ ```ruby
320
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/clicks", {:groupby => 'hour',
321
+ :country => 'US',
322
+ :region => 'TX',
323
+ :city => 'Austin',
324
+ :limit => 100,
325
+ :page => 1,
326
+ :count => true}
327
+ ```
328
+
329
+ **Get a Single Campaign's Click Opens: **
330
+
331
+ ```ruby
332
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/opens", {:groupby => 'hour',
333
+ :country => 'US',
334
+ :region => 'TX',
335
+ :city => 'Austin',
336
+ :limit => 100,
337
+ :page => 1,
338
+ :count => true}
339
+ ```
340
+
341
+ **Get a Single Campaign's Click Unsubscribes: **
342
+
343
+ ```ruby
344
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/unsubscribes", {:groupby => 'hour',
345
+ :country => 'US',
346
+ :region => 'TX',
347
+ :city => 'Austin',
348
+ :limit => 100,
349
+ :page => 1,
350
+ :count => true}
351
+ ```
352
+
353
+ **Get a Single Campaign's Click Complaints: **
354
+
355
+ ```ruby
356
+ result = @mg_client.get "#{domain}/campaigns/#{campaign_id}/complaints", {:groupby => 'hour',
357
+ :limit => 100,
358
+ :page => 1,
359
+ :count => true}
360
+ ```
361
+
362
+ ### Webhooks:
363
+ ____________________________________________________
364
+
365
+ **Get List of Webhooks: **
366
+
367
+ ```ruby
368
+ result = @mg_client.get "domains/#{domain}/webhooks"
369
+ ```
370
+
371
+ **Get a Webhook Properties: **
372
+
373
+ ```ruby
374
+ result = @mg_client.get "domains/#{domain}/webhooks/#{webhook_id}"
375
+ ```
376
+
377
+ **Create a Webhook: **
378
+
379
+ ```ruby
380
+ result = @mg_client.post "domains/#{domain}/webhooks", {:id => 'bounce',
381
+ :url => 'http://example.com/mailgun/events/bounce'}
382
+ ```
383
+
384
+ **Update a Webhook: **
385
+
386
+ ```ruby
387
+ result = @mg_client.put "domains/#{domain}/webhooks/#{webhook_id}", {:id => 'bounce',
388
+ :url => 'http://example.com/mailgun/events/bounce'}
389
+ ```
390
+
391
+ **Remove a Webhook: **
392
+
393
+ ```ruby
394
+ result = @mg_client.delete "domains/#{domain}/webhooks/#{webhook_id}"
395
+ ```
396
+
397
+ ### Mailing Lists:
398
+ ____________________________________________________
399
+
400
+ **Get list of Lists: **
401
+
402
+ ```ruby
403
+ result = @mg_client.get "lists"
404
+ ```
405
+
406
+ **Get List Properties: **
407
+
408
+ ```ruby
409
+ result = @mg_client.get "lists/#{list_address}"
410
+ ```
411
+
412
+ **Create a List: **
413
+
414
+ ```ruby
415
+ result = @mg_client.post "lists", {:address => 'dev.group@samples.mailgun.org',
416
+ :name => 'Development Group List',
417
+ :description => 'List of all developers.',
418
+ :access_level => 'members'}
419
+ ```
420
+
421
+ **Update a List: **
422
+
423
+ ```ruby
424
+ result = @mg_client.put "lists/#{list_address}", {:address => 'dev.group@samples.mailgun.org',
425
+ :name => 'Development Group List',
426
+ :description => 'List of all developers.',
427
+ :access_level => 'members'}
428
+ ```
429
+
430
+ **Remove a List: **
431
+
432
+ ```ruby
433
+ result = @mg_client.delete "lists/#{list_address}"
434
+ ```
435
+
436
+ **Get List Members: **
437
+
438
+ ```ruby
439
+ result = @mg_client.get "lists/#{list_address}/members"
440
+ ```
441
+
442
+ **Get List Member Properties: **
443
+
444
+ ```ruby
445
+ result = @mg_client.get "lists/#{list_address}/members/#{member_address}"
446
+ ```
447
+
448
+ **Add Member to List: **
449
+
450
+ ```ruby
451
+ result = @mg_client.post "lists/#{list_address}/members/#{member_address}", {:address => 'jane@samples.mailgun.org',
452
+ :name => 'Jane Doe',
453
+ :vars => '{"first": "Jane", "last": "Doe"}',
454
+ :subscribed => true,
455
+ :upsert => 'no'}
456
+ ```
457
+
458
+ **Update Member on List: **
459
+
460
+ ```ruby
461
+ result = @mg_client.put "lists/#{list_address}/members/#{member_address}", {:address => 'jane@samples.mailgun.org',
462
+ :name => 'Jane Doe',
463
+ :vars => '{"first": "Jane", "last": "Doe"}',
464
+ :subscribed => true}
465
+ ```
466
+
467
+ **Delete a Member from List: **
468
+
469
+ ```ruby
470
+ result = @mg_client.delete "lists/#{list_address}/members/#{member_address}"
471
+ ```
472
+
473
+ **Get Stats for List: **
474
+
475
+ ```ruby
476
+ result = @mg_client.get "lists/#{list_address}/stats"
477
+ ```
478
+
479
+ ### Email Validation:
480
+ ____________________________________________________
481
+ **Validate Single Address: **
482
+
483
+ ```ruby
484
+ result = @mg_client.get "address/validate", {:address => 'test@example.com'}
485
+ ```
486
+
487
+ **Parse Addresses: **
488
+
489
+ ```ruby
490
+ result = @mg_client.get "address/parse", {:addresses => 'test@example.com, "First Last <first.last@example.com>',
491
+ :syntax_only => true}
492
+ ```
493
+
494
+
495
+ Support and Feedback
496
+ --------------------
497
+
498
+ Be sure to visit the Mailgun official
499
+ [documentation website](http://documentation.mailgun.com/) for additional
500
+ information about our API.
501
+
502
+ If you find a bug, please submit the issue in Github directly.
503
+ [Mailgun-Ruby Issues](https://github.com/mailgun/Mailgun-PHP/issues)
504
+
505
+ As always, if you need additional assistance, drop us a note at
506
+ [support@mailgun.com](mailto:support@mailgun.com).