Everyday Rails

Better SMTP handling in Rails application development

By Aaron Sumner, May 26, 2011.

When adding mailers during Rails application development, you don’t need to connect to productional SMTP servers or use every GMail, Hotmail, and Yahoo! address you’ve ever created to make sure your mailers’ views are rendering the way you’d like. Here are two very handy SMTP development utilities you can have up and running in minutes.

Mailcatcher

MailCatcher is a Ruby gem from Samuel Cochran that fires up a basic SMTP server for development purposes, and a nice little web interface for looking at the results. Install the gem as you normally would:

  $ gem install mailcatcher

Then fire up the server:

  $mailcatcher
  Starting MailCatcher
  ==> smtp://127.0.0.1:1025
  ==> http://127.0.0.1:1080

To configure your app to use MailCatcher in development, you’ll need to add your SMTP details (served from localhost, port 1025). For simplicity’s sake I’m just adding this to my config/environments/development.rb file here; when implementing in real apps I’ve created an initializer that configures my SMTP settings depending on environment (MailCatcher in development; generally GMail in production).

  # config/environments/development.rb
  
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    :address => "localhost",
    :port => 1025,
    :domain => "everydayrails.com" }

Now, whenever you trigger a mailer, the message will be caught by MailCatcher. Fire up the web console at http://127.0.0.1:1080 to have a look at the message in HTML, plain text, or raw source.

MockSMTP

If you’re a Mac-based Rails developer, and you have about $30 to throw at a development SMTP server, check out MockSMTP (also available in the Mac App Store). MockSMTP does the same thing as DreamCatcher, except via a native Mac interface. Fire up MockSMTP and configure your Rails application the exact same way.

For most projects I would imagine that DreamCatcher is just as good, but you may have cases in which you’d want to separate outgoing mail by sender—MockSMTP’s interface makes this quite easy. You can also tell MockSMTP to deliver messages once you’ve looked at them, which can be handy if you want to check how a message looks in specific mail clients.

What about Windows?

Windows-based Rails developers might want to check out Papercut, or search around. I know nothing about these apps myself.

What if I do just want to send out mail?

If you want to use your regular SMTP server during development, I recommend referring to the Railscast on ActionMailer in Rails 3. In particular, watch the part on setting up a mail interceptor. Doing so will catch outgoing messages and deliver them all to a single inbox of your choosing, so you don’t have to check multiple accounts for your mailers’ deliveries.

Discussion

Follow along on on Mastodon, Facebook, or Bluesky to keep up-to-date with my latest posts. Better yet, subscribe to my newsletter for updates from Everyday Rails, book picks, and other thoughts and ideas that didn't quite fit here.
Buy Me A Coffee

Test with confidence!

If you liked my series on practical advice for adding reliable tests to your Rails apps, check out the expanded ebook version. Lots of additional, exclusive content and a complete sample Rails application.

Newsletter

Ruby on Rails news and tips, and other ideas and surprises from Aaron at Everyday Rails. Delivered to your inbox on no particular set schedule.