pboling / sanitize_email

SanitizeEmail allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses.

This URL has Read+Write access

Peter Boling (author)
Wed Nov 05 14:54:42 -0800 2008
commit  4af587719bf4072d286a7f64cc0fa3cbf8386454
tree    70ef17ea7290ee786400763c938680410818b60f
name age message
file MIT-LICENSE Loading commit data...
file README
file Rakefile
file init.rb
file install.rb
directory lib/
directory tasks/
directory test/
file uninstall.rb
README
SanitizeEmail
=============

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.

Install
=======



Setup
=====

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 plugin:
    #Overrides the recipients of all outgoing mail in local environments
    ActionMailer::Base.sanitized_recipients = "peter.boling@swankywebdesign.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 = "peter.boling@swankywebdesign.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 = "peter.boling@swankywebdesign.com"
    
    #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

Example
=======

  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.deliver_signup_notification(self)
      UserMailer.force_sanitize = nil
    end

    def test_signup_email_user_only
      UserMailer.force_sanitize = false
      UserMailer.deliver_signup_notification(self)
      UserMailer.force_sanitize = nil
    end
  end

  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

That's it!  Enjoy!

Copyright (c) 2008 Peter H. Boling of 9thBit LLC
Released under the MIT license