Rails Email Preview

- 1 min read
rails email testing

When working with features that need to send an email, sometimes we find out that testing the email content is a little bit painful. Usually, we will call the function that sends the email and inspect the email content and view it through a development mail server such as mailcatcher or mailtrap. Most of the time we don’t want to go through all processes to only see the email body.

Luckily, Rails has a feature to preview your email mailer without having to use the email server at all. In this tutorial, I will explain how to set up a mailer preview in the Rails framework.

Let’s assume we already have a mailer class like this one.

class UserMailer < ApplicationMailer
  def welcome(user)
    @user = user
    mail(to: user.email, subject: 'Welcome!')
  end
end
Hi <%= @user.full_name %>
<br><br>
Welcome to the club!


In order to enable the email preview for user mailer, we need to create user mailer preview class in test/mailers/previews directory.

💡 if you are using rspec as testing framework, usually the directory is inside the spec folder spec/mailers/previews.

class UserMailerPreview < ActionMailer::Preview
  def welcome
    user = User.first

    UserMailer.welcome(user)
	end
end

Then, you can check the email preview by accessing this url in your local url development. [http://localhost:3000/rails/mailer](http://localhost:3000/rails/mailer) . Make sure your local development server are running.

If everything goes right, you will see the list available email preview like this.

And if you click the welcome email preview it will show the preview of user mailer welcome.

Voila, that’s all. I hope this tutorial will make you more productive when working with a feature with an email.


sources: