Outbox::Rails
Rails Railtie for sending email, SMS, and push notifications using the Outbox gem. Please view the Outbox documentation to understand the philosophy behind this interface and how to use it.
Installation
Add this line to your application's Gemfile:
gem 'outbox-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install outbox-rails
Usage
Outbox::Notifier uses a very similar interface to ActionMailer.
First, define a notifier in app/notifiers
:
class AccountNotifier < Outbox::Notifier
default email: { from: 'noreply@myapp.com' },
sms: { from: '+15551234567' }
def welcome
# Compose message types using the Outbox::Message interface
email do
subject 'Welcome to our App!'
end
sms do
from '<shortcode_id>'
# The "text" template will automatically be used for the body of the SMS.
# But you can explicitly override by calling the #body method.
body 'Welcome to our App!'
end
# Render the body of the message. This is analogous to ActionMailer::Base#mail,
# but unlike in ActionMailer, #render_message is not required.
render_message
end
end
Send a message using the deliver
method:
# Unlike ActionMailer, deliver takes an argument that defines the recipients
# for the message types you want to send.
AccountNotifier.welcome.deliver email: 'user@gmail.com', sms: '+15557654321'
Variants (New with Rails 4.1)
You can have different templates for each message type using variants. Right now, only the implicit template rendering is supported.
<!-- app/views/account_notifier/welcome.html+email.erb -->
<h1>Welcome!</h1>
<!-- app/views/account_notifier/welcome.text+email.erb -->
Welcome! (email)
<!-- app/views/account_notifier/welcome.text+sms.erb -->
Welcome! (sms)
Configuration
Configure Outbox using the config.outbox
accessor during normal Rails
configuration:
# config/application.rb
module Blog
class Application < Rails::Application
# Configure defautl email fields
config.outbox.email_defaults = {
from: 'from@example.com'
}
# Setup default email settings.
config.outbox.default_email_client_settings = {
smtp_settings: {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true
}
}
end
end
# config/environments/test.rb
Blog::Application.configure do
# Always use test client during tests
config.outbox.use_test_client = true
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request