Take the 2-minute tour ×

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?

share|improve this question

9 Answers 9

up vote 30 down vote accepted

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
share|improve this answer
Hello, in production environment send error with route when i try open url ERROR ActionController::RoutingError: No route matches [GET] In test environment i don't have problem. –  andres Dec 17 at 17:23

The previous 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 http://mysite.com/admin/mailer/preview_welcome to preview change to my welcome email template.

share|improve this answer
This was in fact exactly the approach I had taken and just coded as I started to read your answer - thank you :) –  Peter Nixey Aug 24 '11 at 13:27
Great answer! Exactly what I needed! –  Kulgar Mar 26 '12 at 13:05
Now that's a tasty recipe! Thanks for the great answer! –  Rob Cooper May 7 '12 at 19:38
This is an awesome answer! Thank you! –  OzBandit Jul 5 '12 at 6:50
The route should be get 'mailer... now instead of match. –  patrickberkeley Feb 5 at 22:24

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

share|improve this answer
Brilliant- thank you –  Peter Nixey May 3 '12 at 14:17
No problem. What way did you end up using? –  Marc May 3 '12 at 15:28
Bac when I posted this I solved the problem with a custom controller I wrote myself but am going to hit it again soon so will be experimenting with this –  Peter Nixey May 7 '12 at 20:48
I just tried mail_view- it's awesome –  Yarin Nov 8 '13 at 23:41

I use email_preview. Give it a try.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
Are there any samples on how to actually do the integration betwen REP and CMS? –  danieldekay Oct 5 at 15:46
I don't know of any demo applications, but there is a comprehensive article in the wiki –  glebm Oct 6 at 11:37

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.

share|improve this answer
render "#{params[:mailer]}_mailer/#{params[:method]}", :layout => 'mailer' will embed the body of the email inside the HTML email layout you have created for your app. –  dexter Aug 23 '11 at 17:48
Thanks for that - I'm not wanting to double-check browser compatibility, I would treat that separately, I just simply want to spit the thing out quickly and see how it looks - I think your approach works well for that –  Peter Nixey Aug 23 '11 at 17:56

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

share|improve this answer

I recently wrote a gem called Maily to preview and edit (template file) the application emails in the browser. I have planned to add new features in the near future, like:

  • Play with translations keys
  • Render emails with different parameters (arguments of mailer method)

I hope it can help you.

share|improve this answer

Your Answer


