jtrupiano-sanitize_email 0.0.3 → 0.1.0
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 +39 -44
- data/VERSION.yml +2 -2
- data/test/sample_mailer.rb +24 -0
- data/test/sanitize_email_test.rb +16 -3
- metadata +2 -1
data/README.textile
CHANGED
|
@@ -4,36 +4,32 @@ This gem allows you to quickly globally override your mail delivery settings in
|
|
|
4
4
|
|
|
5
5
|
This is the problem I have with site after site:
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
but I don't ever want to risk them getting sent to addresses that are not mine.
|
|
7
|
+
# I have a production site with live data.
|
|
8
|
+
# 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
|
+
# On this separate machine (staging, or development) I run tests, and test various features.
|
|
10
|
+
# 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.
|
|
12
11
|
|
|
13
|
-
So Peter Boling wrote a plugin! And I've taken it and turned into a gem!
|
|
12
|
+
So Peter Boling wrote a plugin! And I've taken it and turned it into a gem!
|
|
14
13
|
|
|
15
14
|
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
15
|
|
|
17
16
|
|
|
18
17
|
h2. Install
|
|
19
18
|
|
|
20
|
-
gem sources -a http://gems.github.com/
|
|
21
|
-
gem install jtrupiano-sanitize_email
|
|
22
|
-
|
|
19
|
+
gem sources -a http://gems.github.com/
|
|
20
|
+
gem install jtrupiano-sanitize_email
|
|
23
21
|
|
|
24
22
|
h2. Setup
|
|
25
23
|
|
|
26
24
|
It only requires a few lines of configuration:
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
Add to bottom of environment.rb
|
|
26
|
+
* Rails 1.x: Add to bottom of environment.rb
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
Use an initializer, stick it in any initializer file, or create a new one for sanitize_email
|
|
28
|
+
* Rails 2.x: Use an initializer, stick it in any initializer file, or create a new one for sanitize_email
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
Add this bit and customize for your app:
|
|
35
31
|
|
|
36
|
-
<
|
|
32
|
+
<pre>
|
|
37
33
|
# Settings for sanitize_email gem. These can be overridden in individual config/%env%/environment.rb files.
|
|
38
34
|
|
|
39
35
|
require 'sanitize_email'
|
|
@@ -44,19 +40,19 @@ It only requires a few lines of configuration:
|
|
|
44
40
|
# These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!
|
|
45
41
|
# All environments not listed will be treated as normal.
|
|
46
42
|
ActionMailer::Base.local_environments = %w( development test staging )
|
|
47
|
-
</
|
|
43
|
+
</pre>
|
|
48
44
|
|
|
49
45
|
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
46
|
|
|
51
47
|
But wait there's more:
|
|
52
48
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
Let's say you have a method in your model that you can call to test the signup email.
|
|
50
|
+
You want to be able to test sending it to any user at any time... but you don't want
|
|
51
|
+
the user to ACTUALLY get the email, even in production. A dilemma, yes? Not anymore!
|
|
56
52
|
|
|
57
|
-
|
|
53
|
+
All your mailers get a force_sanitize class method which takes precedence over the environment override.
|
|
58
54
|
|
|
59
|
-
|
|
55
|
+
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
56
|
|
|
61
57
|
|
|
62
58
|
h2. Example
|
|
@@ -64,33 +60,32 @@ h2. Example
|
|
|
64
60
|
So here's how you can use force_sanitize to override the override.
|
|
65
61
|
|
|
66
62
|
Even if you set:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
</ruby>
|
|
63
|
+
|
|
64
|
+
ActionMailer::Base.local_environments = %w( development )
|
|
70
65
|
|
|
71
66
|
and are in the development environment, you can override the override anywhere in your code.
|
|
72
67
|
|
|
73
|
-
<
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
68
|
+
<pre>
|
|
69
|
+
class User < ActiveRecord::Base
|
|
70
|
+
def test_signup_email_me_only
|
|
71
|
+
UserMailer.force_sanitize = true
|
|
72
|
+
UserMailer.deliver_signup_notification(self)
|
|
73
|
+
UserMailer.force_sanitize = nil
|
|
92
74
|
end
|
|
93
|
-
|
|
75
|
+
|
|
76
|
+
def test_signup_email_user_only
|
|
77
|
+
UserMailer.force_sanitize = false
|
|
78
|
+
UserMailer.deliver_signup_notification(self)
|
|
79
|
+
UserMailer.force_sanitize = nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# this third method would conditionally use the overridden recipients based on current Rails environment
|
|
83
|
+
def test_signup_email_environment
|
|
84
|
+
UserMailer.deliver_signup_notification(self)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
end
|
|
88
|
+
</pre>
|
|
94
89
|
|
|
95
90
|
Load the console with ruby script/console and regardless of what environment you are in:
|
|
96
91
|
|
data/VERSION.yml
CHANGED
|
@@ -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
|
data/test/sanitize_email_test.rb
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
|
+
require 'test/sample_mailer'
|
|
2
3
|
|
|
3
4
|
class SanitizeEmailTest < Test::Unit::TestCase
|
|
4
|
-
|
|
5
|
-
def
|
|
6
|
-
|
|
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
|
|
7
19
|
end
|
|
20
|
+
|
|
8
21
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jtrupiano-sanitize_email
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Trupiano
|
|
@@ -29,6 +29,7 @@ files:
|
|
|
29
29
|
- lib/sanitize_email/custom_environments.rb
|
|
30
30
|
- lib/sanitize_email/sanitize_email.rb
|
|
31
31
|
- lib/sanitize_email.rb
|
|
32
|
+
- test/sample_mailer.rb
|
|
32
33
|
- test/sanitize_email_test.rb
|
|
33
34
|
has_rdoc: true
|
|
34
35
|
homepage: http://github.com/jtrupiano/sanitize_email
|