Skip to content

Commit b8d70a7

Browse files
committed
update readme
1 parent 022fa3b commit b8d70a7

File tree

1 file changed

+199
-29
lines changed

1 file changed

+199
-29
lines changed

README.md

Lines changed: 199 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,45 @@
22
[![docs](https://shields.io/badge/docs-rubydoc.info-blue)](https://rubydoc.info/gems/mailtrap)
33
[![gem](https://shields.io/gem/v/mailtrap)](https://rubygems.org/gems/mailtrap)
44
[![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)
56

7+
# Mailtrap Ruby client - Official
68

9+
## Prerequisites
710

8-
# Official Mailtrap Ruby client
11+
To get the most out of this official Mailtrap.io Ruby SDK:
912

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
1117

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).
1319

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
1544

1645
## Installation
1746

@@ -23,21 +52,51 @@ gem 'mailtrap'
2352

2453
And then execute:
2554

26-
$ bundle install
55+
```bash
56+
bundle install
57+
```
2758

2859
Or install it yourself as:
2960

30-
$ gem install mailtrap
61+
```bash
62+
gem install mailtrap
63+
```
3164

3265
## Usage
3366

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+
3490
### Ruby on Rails
3591

3692
```ruby
37-
# place this code in config/environments/production.rb:
93+
# config/environments/production.rb
3894
config.action_mailer.delivery_method = :mailtrap
95+
config.action_mailer.mailtrap_settings = {
96+
api_key: 'your-api-key'
97+
}
3998

40-
# then set the MAILTRAP_API_KEY environment variable
99+
# Set the MAILTRAP_API_KEY environment variable
41100
# using your hosting solution.
42101
```
43102

@@ -70,7 +129,49 @@ client.send(
70129
subject: 'You are awesome!',
71130
text: 'Congrats for sending test email with Mailtrap!'
72131
)
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+
{ email: '[email protected]' }
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+
)
73166

167+
client.send(
168+
from: { email: '[email protected]', name: 'Mailtrap Test' },
169+
to: [
170+
{ email: '[email protected]' }
171+
],
172+
subject: 'Test Email',
173+
text: 'This is a test email sent to sandbox inbox'
174+
)
74175
```
75176

76177
### Batch Sending
@@ -111,7 +212,7 @@ client.send_batch(
111212
require 'mailtrap'
112213

113214
client = Mailtrap::Client.new(api_key: 'your-api-key')
114-
templates = Mailtrap::EmailTemplatesAPI.new 3229, client
215+
templates = Mailtrap::EmailTemplatesAPI.new(3229, client)
115216

116217
templates.create(
117218
name: 'Welcome Email',
@@ -122,37 +223,60 @@ templates.create(
122223
)
123224
```
124225

125-
Refer to the [`examples`](examples) folder for more examples:
226+
### Contacts API
126227

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'
132230

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+
)
134245

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+
)
141252

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+
```
146269

147270
### Multiple Mailtrap Clients
148271

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
150273
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:
152275

153276
```ruby
154277
# config/application.rb
155278
ActionMailer::Base.add_delivery_method :mailtrap_bulk, Mailtrap::ActionMailer::DeliveryMethod
279+
ActionMailer::Base.add_delivery_method :mailtrap_sandbox, Mailtrap::ActionMailer::DeliveryMethod
156280

157281
# config/environments/production.rb
158282
config.action_mailer.delivery_method = :mailtrap
@@ -163,11 +287,57 @@ config.action_mailer.mailtrap_bulk_settings = {
163287
api_key: 'your-api-key',
164288
bulk: true
165289
}
290+
config.action_mailer.mailtrap_sandbox_settings = {
291+
api_key: 'your-api-key',
292+
sandbox: true,
293+
inbox_id: 12
294+
}
166295

167296
# 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
169299
```
170300

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+
171341
## Migration guide v1 → v2
172342

173343
Change `Mailtrap::Sending::Client` to `Mailtrap::Client`.

0 commit comments

Comments
 (0)