Mike Rogers on a Snowmobile

Mike Rogers

Web Developer

Mailer Previews in Rails 5

Posted on

One of the best features of Ruby on Rails when developing locally is the Preview Email feature, along with premailer it really makes building out mailers super easy.

However, I like to enable mailer previews in my apps staging environments. Primarily this is so clients can confidently sign off mailers and if required, easily communicate amends.

To achieve this I use the following snippet of code in config/environments/production.rb:

# Enable preview mailers
# Add the ENABLE_MAILER_PREVIEWS to your enviroment to enable.
if ENV['ENABLE_MAILER_PREVIEWS'].present?
  config.action_mailer.show_previews = true

  # If you're using RSpec make sure to add the link changing where the previews path is.
  config.action_mailer.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/spec/mailers/previews" : nil
end

Then I just set ENABLE_MAILER_PREVIEWS to true in my Heroku Apps Config Variables and I'm good.

This was written by Mike Rogers, a freelance Ruby on Rails developer based in London.

Share the ♥ by sharing this!

If you want to discuss this post, feel free to tweet me (@MikeRogers0) or drop me an email. Any code samples unless stated otherwise are licensed under the The MIT License (MIT). Spotted a mistake? Send me a pull request :)