This Ruby gem offers integration with the official API for Mailtrap.
Quickly add email sending functionality to your Ruby application with Mailtrap.
(This client uses API v2, for v1 refer to this documentation)
Add this line to your application's Gemfile:
gem 'mailtrap'And then execute:
$ bundle install
Or install it yourself as:
$ gem install mailtrap
# place this code in config/environments/production.rb:
config.action_mailer.delivery_method = :mailtrap
# then set the MAILTRAP_API_KEY environment variable
# using your hosting solution.require 'mailtrap'
# Create mail object
mail = Mailtrap::Mail.from_content(
from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
to: [
{ email: 'your@email.com' }
],
reply_to: { email: 'support@example.com', name: 'Mailtrap Reply-To' },
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!'
)
# Create client and send
client = Mailtrap::Client.new(api_key: 'your-api-key')
client.send(mail)
# You can also pass the request parameters directly
client.send(
from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
to: [
{ email: 'your@email.com' }
],
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!'
)Send up to 500 emails in one API call:
require 'mailtrap'
client = Mailtrap::Client.new(api_key: 'your-api-key')
batch_base = Mailtrap::Mail.batch_base_from_content(
from: { email: 'mailtrap@demomailtrap.co', name: 'Mailtrap Test' },
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!',
html: '<p>Congrats for sending test email with Mailtrap!</p>'
)
client.send_batch(
batch_base, [
Mailtrap::Mail.from_content(
to: [
{ email: 'john.doe@email.com', name: 'John Doe' }
]
),
Mailtrap::Mail.from_content(
to: [
{ email: 'jane.doe@email.com', name: 'Jane Doe' }
]
)
]
)require 'mailtrap'
client = Mailtrap::Client.new(api_key: 'your-api-key')
templates = Mailtrap::EmailTemplatesAPI.new 3229, client
templates.create(
name: 'Welcome Email',
subject: 'Welcome to Mailtrap!',
body_html: '<h1>Hello</h1>',
body_text: 'Hello',
category: 'welcome'
)Refer to the examples folder for more examples:
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).
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.
You can configure two 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:
# config/application.rb
ActionMailer::Base.add_delivery_method :mailtrap_bulk, Mailtrap::ActionMailer::DeliveryMethod
# config/environments/production.rb
config.action_mailer.delivery_method = :mailtrap
config.action_mailer.mailtrap_settings = {
api_key: 'your-api-key'
}
config.action_mailer.mailtrap_bulk_settings = {
api_key: 'your-api-key',
bulk: true
}
# app/mailers/foo_mailer.rb
mail(delivery_method: :mailtrap_bulk)Change Mailtrap::Sending::Client to Mailtrap::Client.
If you use classes which have Sending namespace, remove the namespace like in the example above.
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.