-
Notifications
You must be signed in to change notification settings - Fork 7
update readme #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yanchuk
wants to merge
4
commits into
main
Choose a base branch
from
readme-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
update readme #66
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,48 @@ | |
[](https://rubydoc.info/gems/mailtrap) | ||
[](https://rubygems.org/gems/mailtrap) | ||
[](https://rubygems.org/gems/mailtrap) | ||
[](https://opensource.org/licenses/MIT) | ||
|
||
# Official Mailtrap Ruby client | ||
|
||
## Prerequisites | ||
|
||
# Official Mailtrap Ruby client | ||
To get the most out of this official Mailtrap.io Ruby SDK: | ||
|
||
* [Create a Mailtrap account](https://mailtrap.io/signup) | ||
* [Verify your domain](https://mailtrap.io/sending/domains) | ||
|
||
## Supported functionality | ||
|
||
This Ruby gem offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io). | ||
|
||
Quickly add email sending functionality to your Ruby application with Mailtrap. | ||
|
||
(This client uses API v2, for v1 refer to [this documentation](https://mailtrap.docs.apiary.io/)) | ||
|
||
Currently, with this SDK you can: | ||
|
||
* **Email API/SMTP** | ||
* Send an email (Transactional and Bulk streams) | ||
* Send an email with a template | ||
* Send a batch of emails (Transactional and Bulk streams) | ||
* **Email Sandbox (Testing)** | ||
* Send an email | ||
* Send an email with a template | ||
* Message management | ||
* Inbox management | ||
* Project management | ||
* **Contact management** | ||
* Contacts CRUD | ||
* Lists CRUD | ||
* Contact fields CRUD | ||
* **General** | ||
* Templates CRUD | ||
* Suppressions management (find and delete) | ||
* Account access management | ||
* Permissions management | ||
* List accounts you have access to | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
@@ -31,14 +62,47 @@ Or install it yourself as: | |
|
||
## Usage | ||
|
||
### Basic Setup | ||
|
||
Before you start sending emails, you'll need: | ||
- A [Mailtrap account](https://mailtrap.io/signup) | ||
- A [verified domain](https://mailtrap.io/sending/domains) | ||
- An API key from your Mailtrap dashboard | ||
|
||
Set your API key as an environment variable: | ||
```bash | ||
export MAILTRAP_API_KEY="your-api-key" | ||
``` | ||
|
||
### Minimal | ||
|
||
```ruby | ||
require 'mailtrap' | ||
|
||
# For this example to work, you need to set up a sending domain, | ||
# and obtain a token that is authorized to send from the domain. | ||
|
||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
|
||
client.send( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [{ email: '[email protected]' }], | ||
subject: 'Hello from Mailtrap!', | ||
text: 'Welcome to Mailtrap Sending!' | ||
) | ||
``` | ||
|
||
### Ruby on Rails | ||
|
||
```ruby | ||
# place this code in config/environments/production.rb: | ||
# config/environments/production.rb | ||
config.action_mailer.delivery_method = :mailtrap | ||
|
||
# then set the MAILTRAP_API_KEY environment variable | ||
# using your hosting solution. | ||
# Set the MAILTRAP_API_KEY environment variable | ||
# using your hosting solution, or customize the settings: | ||
config.action_mailer.mailtrap_settings = { | ||
api_key: ENV.fetch('MAILTRAP_API_KEY') | ||
} | ||
``` | ||
|
||
### Pure Ruby | ||
|
@@ -70,7 +134,49 @@ client.send( | |
subject: 'You are awesome!', | ||
text: 'Congrats for sending test email with Mailtrap!' | ||
) | ||
``` | ||
|
||
### Send Email Using Template | ||
|
||
```ruby | ||
require 'mailtrap' | ||
|
||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
|
||
mail = Mailtrap::Mail.from_template( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
], | ||
template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2', | ||
template_variables: { | ||
'user_name' => 'John Doe' | ||
} | ||
) | ||
|
||
client.send(mail) | ||
``` | ||
|
||
### Email Sandbox (Testing) | ||
|
||
```ruby | ||
require 'mailtrap' | ||
|
||
# Send to sandbox inbox for testing | ||
client = Mailtrap::Client.new( | ||
api_key: 'your-api-key', | ||
sandbox: true, | ||
inbox_id: 12 | ||
) | ||
|
||
client.send( | ||
from: { email: '[email protected]', name: 'Mailtrap Test' }, | ||
to: [ | ||
{ email: '[email protected]' } | ||
], | ||
subject: 'Test Email', | ||
text: 'This is a test email sent to sandbox inbox' | ||
) | ||
``` | ||
|
||
### Batch Sending | ||
|
@@ -111,7 +217,7 @@ client.send_batch( | |
require 'mailtrap' | ||
|
||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
templates = Mailtrap::EmailTemplatesAPI.new 3229, client | ||
templates = Mailtrap::EmailTemplatesAPI.new(3229, client) | ||
|
||
templates.create( | ||
name: 'Welcome Email', | ||
|
@@ -120,54 +226,135 @@ templates.create( | |
body_text: 'Hello', | ||
category: 'welcome' | ||
) | ||
|
||
# Get all templates | ||
templates.list | ||
|
||
# Get a specific template | ||
templates.get(email_template.id) | ||
|
||
# Update a template | ||
templates.update(email_template.id, name: 'Welcome Updated') | ||
|
||
# Delete a template | ||
templates.delete(email_template.id) | ||
``` | ||
|
||
Refer to the [`examples`](examples) folder for more examples: | ||
### Contacts API | ||
|
||
- [Full](examples/full.rb) | ||
- [Email template](examples/email_template.rb) | ||
- [Batch Sending](examples/batch.rb) | ||
- [ActionMailer](examples/action_mailer.rb) | ||
- [Email Templates API](examples/email_templates_api.rb) | ||
```ruby | ||
require 'mailtrap' | ||
|
||
### Content-Transfer-Encoding | ||
client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
contacts = Mailtrap::ContactsAPI.new(3229, client) | ||
contact_lists = Mailtrap::ContactListsAPI.new(3229, client) | ||
contact_fields = Mailtrap::ContactFieldsAPI.new(3229, client) | ||
|
||
# Create contact list | ||
list = contact_lists.create(name: 'Test List') | ||
|
||
# Create contact field | ||
field = contact_fields.create( | ||
name: 'Nickname', | ||
data_type: 'text', | ||
merge_tag: 'nickname' | ||
) | ||
|
||
`mailtrap` gem uses Mailtrap API to send emails. Mailtrap API does not try to | ||
replicate SMTP. That is why you should expect some limitations when it comes to | ||
sending. For example, `/api/send` endpoint ignores `Content-Transfer-Encoding` | ||
(see `headers` in the [API documentation](https://railsware.stoplight.io/docs/mailtrap-api-docs/67f1d70aeb62c-send-email)). | ||
Meaning your recipients will receive emails only in the default encoding which | ||
is `quoted-printable`, if you send with Mailtrap API. | ||
# Create contact | ||
contact = contacts.create( | ||
email: 'test@example.com', | ||
fields: { field.merge_tag => 'John Doe' }, | ||
list_ids: [list.id] | ||
) | ||
|
||
For those who need to use `7bit` or any other encoding, SMTP provides | ||
better flexibility in that regard. Go to your _Mailtrap account_ → _Email Sending_ | ||
→ _Sending Domains_ → _Your domain_ → _SMTP/API Settings_ to find the SMTP | ||
configuration example. | ||
# Get contact | ||
contact = contacts.get(contact.id) | ||
|
||
# Update contact | ||
contacts.upsert( | ||
contact.id, | ||
email: '[email protected]', | ||
fields: { field.merge_tag => 'Jane Doe' } | ||
) | ||
|
||
# List contacts | ||
contacts.list | ||
|
||
# Delete contact | ||
contacts.delete(contact.id) | ||
``` | ||
|
||
### Multiple Mailtrap Clients | ||
|
||
You can configure two Mailtrap clients to operate simultaneously. This setup is | ||
You can configure multiple Mailtrap clients to operate simultaneously. This setup is | ||
particularly useful when you need to send emails using both the transactional | ||
and bulk APIs. Refer to the configuration example below: | ||
and bulk APIs, or when using sandbox for testing: | ||
|
||
```ruby | ||
# config/application.rb | ||
ActionMailer::Base.add_delivery_method :mailtrap_bulk, Mailtrap::ActionMailer::DeliveryMethod | ||
ActionMailer::Base.add_delivery_method :mailtrap_sandbox, Mailtrap::ActionMailer::DeliveryMethod | ||
|
||
# config/environments/production.rb | ||
config.action_mailer.delivery_method = :mailtrap | ||
config.action_mailer.mailtrap_settings = { | ||
api_key: 'your-api-key' | ||
api_key: ENV.fetch('MAILTRAP_API_KEY') | ||
} | ||
config.action_mailer.mailtrap_bulk_settings = { | ||
api_key: 'your-api-key', | ||
api_key: ENV.fetch('MAILTRAP_API_KEY'), | ||
bulk: true | ||
} | ||
config.action_mailer.mailtrap_sandbox_settings = { | ||
api_key: ENV.fetch('MAILTRAP_API_KEY'), | ||
sandbox: true, | ||
inbox_id: 12 | ||
} | ||
|
||
# app/mailers/foo_mailer.rb | ||
mail(delivery_method: :mailtrap_bulk) | ||
mail(delivery_method: :mailtrap_bulk) # For bulk sending | ||
mail(delivery_method: :mailtrap_sandbox) # For sandbox testing | ||
``` | ||
|
||
## Examples | ||
|
||
Refer to the [`examples`](examples) folder for the source code of this and other advanced examples: | ||
|
||
### Contacts API | ||
|
||
* [Contacts](examples/contacts_api.rb) | ||
|
||
### Sending API | ||
|
||
* [Full](examples/full.rb) | ||
* [Email template](examples/email_template.rb) | ||
* [ActionMailer](examples/action_mailer.rb) | ||
|
||
### Batch Sending API | ||
|
||
* [Batch Sending](examples/batch.rb) | ||
|
||
### Templates API | ||
|
||
* [Email Templates API](examples/email_templates_api.rb) | ||
|
||
### Email Sandbox (Testing) API | ||
|
||
* [Sandbox examples in Full](examples/full.rb) | ||
|
||
## Content-Transfer-Encoding | ||
|
||
`mailtrap` gem uses Mailtrap API to send emails. Mailtrap API does not try to | ||
replicate SMTP. That is why you should expect some limitations when it comes to | ||
sending. For example, `/api/send` endpoint ignores `Content-Transfer-Encoding` | ||
(see `headers` in the [API documentation](https://railsware.stoplight.io/docs/mailtrap-api-docs/67f1d70aeb62c-send-email)). | ||
Meaning your recipients will receive emails only in the default encoding which | ||
is `quoted-printable`, if you send with Mailtrap API. | ||
|
||
For those who need to use `7bit` or any other encoding, SMTP provides | ||
better flexibility in that regard. Go to your _Mailtrap account_ → _Email Sending_ | ||
→ _Sending Domains_ → _Your domain_ → _SMTP/API Settings_ to find the SMTP | ||
configuration example. | ||
|
||
## Migration guide v1 → v2 | ||
|
||
Change `Mailtrap::Sending::Client` to `Mailtrap::Client`. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're asking to create an env variable, and then we don't use it in the "Minimal" example below 🤷♂️ . I would remove this section about env variable, or actually use it below so it's consistent. But I would make it clear that env variable is just one of the options - the library doesn't require it to be set if you provide the api-key by other means.