The MailCatcher gem makes debugging your email code incredibly easy. This article will show you how to quickly and easily add MailCatcher to your Rails Application.

First, let's add MailCatcher to our gemfile.

Gemfile:

gem 'mailcatcher', '~> 0.5.12'

Now run a bundle install.

Terminal Commands:

bundle install

Now, lets start the mailcatcher daemon. Mailcatcher runs in the foreground, so make sure to open a new terminal window, then run the command below to start it.

Terminal Commands:

bundle exec mailcatcher

You should see some information saying that mailcatcher is starting and a couple of addresses should be listed. Visit the http address listed in the terminal window.. This is typically http://127.0.0.1:1080. A web interface should come up. There isn't anything to see yet, we need to send our email to the SMTP server that mailcatcher emulates.

Now, lets create a mailer to use in this example. Run the following command to create a new hello world mailer.

Terminal Commands:

rails g mailer HelloWorld hello

Okay, now open up the hello world's only view and add the following code.

app/views/hello_world/hello.text.erb:

Hello World!

Great, now lets create a simple controller that will send the email. Run the command below to create a hello controller.

Terminal Commands:

rails g controller Hello show

Great, now open up the hello controller and modify it so that it looks like the code listed below. This will send out an email every time you visit the page.

app/controllers/hello_controller.rb:

class HelloController < ApplicationController
  def show
    HelloWorld.hello.deliver
  end
end

One final step, open up your config/environments/development.rb and add the following lines underneath the line that says config.assets.debug = true.

config/environments/development.rb:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }

Great, now, start a rails server and navigate to http://localhost:3000/hello/show your app will attempt to send out an email, which MailCatcher will catch for display. After that simply visit http://127.0.0.1:1080 to view the email that was sent. This works with any email, html or otherwise. That's it! Thanks for reading!