dalbaqui-sanitize_email 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +112 -0
- data/VERSION.yml +4 -0
- data/lib/sanitize_email.rb +5 -0
- data/lib/sanitize_email/custom_environments.rb +23 -0
- data/lib/sanitize_email/sanitize_email.rb +64 -0
- data/test/sample_mailer.rb +24 -0
- data/test/sanitize_email_test.rb +21 -0
- metadata +62 -0
data/README.rdoc
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
= sanitize_email
|
2
|
+
|
3
|
+
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.
|
4
|
+
|
5
|
+
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.
|
6
|
+
|
7
|
+
Peter Boling wrote the plugin that this gem is derived from. It currently solves three common problems in ruby web applications that use ActionMailer:
|
8
|
+
|
9
|
+
=== Working Locally with Production Data
|
10
|
+
|
11
|
+
Peter described this common problem in his original plugin implementation as such:
|
12
|
+
|
13
|
+
* I have a production site with live data.
|
14
|
+
* 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.
|
15
|
+
* On this separate machine (staging, or development) I run tests, and test various features.
|
16
|
+
* 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.
|
17
|
+
|
18
|
+
=== Re-routing Email on a Staging or QA Server
|
19
|
+
|
20
|
+
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}[http://github.com/jtrupiano/rails-caddy] to allow each QA person to configure it specifically for them.
|
21
|
+
|
22
|
+
=== Testing Email from a Hot Production Server
|
23
|
+
|
24
|
+
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.
|
25
|
+
|
26
|
+
== Install
|
27
|
+
|
28
|
+
gem sources -a http://gems.github.com/
|
29
|
+
gem install jtrupiano-sanitize_email
|
30
|
+
|
31
|
+
== Setup
|
32
|
+
|
33
|
+
It only requires a few lines of configuration:
|
34
|
+
|
35
|
+
* Rails 1.x: Add to bottom of environment.rb
|
36
|
+
|
37
|
+
* Rails 2.x: Use an initializer, stick it in any initializer file, or create a new one for sanitize_email
|
38
|
+
|
39
|
+
Add this bit and customize for your app:
|
40
|
+
|
41
|
+
# Settings for sanitize_email gem. These can be overridden in individual config/%env%/environment.rb files.
|
42
|
+
|
43
|
+
require 'sanitize_email'
|
44
|
+
ActionMailer::Base.sanitized_recipients = "jtrupiano@gmail.com"
|
45
|
+
ActionMailer::Base.sanitized_bcc = nil
|
46
|
+
ActionMailer::Base.sanitized_cc = nil
|
47
|
+
|
48
|
+
# These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!
|
49
|
+
# All environments not listed will be treated as normal.
|
50
|
+
ActionMailer::Base.local_environments = %w( development test staging )
|
51
|
+
|
52
|
+
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.
|
53
|
+
|
54
|
+
But wait there's more:
|
55
|
+
|
56
|
+
Let's say you have a method in your model that you can call to test the signup email.
|
57
|
+
You want to be able to test sending it to any user at any time... but you don't want
|
58
|
+
the user to ACTUALLY get the email, even in production. A dilemma, yes? Not anymore!
|
59
|
+
|
60
|
+
All your mailers get a force_sanitize class method which takes precedence over the environment override.
|
61
|
+
|
62
|
+
When force_sanitize is nil it will not be used by sanitize_email to determine if it should override the recipients, bcc, and cc.
|
63
|
+
|
64
|
+
|
65
|
+
== Example
|
66
|
+
|
67
|
+
So here's how you can use force_sanitize to override the override.
|
68
|
+
|
69
|
+
Even if you set:
|
70
|
+
|
71
|
+
ActionMailer::Base.local_environments = %w( development )
|
72
|
+
|
73
|
+
and are in the development environment, you can override the override anywhere in your code.
|
74
|
+
|
75
|
+
class User < ActiveRecord::Base
|
76
|
+
def test_signup_email_me_only
|
77
|
+
UserMailer.force_sanitize = true
|
78
|
+
UserMailer.deliver_signup_notification(self)
|
79
|
+
UserMailer.force_sanitize = nil
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_signup_email_user_only
|
83
|
+
UserMailer.force_sanitize = false
|
84
|
+
UserMailer.deliver_signup_notification(self)
|
85
|
+
UserMailer.force_sanitize = nil
|
86
|
+
end
|
87
|
+
|
88
|
+
# this third method would conditionally use the overridden recipients based on current Rails environment
|
89
|
+
def test_signup_email_environment
|
90
|
+
UserMailer.deliver_signup_notification(self)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
Load the console with ruby script/console and regardless of what environment you are in:
|
95
|
+
|
96
|
+
> User.find(4).test_signup_email_me_only
|
97
|
+
|
98
|
+
and the email will have it's recipients, bcc, and cc overridden to be whatever you set the sanitized values to be.
|
99
|
+
Then if you want to send it to the actual user, instead of yourself
|
100
|
+
|
101
|
+
> User.find(4).test_signup_email_user_only
|
102
|
+
|
103
|
+
== References
|
104
|
+
* {RDoc}[http://johntrupiano.rubyforge.org/sanitize_email]
|
105
|
+
* {Source Code}[http://github.com/jtrupiano/sanitize_email]
|
106
|
+
* {Gem Release Announcement}[http://blog.smartlogicsolutions.com/2009/04/25/reintroducing-sanitize_email-work-with-production-email-without-fear/]
|
107
|
+
* {Peter's Original Writeup}[http://galtzo.blogspot.com/2008/11/sanitize-email-never-worry-about.html]
|
108
|
+
* {Using sanitize_email to Preview HTML Emails Locally}[http://blog.smartlogicsolutions.com/2009/04/30/using-sanitize-email-to-preview-html-emails-locally/]
|
109
|
+
|
110
|
+
Copyright (c) 2009 {John Trupiano}[http://smartlogicsolutions.com/wiki/John_Trupiano] of {SmartLogic Solutions, LLC}[http://www.smartlogicsolutions.com]
|
111
|
+
Copyright (c) 2008 {Peter H. Boling}[http://www.peterboling.com/about.html] of {9thBit LLC}[http://www.peterboling.com/]
|
112
|
+
Released under the MIT license
|
data/VERSION.yml
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "sanitize_email", "sanitize_email")
|
2
|
+
require File.join(File.dirname(__FILE__), "sanitize_email", "custom_environments")
|
3
|
+
|
4
|
+
ActionMailer::Base.send :include, NinthBit::CustomEnvironments
|
5
|
+
ActionMailer::Base.send :include, NinthBit::SanitizeEmail
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#Copyright (c) 2008 Peter H. Boling of 9thBit LLC
|
2
|
+
#Released under the MIT license
|
3
|
+
|
4
|
+
module NinthBit
|
5
|
+
module CustomEnvironments
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
|
10
|
+
base.cattr_accessor :local_environments
|
11
|
+
base.local_environments = %w( development test )
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def consider_local?
|
17
|
+
local_environments.include?(RAILS_ENV)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#Copyright (c) 2008 Peter H. Boling of 9thBit LLC
|
2
|
+
#Released under the MIT license
|
3
|
+
|
4
|
+
module NinthBit
|
5
|
+
module SanitizeEmail
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
|
9
|
+
# Adds the following class attributes to the classes that include NinthBit::SanitizeEmail
|
10
|
+
base.cattr_accessor :force_sanitize
|
11
|
+
base.force_sanitize = nil
|
12
|
+
|
13
|
+
# Specify the BCC addresses for the messages that go out in 'local' environments
|
14
|
+
base.cattr_accessor :sanitized_bcc
|
15
|
+
base.sanitized_bcc = nil
|
16
|
+
|
17
|
+
# Specify the CC addresses for the messages that go out in 'local' environments
|
18
|
+
base.cattr_accessor :sanitized_cc
|
19
|
+
base.sanitized_cc = nil
|
20
|
+
|
21
|
+
# The recipient addresses for the messages, either as a string (for a single
|
22
|
+
# address) or an array (for multiple addresses) that go out in 'local' environments
|
23
|
+
base.cattr_accessor :sanitized_recipients
|
24
|
+
base.sanitized_recipients = nil
|
25
|
+
|
26
|
+
base.class_eval do
|
27
|
+
#We need to alias these methods so that our new methods get used instead
|
28
|
+
alias :real_bcc :bcc
|
29
|
+
alias :real_cc :cc
|
30
|
+
alias :real_recipients :recipients
|
31
|
+
alias :real_subject :subject
|
32
|
+
|
33
|
+
def localish?
|
34
|
+
#consider_local is a method in sanitize_email/lib/custom_environments.rb
|
35
|
+
# it is included in ActionMailer in sanitize_email/init.rb
|
36
|
+
!self.class.force_sanitize.nil? ? self.class.force_sanitize : self.class.consider_local?
|
37
|
+
end
|
38
|
+
|
39
|
+
def subject(*lines)
|
40
|
+
real_subject *lines
|
41
|
+
localish? ? "(#{real_recipients}) #{real_subject}" : real_subject unless real_recipients.nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
def recipients(*addresses)
|
45
|
+
real_recipients *addresses
|
46
|
+
puts "sanitize_email error: sanitized_recipients is not set" if self.class.sanitized_recipients.nil?
|
47
|
+
localish? ? self.class.sanitized_recipients : real_recipients
|
48
|
+
end
|
49
|
+
|
50
|
+
def bcc(*addresses)
|
51
|
+
real_bcc *addresses
|
52
|
+
localish? ? self.class.sanitized_bcc : real_bcc
|
53
|
+
end
|
54
|
+
|
55
|
+
def cc(*addresses)
|
56
|
+
real_cc *addresses
|
57
|
+
localish? ? self.class.sanitized_cc : real_cc
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end # end Module SanitizeEmail
|
64
|
+
end # end Module NinthBit
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'action_mailer'
|
4
|
+
|
5
|
+
# configure ActionMailer
|
6
|
+
ActionMailer::Base.template_root = "."
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__), "..", "lib", 'sanitize_email')
|
9
|
+
|
10
|
+
|
11
|
+
class SampleMailer < ActionMailer::Base
|
12
|
+
|
13
|
+
def gmail_override
|
14
|
+
# set them all to send to gmail
|
15
|
+
@recipients = "jtrupiano@gmail.com"
|
16
|
+
@cc = "jtrupiano@gmail.com"
|
17
|
+
@bcc = "jtrupiano@gmail.com"
|
18
|
+
|
19
|
+
@subject = "Hello there"
|
20
|
+
|
21
|
+
part :content_type => "text/html", :body => "Hello there"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/sample_mailer'
|
3
|
+
|
4
|
+
class SanitizeEmailTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_send_can_override_recips_cc_bcc_all_independently
|
7
|
+
# configure SanitizeEmail
|
8
|
+
ActionMailer::Base.sanitized_recipients = "john@smartlogicsolutions.com"
|
9
|
+
ActionMailer::Base.sanitized_bcc = nil
|
10
|
+
ActionMailer::Base.sanitized_cc = "john@smartlogicsolutions.com"
|
11
|
+
ActionMailer::Base.local_environments = %w( test )
|
12
|
+
ENV['RAILS_ENV'] = 'test'
|
13
|
+
|
14
|
+
# the mailer sets all 3 of these values to "jtrupiano@gmail.com". We override them independently
|
15
|
+
tmail = SampleMailer.create_gmail_override
|
16
|
+
assert_equal ["john@smartlogicsolutions.com"], tmail.to
|
17
|
+
assert_equal ["john@smartlogicsolutions.com"], tmail.cc
|
18
|
+
assert_equal nil, tmail.bcc
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dalbaqui-sanitize_email
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Trupiano
|
8
|
+
- Peter Boling
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-04-25 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses
|
18
|
+
email: jtrupiano@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- README.rdoc
|
27
|
+
- VERSION.yml
|
28
|
+
- lib/sanitize_email
|
29
|
+
- lib/sanitize_email/custom_environments.rb
|
30
|
+
- lib/sanitize_email/sanitize_email.rb
|
31
|
+
- lib/sanitize_email.rb
|
32
|
+
- test/sample_mailer.rb
|
33
|
+
- test/sanitize_email_test.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/jtrupiano/sanitize_email
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --inline-source
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: johntrupiano
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Tool to aid in development, testing, qa, and production troubleshooting of email issues without worrying that emails will get sent to actual live addresses.
|
61
|
+
test_files: []
|
62
|
+
|