4
4
[ ![ downloads] ( https://shields.io/gem/dt/mailtrap )] ( https://rubygems.org/gems/mailtrap )
5
5
[ ![ license] ( https://shields.io/badge/license-MIT-green )] ( https://opensource.org/licenses/MIT )
6
6
7
- # Mailtrap Ruby client - Official
7
+ # Official Mailtrap Ruby client
8
8
9
9
## Prerequisites
10
10
@@ -17,30 +17,32 @@ To get the most out of this official Mailtrap.io Ruby SDK:
17
17
18
18
This Ruby gem offers integration with the [ official API] ( https://api-docs.mailtrap.io/ ) for [ Mailtrap] ( https://mailtrap.io ) .
19
19
20
- Quickly integrate Mailtrap with your Ruby application.
20
+ Quickly add email sending functionality to your Ruby application with Mailtrap.
21
+
22
+ (This client uses API v2, for v1 refer to [ this documentation] ( https://mailtrap.docs.apiary.io/ ) )
21
23
22
24
Currently, with this SDK you can:
23
25
24
26
* ** Email API/SMTP**
25
- * Send an email (Transactional and Bulk streams)
26
- * Send an email with a template
27
- * Send a batch of emails (Transactional and Bulk streams)
27
+ * Send an email (Transactional and Bulk streams)
28
+ * Send an email with a template
29
+ * Send a batch of emails (Transactional and Bulk streams)
28
30
* ** Email Sandbox (Testing)**
29
- * Send an email
30
- * Send an email with a template
31
- * Message management
32
- * Inbox management
33
- * Project management
31
+ * Send an email
32
+ * Send an email with a template
33
+ * Message management
34
+ * Inbox management
35
+ * Project management
34
36
* ** Contact management**
35
- * Contacts CRUD
36
- * Lists CRUD
37
- * Contact fields CRUD
37
+ * Contacts CRUD
38
+ * Lists CRUD
39
+ * Contact fields CRUD
38
40
* ** General**
39
- * Templates CRUD
40
- * Suppressions management (find and delete)
41
- * Account access management
42
- * Permissions management
43
- * List accounts you have access to
41
+ * Templates CRUD
42
+ * Suppressions management (find and delete)
43
+ * Account access management
44
+ * Permissions management
45
+ * List accounts you have access to
44
46
45
47
## Installation
46
48
@@ -52,15 +54,11 @@ gem 'mailtrap'
52
54
53
55
And then execute:
54
56
55
- ``` bash
56
- bundle install
57
- ```
57
+ $ bundle install
58
58
59
59
Or install it yourself as:
60
60
61
- ``` bash
62
- gem install mailtrap
63
- ```
61
+ $ gem install mailtrap
64
62
65
63
## Usage
66
64
@@ -72,18 +70,13 @@ require 'mailtrap'
72
70
# For this example to work, you need to set up a sending domain,
73
71
# and obtain a token that is authorized to send from the domain.
74
72
75
- TOKEN = " <YOUR-TOKEN-HERE>"
76
- SENDER_EMAIL = " <[email protected] >"
77
- RECIPIENT_EMAIL = " <[email protected] >"
78
-
79
- client = Mailtrap ::Client .new (api_key: TOKEN )
80
- sender = { name: " Mailtrap Test" , email: SENDER_EMAIL }
73
+ client = Mailtrap ::Client .new (api_key: ' your-api-key' )
81
74
82
75
client.send(
83
- from: sender ,
84
- to: [{ email: RECIPIENT_EMAIL }],
85
- subject: " Hello from Mailtrap!" ,
86
- text: " Welcome to Mailtrap Sending!"
76
+ from: { email: ' [email protected] ' , name: ' Mailtrap Test ' } ,
77
+ to: [{
email: ' [email protected] ' }],
78
+ subject: ' Hello from Mailtrap!' ,
79
+ text: ' Welcome to Mailtrap Sending!'
87
80
)
88
81
```
89
82
@@ -92,12 +85,12 @@ client.send(
92
85
``` ruby
93
86
# config/environments/production.rb
94
87
config.action_mailer.delivery_method = :mailtrap
95
- config.action_mailer.mailtrap_settings = {
96
- api_key: ' your-api-key'
97
- }
98
88
99
89
# Set the MAILTRAP_API_KEY environment variable
100
- # using your hosting solution.
90
+ # using your hosting solution, or customize the settings:
91
+ config.action_mailer.mailtrap_settings = {
92
+ api_key: ENV .fetch(' MAILTRAP_API_KEY' )
93
+ }
101
94
```
102
95
103
96
### Pure Ruby
@@ -214,13 +207,26 @@ require 'mailtrap'
214
207
client = Mailtrap ::Client .new (api_key: ' your-api-key' )
215
208
templates = Mailtrap ::EmailTemplatesAPI .new (3229 , client)
216
209
217
- templates.create(
210
+ # Create a new email template
211
+ email_template = templates.create(
218
212
name: ' Welcome Email' ,
219
213
subject: ' Welcome to Mailtrap!' ,
220
214
body_html: ' <h1>Hello</h1>' ,
221
215
body_text: ' Hello' ,
222
216
category: ' welcome'
223
217
)
218
+
219
+ # Get all templates
220
+ templates.list
221
+
222
+ # Get a specific template
223
+ templates.get(email_template.id)
224
+
225
+ # Update a template
226
+ templates.update(email_template.id, name: ' Welcome Updated' )
227
+
228
+ # Delete a template
229
+ templates.delete(email_template.id)
224
230
```
225
231
226
232
### Contacts API
@@ -253,7 +259,7 @@ contact = contacts.create(
253
259
# Get contact
254
260
contact = contacts.get(contact.id)
255
261
256
- # Update contact
262
+ # Update contact (upsert by ID or email)
257
263
contacts.upsert(
258
264
contact.id,
259
265
@@ -263,6 +269,12 @@ contacts.upsert(
263
269
# List contacts
264
270
contacts.list
265
271
272
+ # Add contact to lists
273
+ contacts.add_to_lists(contact.id, [list.id])
274
+
275
+ # Remove contact from lists
276
+ contacts.remove_from_lists(contact.id, [list.id])
277
+
266
278
# Delete contact
267
279
contacts.delete(contact.id)
268
280
```
@@ -281,14 +293,14 @@ ActionMailer::Base.add_delivery_method :mailtrap_sandbox, Mailtrap::ActionMailer
281
293
# config/environments/production.rb
282
294
config.action_mailer.delivery_method = :mailtrap
283
295
config.action_mailer.mailtrap_settings = {
284
- api_key: ' your-api-key '
296
+ api_key: ENV .fetch( ' MAILTRAP_API_KEY ' )
285
297
}
286
298
config.action_mailer.mailtrap_bulk_settings = {
287
- api_key: ' your-api-key ' ,
299
+ api_key: ENV .fetch( ' MAILTRAP_API_KEY ' ) ,
288
300
bulk: true
289
301
}
290
302
config.action_mailer.mailtrap_sandbox_settings = {
291
- api_key: ' your-api-key ' ,
303
+ api_key: ENV .fetch( ' MAILTRAP_API_KEY ' ) ,
292
304
sandbox: true ,
293
305
inbox_id: 12
294
306
}
0 commit comments