Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Send emails to your Sandbox for testing purposes:
```ruby
require 'mailtrap'

client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: YOUR_INBOX_ID)
client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, sandbox_id: YOUR_SANDBOX_ID)
client.send(mail)

# You can also pass the request parameters directly
Expand All @@ -139,9 +139,8 @@ 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.
better flexibility in that regard. Go to your _Mailtrap account_ → _Domains_
→ _Your domain_ → _Integration_ to find the SMTP configuration example.

### Multiple Mailtrap Clients

Expand Down Expand Up @@ -175,17 +174,17 @@ Email API:

- Full Email Sending – [`full.rb`](examples/full.rb)
- Batch Sending – [`batch.rb`](examples/batch.rb)
- Sending Domains API – [`sending_domains_api.rb`](examples/sending_domains_api.rb)
- Domains API – [`domains_api.rb`](examples/domains_api.rb)
- Sending Stats API – [`stats_api.rb`](examples/stats_api.rb)
- Email Logs API – [`email_logs_api.rb`](examples/email_logs_api.rb)
- Webhooks API – [`webhooks_api.rb`](examples/webhooks_api.rb)
- Suppressions API – [`suppressions_api.rb`](examples/suppressions_api.rb)
- Tracking Opt-outs API – [`tracking_opt_outs_api.rb`](examples/tracking_opt_outs_api.rb)

Email Sandbox (Testing):
Email Sandbox:

- Projects CRUD – [`projects_api.rb`](examples/projects_api.rb)
- Inboxes CRUD - [`inboxes_api.rb`](examples/inboxes_api.rb)
- Sandboxes CRUD - [`sandboxes_api.rb`](examples/sandboxes_api.rb)
- Sandbox Messages CRUD - [`sandbox_messages_api.rb`](examples/sandbox_messages_api.rb)
- Sandbox Attachments API - [`sandbox_attachments_api.rb`](examples/sandbox_attachments_api.rb)

Expand Down Expand Up @@ -218,6 +217,40 @@ General:
- Action Mailer – [`action_mailer.rb`](examples/action_mailer.rb)
- Verifying webhook signatures – [`webhooks_signature_verification.ru`](examples/webhooks_signature_verification.ru)

## Renamed APIs

Email Sandbox inboxes are now called sandboxes, and sending domains are now called
domains. The old names still work and call the same API endpoints, but they print a
deprecation warning and will be removed in a future major release.

| Deprecated | Use instead |
|------------|-------------|
| `Mailtrap::InboxesAPI` | `Mailtrap::SandboxesAPI` |
| `Mailtrap::Inbox` | `Mailtrap::Sandbox` |
| `Mailtrap::SendingDomainsAPI` | `Mailtrap::DomainsAPI` |
| `Mailtrap::SendingDomain` | `Mailtrap::Domain` |
| `Mailtrap::Client.new(inbox_id:)` and the ActionMailer `inbox_id` setting | `sandbox_id` |
| `#inbox_id` on `Client`, `SandboxMessagesAPI` and `SandboxAttachmentsAPI` | `#sandbox_id` |

The other old names warn once per process. `Mailtrap::Inbox` and `Mailtrap::SendingDomain`
use Ruby's `deprecate_constant`, so they warn only when Ruby's deprecation warnings are
enabled (for example, `ruby -W:deprecated`), and they can warn more than once. Attributes and filters that
mirror API fields keep their names, such as `SandboxMessage#inbox_id`, `Project#inboxes`
and the `sending_domain_ids:` stats filter.

The once-per-process warnings print whether or not Ruby's deprecation warnings are
enabled, so `-W:no-deprecated` doesn't hide them. To silence them, filter out the
warnings tagged `[mailtrap]`:

```ruby
module SilenceMailtrapDeprecations
def warn(message, ...)
super unless message.include?('[mailtrap]')
end
end
Warning.extend(SilenceMailtrapDeprecations)
```

## Migration guide v1 → v2

Change `Mailtrap::Sending::Client` to `Mailtrap::Client`.
Expand Down
4 changes: 2 additions & 2 deletions bin/record-vcr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#
# Usage:
# bin/record-vcr # all specs
# bin/record-vcr spec/mailtrap/sending_domains_api_spec.rb # single file
# bin/record-vcr spec/mailtrap/sending_domains_api_spec.rb:42 # single example
# bin/record-vcr spec/mailtrap/domains_api_spec.rb # single file
# bin/record-vcr spec/mailtrap/domains_api_spec.rb:42 # single example
# bin/record-vcr -e "#update" # filter by name
# bin/record-vcr --org spec/mailtrap/sub_accounts_api_spec.rb # use organization API token
#
Expand Down
2 changes: 1 addition & 1 deletion examples/action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# # Switch to bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
# bulk: true,
# # Switch to sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
# sandbox: true, inbox_id: 12,
# sandbox: true, sandbox_id: 12,
# }

# Now you can use ActionMailer to deliver mail through Mailtrap.
Expand Down
2 changes: 1 addition & 1 deletion examples/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
# client = Mailtrap::Client.new(bulk: true)
# Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
# client = Mailtrap::Client.new(sandbox: true, inbox_id: 12)
# client = Mailtrap::Client.new(sandbox: true, sandbox_id: 12)

# Batch sending with text and html content
batch_base = Mailtrap::Mail.batch_base_from_content(
Expand Down
6 changes: 3 additions & 3 deletions examples/company_info_api.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'mailtrap'

account_id = 3229
sending_domain_id = 1
domain_id = 1
client = Mailtrap::Client.new(api_key: 'your-api-key')
company_info_api = Mailtrap::CompanyInfoAPI.new(account_id, client)

# Create company info for a sending domain
# Create company info for a domain
company_info_api.create(
sending_domain_id,
domain_id,
name: 'Mailtrap',
address: '123 Main St',
city: 'San Francisco',
Expand Down
36 changes: 36 additions & 0 deletions examples/domains_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'mailtrap'

account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
domains = Mailtrap::DomainsAPI.new(account_id, client)

# Create new domain
domain = domains.create(domain_name: 'example.com')
# => #<struct Mailtrap::Domain id=1, domain_name="example.com">

# Get all domains
domains.list
# => [#<struct Mailtrap::Domain id=1, domain_name="example.com">]

# Get domain
domain = domains.get(domain.id)
# => #<struct Mailtrap::Domain id=1, domain_name="example.com">

# Update domain configuration
domains.update(
domain.id,
open_tracking_enabled: true,
click_tracking_enabled: true,
tracking_opt_out_enabled: true,
auto_unsubscribe_link_enabled: false,
inbound_enabled: false
)
# => #<struct Mailtrap::Domain id=1, domain_name="example.com", ...>

# Send setup email
domains.send_setup_instructions(domain.id, email: 'jonathan@mail.com')
# => nil

# Delete domain
domains.delete(domain.id)
# => nil
2 changes: 1 addition & 1 deletion examples/full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# client = Mailtrap::Client.new(api_key: 'your-api-key', bulk: true)

# Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
# client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: 12)
# client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, sandbox_id: 12)

client.send(mail)

Expand Down
39 changes: 0 additions & 39 deletions examples/inboxes_api.rb

This file was deleted.

2 changes: 1 addition & 1 deletion examples/marketing_stats_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# unsubscription_count=12, unsubscription_rate=0.01,
# sent_count=1250>

# Narrow to campaigns only (or automations only), for some sending domains and email service providers
# Narrow to campaigns only (or automations only), for some domains and email service providers
marketing_stats.get(
start_date: today - 29,
end_date: today,
Expand Down
4 changes: 2 additions & 2 deletions examples/sandbox_attachments_api.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'mailtrap'

account_id = 3229
inbox_id = 12
sandbox_id = 12
message_id = 123
client = Mailtrap::Client.new(api_key: 'your-api-key')
sandbox_attachments = Mailtrap::SandboxAttachmentsAPI.new(account_id, inbox_id, message_id, client)
sandbox_attachments = Mailtrap::SandboxAttachmentsAPI.new(account_id, sandbox_id, message_id, client)

# List all attachments for a sandbox message (up to 30)
sandbox_attachments.list
Expand Down
6 changes: 3 additions & 3 deletions examples/sandbox_messages_api.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'mailtrap'

account_id = 3229
inbox_id = 12
sandbox_id = 12
client = Mailtrap::Client.new(api_key: 'your-api-key')
sandbox_messages = Mailtrap::SandboxMessagesAPI.new(account_id, inbox_id, client)
sandbox_messages = Mailtrap::SandboxMessagesAPI.new(account_id, sandbox_id, client)

# List all sandbox messages in the inbox (optional: search, page, last_id)
# List all messages in the sandbox (optional: search, page, last_id)
sandbox_messages.list
# => [#<struct Mailtrap::SandboxMessage id=1, subject="Welcome", ...>]

Expand Down
39 changes: 39 additions & 0 deletions examples/sandboxes_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'mailtrap'

account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
sandboxes = Mailtrap::SandboxesAPI.new(account_id, client)

# Create new sandbox
sandbox = sandboxes.create(name: 'Test Sandbox', project_id: 123_456)
# => #<struct Mailtrap::Sandbox id=1, name="Test Sandbox">

# Get all sandboxes
sandboxes.list
# => [#<struct Mailtrap::Sandbox id=1, name="Test Sandbox">]

# Update sandbox
sandboxes.update(sandbox.id, name: 'Test Sandbox Updated')
# => #<struct Mailtrap::Sandbox id=1, name="Test Sandbox Updated">

# Get sandbox
sandbox = sandboxes.get(sandbox.id)
# => #<struct Mailtrap::Sandbox id=1, name="Test Sandbox Updated">

# Delete all messages (emails) from the sandbox
sandboxes.clean(sandbox.id)

# Mark all messages in the sandbox as read
sandboxes.mark_as_read(sandbox.id)

# Reset SMTP credentials of the sandbox
sandboxes.reset_credentials(sandbox.id)

# Turn the email address of the sandbox on/off
sandboxes.toggle_email_username(sandbox.id)

# Reset username of email address per sandbox
sandboxes.reset_email_username(sandbox.id)

# Delete sandbox
sandboxes.delete(sandbox.id)
36 changes: 0 additions & 36 deletions examples/sending_domains_api.rb

This file was deleted.

2 changes: 1 addition & 1 deletion examples/tracking_opt_outs_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
puts page.data.size
end

# Opt an email out of open and click tracking for a sending domain
# Opt an email out of open and click tracking for a domain
opt_out = tracking_opt_outs.create(email: 'tracked@example.com', domain_id: 12_345)
# => #<struct Mailtrap::TrackingOptOut id="64d71bf3-1276-417b-86e1-8e66f138acfe", ...>

Expand Down
2 changes: 2 additions & 0 deletions lib/mailtrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
require_relative 'mailtrap/tracking_opt_outs_api'
require_relative 'mailtrap/email_campaigns_api'
require_relative 'mailtrap/marketing_stats_api'
require_relative 'mailtrap/domains_api'
require_relative 'mailtrap/sending_domains_api'
require_relative 'mailtrap/company_info_api'
require_relative 'mailtrap/email_logs_api'
require_relative 'mailtrap/projects_api'
require_relative 'mailtrap/sandboxes_api'
require_relative 'mailtrap/inboxes_api'
require_relative 'mailtrap/sandbox_messages_api'
require_relative 'mailtrap/sandbox_attachments_api'
Expand Down
4 changes: 2 additions & 2 deletions lib/mailtrap/account_accesses_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class AccountAccessesAPI

self.response_class = AccountAccess

# Retrieves a list of account accesses with optional filtering by domain, inbox, or project IDs
# Retrieves a list of account accesses with optional filtering by domain, sandbox, or project IDs
# @param domain_ids [Array<Integer>] Optional array of domain IDs to filter by
# @param inbox_ids [Array<Integer>] Optional array of inbox IDs to filter by
# @param inbox_ids [Array<Integer>] Optional array of sandbox IDs to filter by
# @param project_ids [Array<Integer>] Optional array of project IDs to filter by
# @return [Array<AccountAccess>] List of account access objects
# @!macro api_errors
Expand Down
15 changes: 13 additions & 2 deletions lib/mailtrap/action_mailer/delivery_method.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require_relative '../deprecation'

module Mailtrap
module ActionMailer
class DeliveryMethod
attr_accessor :settings

ALLOWED_PARAMS = %i[api_key api_host api_port bulk sandbox inbox_id].freeze
ALLOWED_PARAMS = %i[api_key api_host api_port bulk sandbox sandbox_id].freeze

def initialize(settings)
self.settings = settings
Expand All @@ -22,7 +24,16 @@ def deliver!(message)
private

def client
@client ||= Mailtrap::Client.new(**settings.slice(*ALLOWED_PARAMS))
@client ||= Mailtrap::Client.new(**client_settings)
end

def client_settings
params = settings.slice(*ALLOWED_PARAMS)
inbox_id = settings[:inbox_id]
return params if inbox_id.nil?

Deprecation.warn('The ActionMailer mailtrap_settings inbox_id', 'sandbox_id')
params.merge(sandbox_id: Deprecation.resolve_sandbox_id(params[:sandbox_id], inbox_id))
end
end
end
Expand Down
Loading
Loading