Preview plain text and html mail templates in your browser without redelivering it every time you make a change.
NOTE: MailView is part of Rails 4.1+. No need for the plugin anymore!
- 4.1.0 API docs: http://api.rubyonrails.org/v4.1.0/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Previewing+emails
- Release notes: http://edgeguides.rubyonrails.org/4_1_release_notes.html#action-mailer-previews
- Pull request: https://github.com/rails/rails/pull/13332/files
Add the gem to your Gemfile:
gem'mail_view',:git=>'https://github.com/basecamp/mail_view.git'# orgem"mail_view","~> 2.0.4"And run bundle install.
Since most emails do something interesting with database data, you'll need to write some scenarios to load messages with fake data. Its similar to writing mailer unit tests but you see a visual representation of the output instead.
# app/mailers/mail_preview.rb or lib/mail_preview.rbclassMailPreview < MailView# Pull data from existing fixturesdefinvitationaccount=Account.firstinviter,invitee=account.users[0,2]Notifier.invitation(inviter,invitee)end# Factory-like patterndefwelcomeuser=User.create!mail=Notifier.welcome(user)user.destroymailend# Stub-likedefforgot_passworduser=Struct.new(:email,:name).new('name@example.com','Jill Smith')mail=UserMailer.forgot_password(user)endendMethods must return a Mail or TMail object. Using ActionMailer, call Notifier.create_action_name(args) to return a compatible TMail object. Now on ActionMailer 3.x, Notifier.action_name(args) will return a Mail object.
A mini router middleware is bundled for Rails 2.x support.
# config/environments/development.rbconfig.middleware.useMailView::Mapper,[MailPreview]For Rails³ you can map the app inline in your routes config.
# config/routes.rbifRails.env.development?mountMailPreview=>'mail_view'endNow just load up http://localhost:3000/mail_view.

