2
2
[ ![ docs] ( https://shields.io/badge/docs-rubydoc.info-blue )] ( https://rubydoc.info/gems/mailtrap )
3
3
[ ![ gem] ( https://shields.io/gem/v/mailtrap )] ( https://rubygems.org/gems/mailtrap )
4
4
[ ![ downloads] ( https://shields.io/gem/dt/mailtrap )] ( https://rubygems.org/gems/mailtrap )
5
+ [ ![ license] ( https://shields.io/badge/license-MIT-green )] ( https://opensource.org/licenses/MIT )
5
6
7
+ # Mailtrap Ruby client - Official
6
8
9
+ ## Prerequisites
7
10
8
- # Official Mailtrap Ruby client
11
+ To get the most out of this official Mailtrap.io Ruby SDK:
9
12
10
- This Ruby gem offers integration with the [ official API] ( https://api-docs.mailtrap.io/ ) for [ Mailtrap] ( https://mailtrap.io ) .
13
+ * [ Create a Mailtrap account] ( https://mailtrap.io/signup )
14
+ * [ Verify your domain] ( https://mailtrap.io/sending/domains )
15
+
16
+ ## Supported functionality
11
17
12
- Quickly add email sending functionality to your Ruby application with Mailtrap.
18
+ This Ruby gem offers integration with the [ official API ] ( https://api-docs.mailtrap.io/ ) for [ Mailtrap] ( https://mailtrap.io ) .
13
19
14
- (This client uses API v2, for v1 refer to [ this documentation] ( https://mailtrap.docs.apiary.io/ ) )
20
+ Quickly integrate Mailtrap with your Ruby application.
21
+
22
+ Currently, with this SDK you can:
23
+
24
+ * ** 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)
28
+ * ** Email Sandbox (Testing)**
29
+ * Send an email
30
+ * Send an email with a template
31
+ * Message management
32
+ * Inbox management
33
+ * Project management
34
+ * ** Contact management**
35
+ * Contacts CRUD
36
+ * Lists CRUD
37
+ * Contact fields CRUD
38
+ * ** General**
39
+ * Templates CRUD
40
+ * Suppressions management (find and delete)
41
+ * Account access management
42
+ * Permissions management
43
+ * List accounts you have access to
15
44
16
45
## Installation
17
46
@@ -23,21 +52,51 @@ gem 'mailtrap'
23
52
24
53
And then execute:
25
54
26
- $ bundle install
55
+ ``` bash
56
+ bundle install
57
+ ```
27
58
28
59
Or install it yourself as:
29
60
30
- $ gem install mailtrap
61
+ ``` bash
62
+ gem install mailtrap
63
+ ```
31
64
32
65
## Usage
33
66
67
+ ### Minimal
68
+
69
+ ``` ruby
70
+ require ' mailtrap'
71
+
72
+ # For this example to work, you need to set up a sending domain,
73
+ # and obtain a token that is authorized to send from the domain.
74
+
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 }
81
+
82
+ client.send(
83
+ from: sender,
84
+ to: [{ email: RECIPIENT_EMAIL }],
85
+ subject: " Hello from Mailtrap!" ,
86
+ text: " Welcome to Mailtrap Sending!"
87
+ )
88
+ ```
89
+
34
90
### Ruby on Rails
35
91
36
92
``` ruby
37
- # place this code in config/environments/production.rb:
93
+ # config/environments/production.rb
38
94
config.action_mailer.delivery_method = :mailtrap
95
+ config.action_mailer.mailtrap_settings = {
96
+ api_key: ' your-api-key'
97
+ }
39
98
40
- # then set the MAILTRAP_API_KEY environment variable
99
+ # Set the MAILTRAP_API_KEY environment variable
41
100
# using your hosting solution.
42
101
```
43
102
@@ -70,7 +129,49 @@ client.send(
70
129
subject: ' You are awesome!' ,
71
130
text: ' Congrats for sending test email with Mailtrap!'
72
131
)
132
+ ```
133
+
134
+ ### Send Email Using Template
135
+
136
+ ``` ruby
137
+ require ' mailtrap'
138
+
139
+ client = Mailtrap ::Client .new (api_key: ' your-api-key' )
140
+
141
+ mail = Mailtrap ::Mail .from_template(
142
+ from: {
email: ' [email protected] ' ,
name: ' Mailtrap Test' },
143
+ to: [
144
+
145
+ ],
146
+ template_uuid: ' 2f45b0aa-bbed-432f-95e4-e145e1965ba2' ,
147
+ template_variables: {
148
+ ' user_name' => ' John Doe'
149
+ }
150
+ )
151
+
152
+ client.send(mail)
153
+ ```
154
+
155
+ ### Email Sandbox (Testing)
156
+
157
+ ``` ruby
158
+ require ' mailtrap'
159
+
160
+ # Send to sandbox inbox for testing
161
+ client = Mailtrap ::Client .new (
162
+ api_key: ' your-api-key' ,
163
+ sandbox: true ,
164
+ inbox_id: 12
165
+ )
73
166
167
+ client.send(
168
+ from: {
email: ' [email protected] ' ,
name: ' Mailtrap Test' },
169
+ to: [
170
+
171
+ ],
172
+ subject: ' Test Email' ,
173
+ text: ' This is a test email sent to sandbox inbox'
174
+ )
74
175
```
75
176
76
177
### Batch Sending
@@ -111,7 +212,7 @@ client.send_batch(
111
212
require ' mailtrap'
112
213
113
214
client = Mailtrap ::Client .new (api_key: ' your-api-key' )
114
- templates = Mailtrap ::EmailTemplatesAPI .new 3229 , client
215
+ templates = Mailtrap ::EmailTemplatesAPI .new ( 3229 , client)
115
216
116
217
templates.create(
117
218
name: ' Welcome Email' ,
@@ -122,37 +223,60 @@ templates.create(
122
223
)
123
224
```
124
225
125
- Refer to the [ ` examples ` ] ( examples ) folder for more examples:
226
+ ### Contacts API
126
227
127
- - [ Full] ( examples/full.rb )
128
- - [ Email template] ( examples/email_template.rb )
129
- - [ Batch Sending] ( examples/batch.rb )
130
- - [ ActionMailer] ( examples/action_mailer.rb )
131
- - [ Email Templates API] ( examples/email_templates_api.rb )
228
+ ``` ruby
229
+ require ' mailtrap'
132
230
133
- ### Content-Transfer-Encoding
231
+ client = Mailtrap ::Client .new (api_key: ' your-api-key' )
232
+ contacts = Mailtrap ::ContactsAPI .new (3229 , client)
233
+ contact_lists = Mailtrap ::ContactListsAPI .new (3229 , client)
234
+ contact_fields = Mailtrap ::ContactFieldsAPI .new (3229 , client)
235
+
236
+ # Create contact list
237
+ list = contact_lists.create(name: ' Test List' )
238
+
239
+ # Create contact field
240
+ field = contact_fields.create(
241
+ name: ' Nickname' ,
242
+ data_type: ' text' ,
243
+ merge_tag: ' nickname'
244
+ )
134
245
135
- ` mailtrap ` gem uses Mailtrap API to send emails. Mailtrap API does not try to
136
- replicate SMTP. That is why you should expect some limitations when it comes to
137
- sending. For example, ` /api/send ` endpoint ignores ` Content-Transfer-Encoding `
138
- (see ` headers ` in the [ API documentation ] ( https://railsware.stoplight.io/docs/mailtrap-api-docs/67f1d70aeb62c-send-email ) ).
139
- Meaning your recipients will receive emails only in the default encoding which
140
- is ` quoted-printable ` , if you send with Mailtrap API.
246
+ # Create contact
247
+ contact = contacts.create(
248
+ email: ' test@ example.com ' ,
249
+ fields: { field.merge_tag => ' John Doe ' },
250
+ list_ids: [list.id]
251
+ )
141
252
142
- For those who need to use ` 7bit ` or any other encoding, SMTP provides
143
- better flexibility in that regard. Go to your _ Mailtrap account_ → _ Email Sending_
144
- → _ Sending Domains_ → _ Your domain_ → _ SMTP/API Settings_ to find the SMTP
145
- configuration example.
253
+ # Get contact
254
+ contact = contacts.get(contact.id)
255
+
256
+ # Update contact
257
+ contacts.upsert(
258
+ contact.id,
259
+
260
+ fields: { field.merge_tag => ' Jane Doe' }
261
+ )
262
+
263
+ # List contacts
264
+ contacts.list
265
+
266
+ # Delete contact
267
+ contacts.delete(contact.id)
268
+ ```
146
269
147
270
### Multiple Mailtrap Clients
148
271
149
- You can configure two Mailtrap clients to operate simultaneously. This setup is
272
+ You can configure multiple Mailtrap clients to operate simultaneously. This setup is
150
273
particularly useful when you need to send emails using both the transactional
151
- and bulk APIs. Refer to the configuration example below :
274
+ and bulk APIs, or when using sandbox for testing :
152
275
153
276
``` ruby
154
277
# config/application.rb
155
278
ActionMailer ::Base .add_delivery_method :mailtrap_bulk , Mailtrap ::ActionMailer ::DeliveryMethod
279
+ ActionMailer ::Base .add_delivery_method :mailtrap_sandbox , Mailtrap ::ActionMailer ::DeliveryMethod
156
280
157
281
# config/environments/production.rb
158
282
config.action_mailer.delivery_method = :mailtrap
@@ -163,11 +287,57 @@ config.action_mailer.mailtrap_bulk_settings = {
163
287
api_key: ' your-api-key' ,
164
288
bulk: true
165
289
}
290
+ config.action_mailer.mailtrap_sandbox_settings = {
291
+ api_key: ' your-api-key' ,
292
+ sandbox: true ,
293
+ inbox_id: 12
294
+ }
166
295
167
296
# app/mailers/foo_mailer.rb
168
- mail(delivery_method: :mailtrap_bulk )
297
+ mail(delivery_method: :mailtrap_bulk ) # For bulk sending
298
+ mail(delivery_method: :mailtrap_sandbox ) # For sandbox testing
169
299
```
170
300
301
+ ## Examples
302
+
303
+ Refer to the [ ` examples ` ] ( examples ) folder for the source code of this and other advanced examples:
304
+
305
+ ### Contacts API
306
+
307
+ * [ Contacts] ( examples/contacts_api.rb )
308
+
309
+ ### Sending API
310
+
311
+ * [ Full] ( examples/full.rb )
312
+ * [ Email template] ( examples/email_template.rb )
313
+ * [ ActionMailer] ( examples/action_mailer.rb )
314
+
315
+ ### Batch Sending API
316
+
317
+ * [ Batch Sending] ( examples/batch.rb )
318
+
319
+ ### Templates API
320
+
321
+ * [ Email Templates API] ( examples/email_templates_api.rb )
322
+
323
+ ### Email Sandbox (Testing) API
324
+
325
+ * [ Sandbox examples in Full] ( examples/full.rb )
326
+
327
+ ## Content-Transfer-Encoding
328
+
329
+ ` mailtrap ` gem uses Mailtrap API to send emails. Mailtrap API does not try to
330
+ replicate SMTP. That is why you should expect some limitations when it comes to
331
+ sending. For example, ` /api/send ` endpoint ignores ` Content-Transfer-Encoding `
332
+ (see ` headers ` in the [ API documentation] ( https://railsware.stoplight.io/docs/mailtrap-api-docs/67f1d70aeb62c-send-email ) ).
333
+ Meaning your recipients will receive emails only in the default encoding which
334
+ is ` quoted-printable ` , if you send with Mailtrap API.
335
+
336
+ For those who need to use ` 7bit ` or any other encoding, SMTP provides
337
+ better flexibility in that regard. Go to your _ Mailtrap account_ → _ Email Sending_
338
+ → _ Sending Domains_ → _ Your domain_ → _ SMTP/API Settings_ to find the SMTP
339
+ configuration example.
340
+
171
341
## Migration guide v1 → v2
172
342
173
343
Change ` Mailtrap::Sending::Client ` to ` Mailtrap::Client ` .
0 commit comments