Mailer preview rails

The joys of testing email previews in Rails!

In Rails, you can use the mail_preview gem to preview emails sent by your application. Here's a step-by-step guide on how to set it up:

Step 1: Add the mail_preview gem to your Gemfile

Add the following line to your Gemfile:

gem 'mail_preview', '~> 1.0'

Then, run bundle install to install the gem.

Step 2: Configure mail_preview

In your config/environments/development.rb file, add the following configuration:

config.action_mailer.delivery_method = :mail_preview

This tells Rails to use the mail_preview delivery method instead of the default smtp method.

Step 3: Create a mailer preview

Create a new file in your spec/mailers directory, e.g., user_mailer_preview.rb. This file will contain the mailer preview code.

In this file, define a mailer preview method that sends an email using the mail method:

# spec/mailers/user_mailer_preview.rb
class UserMailerPreview < ActionMailer::Preview
  def welcome_email
    UserMailer.welcome_email(User.first)
  end
end

In this example, we're defining a welcome_email preview method that sends a welcome email to the first user in the database.

Step 4: Run the mailer preview

To run the mailer preview, use the following command in your terminal:

rails mailer:preview

This will render the email preview in your default email client.

Tips and Variations