jtrupiano-sanitize_email 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +107 -0
- data/VERSION.yml +4 -0
- data/lib/sanitize_email/custom_environments.rb +27 -0
- data/lib/sanitize_email/sanitize_email.rb +58 -0
- data/lib/sanitize_email.rb +5 -0
- data/test/sanitize_email_test.rb +8 -0
- metadata +61 -0
data/README.textile
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
h1. sanitize_email
|
|
2
|
+
|
|
3
|
+
This gem allows you to quickly globally override your mail delivery settings in depending on the environment. 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
|
+
This is the problem I have with site after site:
|
|
6
|
+
|
|
7
|
+
1) I have a production site with live data.
|
|
8
|
+
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.
|
|
9
|
+
3) On this separate machine (staging, or development) I run tests, and test various features.
|
|
10
|
+
4) I usually want the emails to get sent from these non-production evnironments so I can verify what they look like when sent,
|
|
11
|
+
but I don't ever want to risk them getting sent to addresses that are not mine.
|
|
12
|
+
|
|
13
|
+
So Peter Boling wrote a plugin! And I've taken it and turned into a gem!
|
|
14
|
+
|
|
15
|
+
It is an "configure it and forget it" type gem that requires very little setup. It includes some very innocuous monkey patching of ActiveRecord::Base to work its magic.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
h2. Install
|
|
19
|
+
|
|
20
|
+
gem sources -a http://gems.github.com/
|
|
21
|
+
gem install jtrupiano-sanitize_email
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
h2. Setup
|
|
25
|
+
|
|
26
|
+
It only requires a few lines of configuration:
|
|
27
|
+
|
|
28
|
+
Rails 1.x:
|
|
29
|
+
Add to bottom of environment.rb
|
|
30
|
+
|
|
31
|
+
Rails 2.x:
|
|
32
|
+
Use an initializer, stick it in any initializer file, or create a new one for sanitize_email
|
|
33
|
+
|
|
34
|
+
Add this bit and customize for your app:
|
|
35
|
+
|
|
36
|
+
<ruby>
|
|
37
|
+
# Settings for sanitize_email gem. These can be overridden in individual config/<env>/environment.rb files.
|
|
38
|
+
|
|
39
|
+
require 'sanitize_email'
|
|
40
|
+
ActionMailer::Base.sanitized_recipients = "jtrupiano@gmail.com"
|
|
41
|
+
ActionMailer::Base.sanitized_bcc = nil
|
|
42
|
+
ActionMailer::Base.sanitized_cc = nil
|
|
43
|
+
|
|
44
|
+
# These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!
|
|
45
|
+
# All environments not listed will be treated as normal.
|
|
46
|
+
ActionMailer::Base.local_environments = %w( development test staging )
|
|
47
|
+
</ruby>
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
But wait there's more:
|
|
52
|
+
|
|
53
|
+
Let's say you have a method in your model that you can call to test the signup email.
|
|
54
|
+
You want to be able to test sending it to any user at any time... but you don't want
|
|
55
|
+
the user to ACTUALLY get the email, even in production. A dilemma, yes? Not anymore!
|
|
56
|
+
|
|
57
|
+
All your mailers get a force_sanitize class method which takes precedence over the environment override.
|
|
58
|
+
|
|
59
|
+
When force_sanitize is nil it will not be used by sanitize_email to determine if it should override the recipients, bcc, and cc.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
h2. Example
|
|
63
|
+
|
|
64
|
+
So here's how you can use force_sanitize to override the override.
|
|
65
|
+
|
|
66
|
+
Even if you set:
|
|
67
|
+
<ruby>
|
|
68
|
+
ActionMailer::Base.local_environments = %w( development )
|
|
69
|
+
</ruby>
|
|
70
|
+
|
|
71
|
+
and are in the development environment, you can override the override anywhere in your code.
|
|
72
|
+
|
|
73
|
+
<ruby>
|
|
74
|
+
class User < ActiveRecord::Base
|
|
75
|
+
def test_signup_email_me_only
|
|
76
|
+
UserMailer.force_sanitize = true
|
|
77
|
+
UserMailer.deliver_signup_notification(self)
|
|
78
|
+
UserMailer.force_sanitize = nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def test_signup_email_user_only
|
|
82
|
+
UserMailer.force_sanitize = false
|
|
83
|
+
UserMailer.deliver_signup_notification(self)
|
|
84
|
+
UserMailer.force_sanitize = nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
#this third method would conditionally use the overridden recipients based on current Rails environment
|
|
88
|
+
def test_signup_email_environment
|
|
89
|
+
UserMailer.deliver_signup_notification(self)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
</ruby>
|
|
94
|
+
|
|
95
|
+
Load the console with ruby script/console and regardless of what environment you are in:
|
|
96
|
+
|
|
97
|
+
> User.find(4).test_signup_email_me_only
|
|
98
|
+
|
|
99
|
+
and the email will have it's recipients, bcc, and cc overridden to be whatever you set the sanitized values to be.
|
|
100
|
+
Then if you want to send it to the actual user, instead of yourself
|
|
101
|
+
|
|
102
|
+
> User.find(4).test_signup_email_user_only
|
|
103
|
+
|
|
104
|
+
That's it! Enjoy!
|
|
105
|
+
|
|
106
|
+
Copyright (c) 2008 Peter H. Boling of 9thBit LLC
|
|
107
|
+
Released under the MIT license
|
data/VERSION.yml
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
base.cattr_accessor :deployed_environments
|
|
14
|
+
base.deployed_environments = %w( production staging )
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def consider_local?
|
|
19
|
+
local_environments.include?(ENV['RAILS_ENV'])
|
|
20
|
+
end
|
|
21
|
+
def consider_deployed?
|
|
22
|
+
deployed_environments.include?(ENV['RAILS_ENV'])
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
|
|
32
|
+
def localish?
|
|
33
|
+
#consider_local is a method in sanitize_email/lib/custom_environments.rb
|
|
34
|
+
# it is included in ActionMailer in sanitize_email/init.rb
|
|
35
|
+
!self.class.force_sanitize.nil? ? self.class.force_sanitize : self.class.consider_local?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def recipients(*addresses)
|
|
39
|
+
real_recipients *addresses
|
|
40
|
+
puts "santize_email error: sanitized_recipients is not set" if self.class.sanitized_recipients.nil?
|
|
41
|
+
localish? ? self.class.sanitized_recipients : real_recipients
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def bcc(*addresses)
|
|
45
|
+
real_bcc *addresses
|
|
46
|
+
localish? ? self.class.sanitized_bcc : real_bcc
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def cc(*addresses)
|
|
50
|
+
real_cc *addresses
|
|
51
|
+
localish? ? self.class.sanitized_cc : real_cc
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
end # end Module SanitizeEmail
|
|
58
|
+
end # end Module NinthBit
|
|
@@ -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
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jtrupiano-sanitize_email
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Trupiano
|
|
8
|
+
- Peter Boling
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2009-03-02 00:00:00 -08: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
|
+
|
|
25
|
+
files:
|
|
26
|
+
- README.textile
|
|
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/sanitize_email_test.rb
|
|
33
|
+
has_rdoc: true
|
|
34
|
+
homepage: http://github.com/jtrupiano/sanitize_email
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options:
|
|
37
|
+
- --inline-source
|
|
38
|
+
- --charset=UTF-8
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
requirements: []
|
|
54
|
+
|
|
55
|
+
rubyforge_project:
|
|
56
|
+
rubygems_version: 1.2.0
|
|
57
|
+
signing_key:
|
|
58
|
+
specification_version: 2
|
|
59
|
+
summary: "Gemified fork of pboling's sanitize_email plugin: allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses."
|
|
60
|
+
test_files: []
|
|
61
|
+
|