MailCatcher: Making working with email in development mode a pleasure

Posted on
Mailcatcher

When developing web applications, working with email has always been a pain. By default in Ruby on Rails, all email messages sent in development via Action Mailer are delivered in test mode. This means if you send an email from your application, the output of that message would display in your development log. While this can show you if the output is correct, it does not indicate if the HTML of the email message is rendered correctly. A way around this would be to connect your development environment to an actual SMTP server. Even though this would allow you to view the email in your mail client of choice, it is a very bad idea, as you could potentially email real people.

Originally to solve this problem, the ruby gem letter_opener was our tool of choice. When an email was sent from the web application, letter_opener would open up the email in a new browser tab. However, in use cases where multiple emails were fired off, multiple browser tabs being opened at once was very obtrusive.

Enter MailCatcher

MailCatcher is a ruby gem that runs a local SMTP server on your machine. Any email message sent to this SMTP server will be captured and not sent out. Also, all messages will be viewable in a web interface available at http://localhost:1080.

MailCatcher

MailCatcher utilizes WebSockets to display email messages immediately after they are sent without a refresh to the browser. If your browser does not support WebSockets, a simple refresh will update the email listing.

Another great feature of MailCatcher is the ability to view both the HTML and plain text versions of emails. This feature alone has saved us some time during testing of our email views.

MailCatcher also lists any attachments within the email for easy downloading.

Getting Started

To get up and running, install the mailcatcher gem:

$ gem install mailcatcher

Next, in your development configuration file config/development.rb, set the following Action Mailer settings:

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

Finally, start the mailcatcher daemon in your terminal:

$ mailcatcher
Starting MailCatcher
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

Further Reading

002

This post is by Kevin Faustino. Kevin is the Chief Craftsman of Remarkable Labs and also the founder of the Toronto Ruby Brigade.


Comments

comments powered by Disqus