ASCII Thoughts

Preview all your development emails with Mailcatcher

One of the great features coming in Rails 4.1 is Action Mailer Previews. This will offer a quick and simple way to preview the emails of your application as your develop them.

Unfortunately, not everyone will be able to jump to Rails 4.1 right away, and not everyone uses Rails. Your application might be written in PHP, Java, .Net, etc. Wouldn't it be great if you could easily preview all your emails without having to use Gmail as your SMTP?

Well rejoice my friend: here comes mailcatcher!

What is mailcatcher?

The premise of mailcatcher is really simple: it runs a SMTP server on your machine, on port 1025, and saves all the emails it receives. It also comes with a web interface where you can inspect said emails:

Mailcatcher screenshot

Why is it awesome?

Installation

The mailcatcher site provides a lot more details but the gist of it is really simple. If you have a reasonably recent version of Linux or Mac OS X, you probably have Ruby installed. If not, you will have to install Ruby first.

Once you have Ruby installed:

# If you are using the system's Ruby:
sudo gem install mailcatcher

# If you use rbenv/rvm/etc:
gem install mailcatcher

You can then run mailcatcher with:

mailcatcher     # As a daemon
mailcatcher -f  # In the foreground

If mailcatcher is not in your path, look at your ruby environment:

gem environment
# RubyGems Environment:
# - RUBYGEMS VERSION: 2.2.2
# - RUBY VERSION: 2.1.1 (2014-02-24 patchlevel 76) [x86_64-darwin13.0]
# - INSTALLATION DIRECTORY: /Users/Julien/.gem/ruby/2.1.1
# - RUBY EXECUTABLE: /Users/Julien/.rubies/2.1.1/bin/ruby
# - EXECUTABLE DIRECTORY: /Users/Julien/.gem/ruby/2.1.1/bin
# - SPEC CACHE DIRECTORY: /Users/Julien/.gem/specs

Look in the EXECUTABLE DIRECTORY:

# It should be here
/Users/Julien/.rubies/2.1.1/bin/mailcatcher

Configuring Rails

# In config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { 
  :address => "localhost", :port => 1025 
}

The mailcatcher site provides information for other platforms.

Starting it automatically

You will probably want to automatically start mailcatcher when you start your computer. It's pretty easy on Mac OS X:

# Path to mailcatcher. You might want to try 
# 'which mailcatcher' in your terminal first
# to make sure it returns a result.
# If it doesn't, and you know the path to the
# executable, just set it here.
MAILCATCHER_PATH=`which mailcatcher`

# Create the daemon file
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
  <dict>
    <key>Label</key>
    <string>com.mailcatcher</string>
    <key>ProgramArguments</key>
    <array>
      <string>$MAILCATCHER_PATH</string>
      <string>-f</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/com.mailcatcher.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.mailcatcher.out</string>
  </dict>
</plist>
" > ~/Library/LaunchAgents/com.mailcatcher.plist

You can then start it for the current session with (make sure it's not already running):

launchctl load ~/Library/LaunchAgents/com.mailcatcher.plist

If you run into any trouble, look at the logs:

/tmp/com.mailcatcher.out
/tmp/com.mailcatcher.err

Caveat

If you use rbenv, rvm, etc., you might need a wrapper script for the environment:

#!/bin/bash
export PATH="$PATH:/usr/local/bin"
source /usr/local/share/chruby/chruby.sh
chruby 2.1
mailcatcher -f

Conclusion

Mailcatcher is absolutely awesome and I don't know what I would do without it.

That's it for today. Cheers!