Skip to Content
Course content

216: Email Testing with letter_opener

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Why can't I just use a real SMTP server for testing?

You could, but trust me, you don't want to. I've seen far too many developers accidentally send "Test email #42 - please ignore" to a real client because they forgot they were connected to a production-like SMTP relay. Beyond the risk of embarrassment, setting up a real mail server locally is a chore. You have to deal with app passwords, ports, and sometimes your ISP blocking port 25.

letter_opener solves this by completely bypassing the network. Instead of sending the email out into the void, it intercepts the message and opens it as an HTML file in your default web browser. It's fast, it's safe, and it requires zero credentials.

How do I actually get this thing configured?

It's a two-step process. First, you need to add the gem to your Gemfile. Make sure you put it specifically in the :development group. You definitely don't want this gem trying to open browser windows on your production server.

group :development do
  gem 'letter_opener'
end

After you've run bundle install, you have to tell Rails to use it as the delivery method. Head over to config/environments/development.rb and add this line:

config.action_mailer.delivery_method = :letter_opener

I usually put this right near the other mailer settings. Once you restart your server, Rails will stop trying to find an SMTP server and start handing emails off to the gem.

What does the workflow look like when I trigger an email?

Let's say you're building a gear-sharing app called "GearSwap" and you have a WelcomeMailer that sends a greeting when a user signs up. In your controller, you'd have something like this:

def create
  @user = User.new(user_params)
  if @user.save
    WelcomeMailer.with(user: @user).welcome_email.deliver_now
    redirect_to root_path, notice: "Welcome to GearSwap!"
  end
end

The moment deliver_now is called, your browser will automatically open a new tab displaying the email exactly as the recipient would see it. You can check your CSS, verify that the user's name is interpolating correctly, and click your links—all without a single packet leaving your machine. If you're sending a bulk batch of emails, just be careful; letter_opener will try to open a new tab for every single one, which can crash your browser if you're not cautious.




📋 Practical Task

Intercepting GearSwap Welcome Notifications

You have been handed a partially completed "GearSwap" application. The UserMailer is written, but the app is currently crashing in development because it's trying to connect to a non-existent SMTP server.

  • Add the letter_opener gem to the :development group in the Gemfile.
  • Update config/environments/development.rb to use :letter_opener as the delivery method.
  • Restart the server and create a new user via the signup form.
  • Verify that a new browser tab opens automatically displaying the "Welcome to GearSwap" email with the correct user details.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.