This plugin fills a gaping hole I have found in every one of the Rails projects I've worked on.
SanitizeEmail allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses.
This is the problem I have with site after site:
1) I have a production site with live data.
2) I dump the live data and securely transfer it to another machine (rync -e ssh), and import it using scripts that I will soon open source
3) On this separate machine (staging, or development) I run tests, and test various features.
4) I usually want the emails to get sent from these non-production evnironments 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.
So I wrote this plugin! :P
It is an "install it and forget it" type plugin that requires very little setup. It includes some very innocuous monkey patching of ActiveRecord::Base to work its magic.
./script/plugin install git://github.com/pboling/sanitize_email.git
It only requires a few lines of configuration:
Add to bottom of environment.rb
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 plugin:
#Overrides the recipients of all outgoing mail in local environments
ActionMailer::Base.sanitized_recipients = "email@example.com"
#Overrides the BCC of all outgoing mail in local environments,
# but only if a BCC is specified on the message
ActionMailer::Base.sanitized_bcc = "firstname.lastname@example.org"
#Or you can override bcc's with a nil value thereby ignoring the bcc in mail sent from the local_enviroments
#ActionMailer::Base.sanitized_bcc = "email@example.com"
#Overrides the CC of all outgoing mail in local environments,
# but only if a CC is specified on the message
ActionMailer::Base.sanitized_cc = "firstname.lastname@example.org"
#Or you can override cc's with a nil value thereby ignoring the cc in mail sent from the local_enviroments
#ActionMailer::Base.sanitized_cc = nil
#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 )
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. A dilemna, yes? Not anymore!
All your mailers get a force_sanitize clas method which takes precedence over the environment override.
When force_sanitize is nil it will not be used by sanitize_email to detrmine 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
UserMailer.force_sanitize = true
UserMailer.force_sanitize = nil
UserMailer.force_sanitize = false
UserMailer.force_sanitize = nil
#this third method would conditionally use the overridden recipients based on current Rails environment
Load the console with ruby script/console and regardless of what environment you are in:
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
That's it! Enjoy!
Copyright (c) 2008 Peter H. Boling of 9thBit LLC
Released under the MIT license