OhMySMTP::Rails
OhMySMTP lets you send transactional emails from your app over an easy to use API.
The OhMySMTP Rails Gem is a plug in for ActionMailer to send emails via OhMySMTP to make sending emails from Rails apps super simple.
New in 0.3.0: The ability to consume inbound emails from OhMySMTP via ActionMailbox
Usage
Once installed and configured, continue to send emails using ActionMailer and receive emails with ActionMailbox like normal.
Other Requirements
You will need an OhMySMTP account with a verified domain and organization with an active plan.
Installation
Account Setup
Set up an account at OhMySMTP and complete the Onboarding steps
Gem Installation
Add this line to your application's Gemfile:
gem 'ohmysmtp-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ohmysmtp-rails
Configure the Gem
First you will need to retrieve your API token for your sending domain from OhMySMTP. You can find it under Organization -> Domain -> API Tokens.
Use the encrypted secret management to save your API Token to config/credentials.yml.enc
by running the following:
rails secret
rails credentials:edit
Then add your token:
ohmysmtp_api_token: "TOKEN_GOES_HERE"
Set OhMySMTP as your mail delivery method in config/application.rb
:
config.action_mailer.delivery_method = :ohmysmtp
config.action_mailer.ohmysmtp_settings = { api_token: Rails.application.credentials.ohmysmtp_api_token }
Tagging
You can tag messages and filter them later in the OhMySMTP UI. To do this, pass the tags as a header by adding a tag variable to your mail
method call.
class TestMailer < ApplicationMailer
default from: '[email protected]',
to: '[email protected]'
def single_tag
mail(
tags: 'test tag' # One tag
)
end
def multi_tag
mail(
tags: "['test tag', 'another-tag']" # Multiple tags
)
end
end
Note that this should always be a string, even if using an array of multiple tags.
List-Unsubscribe
To add a List-Unsubscribe header, pass a list_unsubscribe
string to the mail
function:
class TestMailer < ApplicationMailer
default from: '[email protected]',
to: '[email protected]'
def list_unsub_header
mail(
list_unsubscribe: 'https://listunsublink.com'
)
end
end
ActionMailbox (for receiving inbound emails)
As of v0.3.0, this Gem supports handling Inbound Emails (see https://docs.ohmysmtp.com/guide/inbound/ for more details) via ActionMailbox. To set this up:
- Tell Action Mailbox to accept emails from OhMySMTP in
config/environments/production.rb
config.action_mailbox.ingress = :ohmysmtp
- Generate a strong password that Action Mailbox can use to authenticate requests to the OhMySMTP ingress.
Use
bin/rails credentials:edit
to add the password to your application's encrypted credentials underaction_mailbox.ingress_password
, where Action Mailbox will automatically find it:
action_mailbox:
ingress_password: ...
Alternatively, provide the password in the RAILS_INBOUND_EMAIL_PASSWORD
environment variable.
- Configure OhMySMTP to forward inbound emails to
/rails/action_mailbox/ohmysmtp/inbound_emails
with the usernameactionmailbox
and the password you previously generated. If your application lived athttps://example.com
you would configure your OhMySMTP inbound endpoint URL with the following fully-qualified URL:
https://actionmailbox:[email protected]/rails/action_mailbox/ohmysmtp/inbound_emails
That's it! Emails should start flowing into your app just like magic.
Support
For support please check the OhMySMTP Documentation or contact us at [email protected]
Contributing
Please ensure to add a test for any change you make. To run the tests:
bin/test
Pull requests always welcome
License
The gem is available as open source under the terms of the MIT License.