How do I preview emails in Rails?

Ruby on-RailsEmail

Ruby on-Rails Problem Overview

This might be a dumb question but when I'm putting together an HTML email in Rails, is there a particularly easy built-in way to preview the template it in the browser or do I need to write some sort of custom controller that pulls it in as its view?

Ruby on-Rails Solutions

Solution 1 - Ruby on-Rails

Action Mailer now has a built in way of previewing emails in Rails 4.1. For example, check this out:

# located in test/mailers/previews/notifier_mailer_preview.rb

class NotifierPreview < ActionMailer::Preview
  # Accessible from http://localhost:3000/rails/mailers/notifier/welcome
  def welcome

Solution 2 - Ruby on-Rails

Daniel's answer is a good start, but if your email templates contain any dynamic data, it won't work. E.g. suppose your email is an order receipt and within it you print out @order.total_price - using the previous method the @order variable will be nil.

Here's a little recipe I use:

First, since this email preview functionality is definitely for internal use only, I set up some generic routes in the admin namespace:


MySite::Application.routes.draw do
  namespace :admin do
    match 'mailer(/:action(/:id(.:format)))' => 'mailer#:action'

Next, I create the controller. In this controller, I create one method per email template. Since most emails contain dynamic data, we need to populate whatever member variables the template expects.

This could be done with fixtures, but I typically prefer to just grab some pseudo-random real data. Remember - this is NOT a unit test - this is purely a development aid. It doesn't need to produce the same result every single time - in fact - it's probably better if it doesn't!

class Admin::MailerController < Admin::ApplicationController

  def preview_welcome()
    @user = User.last
    render :file => 'mailer/welcome.html.erb', :layout => 'mailer'


Note that when we render the template, we use layout=>:mailer. This embeds the body of your email inside the HTML email layout that you've created instead of inside your typical web application layout (e.g. application.html.erb).

And that's pretty much it. Now I can visit to preview change to my welcome email template.

Solution 3 - Ruby on-Rails

37Signals also has their own mail testing gem called mail_view. It's pretty fantastic.

Solution 4 - Ruby on-Rails

The easiest setup I've seen is MailCatcher. Setup took 2 minutes, and it works for new mailers out of the box.

Solution 5 - Ruby on-Rails

I use email_preview. Give it a try.

Solution 6 - Ruby on-Rails

I'm surprised no one's mentioned letter_opener. It's a gem that will render and open emails as a browser page whenever an email is delivered in dev.

Solution 7 - Ruby on-Rails

I recently wrote a gem named Maily to preview, edit (template file) and deliver the application emails via a browser. It also provides a friendly way to hook data, a flexible authorization system and a minimalist UI.

I have planned to add new features in the near future, like:

  • Multiple hooks per email
  • Parametrize emails via UI (arguments of mailer method)
  • Play with translations keys (list, highlight, ...)

I hope it can help you.

Solution 8 - Ruby on-Rails

Easiest solution in rails 6: just remember one url:


Solution 9 - Ruby on-Rails

rails generates a mail preview if you use rails g mailer CustomMailer. You will get a file CustomMailerPreview inside spec/mailers/previews folder.

Here you can write your method that will call the mailer and it'll generate a preview.

For ex -

class CustomMailerPreview < ActionMailer::Preview
  def contact_us_mail_preview
    CustomMailer.my_mail(user: User.first)

Preview all emails at http://localhost:3000/rails/mailers/custom_mailer

Solution 10 - Ruby on-Rails

You can use Rails Email Preview

rails-email-preview screenshot

REP is a rails engine to preview and test send emails, with I18n support, easy premailer integration, and optional CMS editing with comfortable_mexican_sofa.

Solution 11 - Ruby on-Rails

There is no way to preview it directly out of the Mailer. But as you wrote, you can write a controller, which looks something like this.

class EmailPreviewsControllers < ActionController::Base
  def show
    render "#{params[:mailer]}_mailer/#{params[:method]}"

But I think, that's not the best way to test emails, if they look correctly.

Solution 12 - Ruby on-Rails

Rails Email Preview helps us to quickly view the email in web browser in development mode.

  1. Add “gem ‘rails_email_preview’, ‘~> 0.2.29’ “ to gem file and bundle install.

  2. Run “rails g rails_email_preview:install” this creates initializer in config folder and add routes.

  3. Run “rails g rails_email_preview:update_previews” this crates mailer_previews folder in app directory.

Generator will add a stub to each of your emails, then u populate the stub with mock data.


class UserMailerPreview
def invitation
UserMailer.invitation mock_user(‘Alice’), mock_user(‘Bob’)
def welcome
UserMailer.welcome mock_user
def mock_user(name = ‘Bill Gates’)
fake_id name, email: “user#{rand 100}”)
def fake_id(obj)
obj.define_singleton_method(:id) { 123 + rand(100) }

4) Parameters in search query will be available as an instance variable to preview class. Ex: if we have a URL like “/emails/user_mailer_preview-welcome?user_id=1” @user_id is defined in welcome method of UserMailerPreview it helps us to send mail to specific user.

class UserMailerPreview
def welcome
user = @user_id ? User.find(@user_id) : mock_user

5) To access REP url’s like this


6) We can send emails via REP, this will use environment mailer settings. Uncomment this line in the initializer to disable sending mail in test environment.

config.enable_send_email = false

Source : RailsCarma Blog : Previewing Emails in Rails Applications With the Mail_View Gem

Solution 13 - Ruby on-Rails

I prefer mails_viewer gem. This gem is quite useful as it save the HTML template into tmp folder.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter NixeyView Question on Stackoverflow
Solution 1 - Ruby on-RailsbenjaminjosephwView Answer on Stackoverflow
Solution 2 - Ruby on-RailscailinanneView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMarcView Answer on Stackoverflow
Solution 4 - Ruby on-RailsGalenView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPikachuView Answer on Stackoverflow
Solution 6 - Ruby on-RailsKurtPrestonView Answer on Stackoverflow
Solution 7 - Ruby on-RailsmarketsView Answer on Stackoverflow
Solution 8 - Ruby on-RailsstevecView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSwapsView Answer on Stackoverflow
Solution 10 - Ruby on-RailsglebmView Answer on Stackoverflow
Solution 11 - Ruby on-RailsDaniel SpangenbergView Answer on Stackoverflow
Solution 12 - Ruby on-RailsCarma TecView Answer on Stackoverflow
Solution 13 - Ruby on-RailsTrung LêView Answer on Stackoverflow