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?
13 Answers
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
Notifier.welcome(User.first)
end
end
-
6@andres Pretty sure that's by design--you don't want users to preview your mail on production servers, it's meant only for local development.– mpoweredJan 24, 2015 at 5:45
-
1@mpowered And what if I DO want users to be able to preview/modify mails they are about to send ? (Ex : a manager refuses a user application, and an email is sent to warn the applicant) Feb 20, 2015 at 20:51
-
2@CyrilDD You'd have to build something like that yourself, I don't think this would be the tool for that job. Feb 21, 2015 at 0:03
-
3The preview is available in localhost:3000/rails/mailers/notifier/welcome, and a list of them in localhost:3000/rails/mailers.– ziresMar 7, 2016 at 10:33
-
12If you're using Rspec, your previews go in
spec/mailers/previews
Jan 18, 2017 at 19:52
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:
#routes.rb
MySite::Application.routes.draw do
namespace :admin do
match 'mailer(/:action(/:id(.:format)))' => 'mailer#:action'
end
end
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!
#app/controllers/admin/mailer_controller.rb
class Admin::MailerController < Admin::ApplicationController
def preview_welcome()
@user = User.last
render :file => 'mailer/welcome.html.erb', :layout => 'mailer'
end
end
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://example.com/admin/mailer/preview_welcome to preview change to my welcome email template.
-
This was in fact exactly the approach I had taken and just coded as I started to read your answer - thank you :) Aug 24, 2011 at 13:27
-
-
-
-
6
37Signals also has their own mail testing gem called mail_view. It's pretty fantastic.
-
-
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 May 7, 2012 at 20:48
The easiest setup I've seen is MailCatcher. Setup took 2 minutes, and it works for new mailers out of the box.
-
This indeed is great. Setup is very fast and works for all mailers! Great tip!– reptFeb 8, 2016 at 23:41
Easiest solution in rails 5, rails 6, and rails 7: just remember one url:
-
1This also works under Rails 5. This method uses ActionMailer::Preview. More information is available in Rails Guides : guides.rubyonrails.org/… yesterday
-
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.
-
3letter_opener is a cool gem and it's a must have. But sometimes the mail start from special events that you want to avoid playing to check purely the html/css of the mail.– MauroJun 30, 2017 at 7:22
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.
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)
end
end
Preview all emails at http://localhost:3000/rails/mailers/custom_mailer
-
For my app with rspec I added
config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"
– MauroJun 30, 2017 at 8:29
You can use Rails Email Preview
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.
-
Are there any samples on how to actually do the integration betwen REP and CMS? Oct 5, 2014 at 15:46
-
I don't know of any demo applications, but there is a comprehensive article in the wiki– glebmOct 6, 2014 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]}"
end
end
But I think, that's not the best way to test emails, if they look correctly.
-
4
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.– dexterAug 23, 2011 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 Aug 23, 2011 at 17:56
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.
Ex:
class UserMailerPreview
def invitation
UserMailer.invitation mock_user(‘Alice’), mock_user(‘Bob’)
end
def welcome
UserMailer.welcome mock_user
end
private
def mock_user(name = ‘Bill Gates’)
fake_id User.new(name: name, email: “user#{rand 100}@test.com”)
end
def fake_id(obj)
obj.define_singleton_method(:id) { 123 + rand(100) }
obj
end
end
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
UserMailer.welcome(user)
end
end
5) To access REP url’s like this
rails_email_preview.rep_root_url
rails_email_preview.rep_emails_url
rails_email_preview.rep_email_url(‘user_mailer-welcome’)
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
I prefer mails_viewer gem. This gem is quite useful as it save the HTML template into tmp folder.