Links
SanitizeEmail
Categories
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 to.
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:
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 = "developer@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 = "developer@example.com"
#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 = nil
#Overrides the CC of all outgoing mail in local environments,
# but only if a CC is specified on the message
ActionMailer::Base.sanitized_cc = "developer@example.com"
#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 class 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
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
#this third method would conditionally use the overridden recipients based on current Rails environment
def test_signup_email_environment
OicuMailer.deliver_signup_notification(self)
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
Vitals
| Home | http://galtzo.blogspot.com/2008/11/sanitize-email-never-worry-about.html |
|---|---|
| Repository | git://github.com/pboling/sanitize_email.git |
| License | Rails' (MIT) |
| Tags |
actionmailer email
|
| Rating | (2 votes) |
| Owner | Peter Boling |
| Created | 6 November 2008 |

