Rails-specific features for Courrier, the API-powered email delivery gem for Ruby.
Sponsored By Rails Designer
Add to your Gemfile:
bundle add rails_courrierGenerate the initializer:
bin/rails generate courrier:install
# Or pass a provider to pre-fill provider-specific config:
bin/rails generate courrier:install --provider=mailpaceThis creates config/initializers/courrier.rb.
Generate an email:
bin/rails generate courrier:email OrderThis creates app/emails/order_email.rb.
Mount the engine to browse inbox previews in your browser:
# config/routes.rb
mount Courrier::Engine => "/courrier"OrderEmail.deliver to: "[email protected]"OrderEmail.deliver_later to: "[email protected]"Configure queue options in the email class:
class OrderEmail < Courrier::Email
enqueue queue: "emails", wait: 5.minutes
def subject = "Your order is ready!"
# …
endPreview emails in your browser:
# config/initializers/courrier.rb
Courrier.configure do |config|
config.email = { provider: "inbox", auto_open: true }
endThe auto_open option opens each sent email in your default browser automatically.
Clear inbox files:
bin/rails courrier:clearRails Courrier automatically includes Rails URL helpers in your email classes:
class OrderEmail < Courrier::Email
def text
"View your order: #{order_url(token: "abc123")}"
end
endRails Courrier publishes delivery lifecycle events via ActiveSupport::Notifications:
ActiveSupport::Notifications.subscribe("delivery.courrier") do |event|
Rails.logger.info "[Courrier] #{event.payload[:email]} delivered in #{event.duration}ms"
end| Event | When it fires | Payload |
|---|---|---|
delivery.courrier |
Email delivered successfully | email, options |
delivery_failed.courrier |
Delivery raised an exception | email, options, exception |
Use the t helper in email classes and ERB templates, scoped under courrier.email.<class_name>:
# config/locales/courrier/en.yml
en:
courrier:
email:
order_email:
subject: "Your order is ready!"
text:
greeting: "Hello %{name}, your order is ready."class OrderEmail < Courrier::Email
def subject = t(".subject")
def text = t(".text.greeting", name: options.to)
endFull translation keys (e.g. t("shared.greeting")) pass through unscoped.
See the Courrier README for full configuration, providers, layouts, templates and more.
This project uses Standard for formatting Ruby code. Please make sure to run rake before submitting pull requests.
Courrier is released under the MIT License.
