SanitizeEmail allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses.
Switch branches/tags
Clone or download
Pull request Compare This branch is 2 commits ahead, 323 commits behind pboling:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



This gem allows you to globally override your mail delivery settings. It's particularly helpful when you want to omit the delivery of email (e.g. in development/test environments) or alter the to/cc/bcc (e.g. in staging or demo environments) of all email generated from your application.

It is a “configure it and forget it” type gem that requires very little setup. It includes some very innocuous monkey patching of ActionMailer::Base to work its magic.

It currently solves three common problems in ruby web applications that use ActionMailer:

Working Locally with Production Data

Peter described this common problem in his original plugin implementation as such:

  • I have a production site with live data.

  • I dump the live data and securely transfer it to another machine (rync -e ssh), and import it using a few rake tasks here:

  • On this separate machine (staging, or development) I run tests, and test various features which often send out email (registration/signup, order placement, etc.)

  • I usually want the emails to get sent from these non-production environments so I can verify what they look like when sent, but I don't ever want to risk them getting sent to addresses that are not mine.

Re-routing Email on a Staging or QA Server

Another very important use case for me is to transparently re-route email generated from a staging or QA server to an appropriate person. For example, it's common for us to set up a staging server for a client to use to view our progress and test out new features. It's important for any email that is generated from our web application be delivered to the client's inbox so that they can review the content and ensure that it's acceptable. Similarly, we set up QA instances for our own QA team and we use rails-caddy to allow each QA person to configure it specifically for them.

Testing Email from a Hot Production Server

If you install this gem on a production server (which I don't always do), you can load up script/console and override the to/cc/bcc on all emails for the duration of your console session. This allows you to poke and prod a live production instance, and route all email to your own inbox for inspection. The best part is that this can all be accomplished without changing a single line of your application code.

Install as a RubyGem

Gem Using Git building from source:

mkdir -p ~/src
cd ~/src
git clone git://
cd sanitize_email
gem build sanitize_email.gemspec
sudo gem install sanitize_email-0.3.6.gem # (Or whatever version gets built)

Gemcutter is the hot new gem host, and you can use it like this:

[sudo] gem install gemcutter
[sudo] gem tumble # makes gemcutter gem source first in line
[sudo] gem install sanitize_email

Then in your environment.rb (if you are just going to use it from the console, you can require it as needed there, and skip the config.gem):

config.gem 'sanitize_email'

Then cd to your rails app to optionally freeze the gem into your app (if you roll this way):

rake gems:freeze GEM=sanitize_email

Install as a Plugin

Plugin using Git:

# Installation as plugin works too! (let me know if you find any bugs, as I don't ever run it this way.)
./script/plugin install git://

Install as a Git Submodule (plugin)

git submodule add git:// vendor/plugins/sanitize_email


It only requires a few lines of configuration:

  • Rails 1.x: Add to bottom of environment.rb

  • Rails 2.x: Use an initializer, stick it in any initializer file, or create a new one for sanitize_email

Add this bit and customize for your app:

# Settings for sanitize_email gem.  These can be overridden in individual config/%env%/environment.rb files.

require 'sanitize_email'
ActionMailer::Base.sanitized_recipients = ""
ActionMailer::Base.sanitized_bcc = nil
ActionMailer::Base.sanitized_cc = nil

# optionally, you can configure sanitize_email to to include the "real" email address as the 'user name' of the
# "sanitized" email (e.g. " <>")
ActionMailer::Base.use_actual_email_as_sanitized_user_name = true # defaults to false

# These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!  
# All environments not listed will be treated as normal.
ActionMailer::Base.local_environments = %w( development test staging )

Keep in mind, this is ruby (and possibly rails), so you can add conditionals or utilize different environment.rb files to customize these settings on a per-environment basis.

But wait there's more:

Let's say you have a method in your model that you can call to test the signup email. You want to be able to test sending it to any user at any time… but you don't want the user to ACTUALLY get the email, even in production. A dilemma, yes? Not anymore!

All your mailers get a force_sanitize class method which takes precedence over the environment override.

When force_sanitize is nil it will not be used by sanitize_email to determine if it should override the recipients, bcc, and cc.


So here's how you can use force_sanitize to override the override.

Even if you set:

ActionMailer::Base.local_environments = %w( development )

and are in the development environment, you can override the override anywhere in your code.

class User < ActiveRecord::Base
  def test_signup_email_me_only
    UserMailer.force_sanitize = true
    UserMailer.force_sanitize = nil

  def test_signup_email_user_only
    UserMailer.force_sanitize = false
    UserMailer.force_sanitize = nil

  # this third method would conditionally use the overridden recipients based on current Rails environment
  def test_signup_email_environment

Load the console with ruby script/console and regardless of what environment you are in:

> User.find(4).test_signup_email_me_only

and the email will have it's recipients, bcc, and cc overridden to be whatever you set the sanitized values to be. Then if you want to send it to the actual user, instead of yourself

> User.find(4).test_signup_email_user_only


Peter Boling is the author of the gem/plugin. John Trupiano did the initial conversion from plugin to gem as well as improving the code.


George Anderson's work / improvements has been merged in.


Copyright © 2008-9 Peter H. Boling of 9thBit LLC Copyright © 2009 John Trupiano of SmartLogic Solutions, LLC Released under the MIT license