Home

Preview Emails in Rails

Rails 4.1 introduced the ability to preview email messages from your mailers without sending an email. Learn how ...

As of Rails 4.1, you can now preview emails while developing (without sending an email). Let's say you have a mailer that looks like this:

app/mailers/notification_mailer.rb

class NotificationMailer < ActionMailer::Base

def welcome(user)
@user = user
mail :to => user.email, :from => 'me@mydomain.com',
:subject => 'Welcome!'
end

end

To preview it, create a file in test/previews and call the mailer method just as you would from any model or controller in your app.

test/previews/notification_preview.rb

class NotificationPreview < ActionMailer::Preview

def welcome
NotificationMailer.welcome(User.first)
end

end

Then you can preview the email at [/rails/mailers/notification/welcome](http:// rails/mailers/notification/welcome).

Notice the preview route matches the preview class name and then preview method, which doesn't necessarily need to be related to the mailer itself.

Also note the preview filename is appended with _preview.

Let's Connect

Keep Reading

Rails has_many :through Polymorphic Association

How to maintain HMT behavior on a polymorphic association.

Oct 13, 2014

Render an Inline SVG in Rails (or Middleman)

You can avoid multiple requests to your server by rendering SVG images inline to the rest of your HTML.

Feb 11, 2016

How to Transition from CarrierWave to Dragonfly

It can be a process to move away from CarrierWave once you're already using it. Here's a step-by-step process to make it easy to transition to Dragonfly.

Jan 02, 2015