Most Rails Apps send emails of some sort. Users receive welcome emails, passwort reset emails, order confirmation or other transactional mails and marketing emails of differnet flavours. Sending Emails often requires thorough manual testing with different email clients to make sure they look like they are supposed to. And while testing we must to be extra sure we’re not accidentally sending emails to real people.

Rails supports a number of different ways of dealing with emails out of the box. You can configure ActionMailer to deliver Emails to:

  • a local sendmail server
  • an SMTP server
  • a local file
  • nowhere (ignore the email)

Other popular third party tools are the letter_opener gem by Ryan Bates which opens any email in the browser as they are sent and mailcatcher, which runs a local SMTP server and mail viewer web-app.

While these options are very useful, if find that none of them are giving me the close to real world testing scenario of testing emails in actual email clients I’m looking for while making sure that no emails get accidentally sent to real people.

The danger of emails escaping your development or staging environment is especially big of you use snapshots of production data to populate your development and staging databases.

My Preferred Setup

I found that the following setup works best for me:

  • in the development environment all emails are redirected to my personal email account
  • in the staging environment emails are only sent to a whitelisted set of domains

This means that while developing on my local machine, I get to see every email that is sent. I get to see the email in in the wild that is in an actual email client. I can also use different email clients or web-clients to test the layout. This gives me a more realistic way of testing the email than just in my browser where they layout can look quite different to an email client.

On staging I typically whitelist the email domain(s) of the involved development companies or private domains of individual developers. This way all developers, QA and business people can see how the emails look like, but still no emails get sent to actual customers.

Gems

There are a couple of guides and blog posts about how to set this up manually. Or you can use a gem to help you with the setup:

  • A popular solution is Myron Marston’s gem mail_safe
  • I extracted a similar solution from my projects in the mailsafe gem (sorry for the name clash… I didn’t notice until a while after I published it)

Here are the instructions to work with the mailsafe gem:

To install it add

gem 'mailsafe' 

to your Gemfile and run

bundle install 

Then add the following lines to your development.rb file:

Mailsafe.setup do |config|
  config.override_receiver = "your-email-address@example.com"
  config.prefix_email_subject_with_rails_env = true
end

and the following lines to your staging.rb file:

Mailsafe.setup do |config|
  config.allowed_domain = "devco.com, clientco.com"
  config.prefix_email_subject_with_rails_env = true
end

And you will never have to worry about escaping emails again.

References