mail_safe-sendgrid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +5 -0
- data/Rakefile +7 -0
- data/lib/core_ext/kernel.rb +8 -0
- data/lib/mail_safe-sendgrid.rb +81 -0
- data/mail_safe-sendgrid.gemspec +23 -0
- data/spec/mail_safe-sendgrid_spec.rb +36 -0
- data/spec/spec_helper.rb +41 -0
- metadata +124 -0
data/.gitignore
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
module MailSafe
|
2
|
+
class AddressReplacer
|
3
|
+
require 'json'
|
4
|
+
class << self
|
5
|
+
|
6
|
+
@@replaced_addresses = {}
|
7
|
+
@@address_types = ADDRESS_TYPES.dup
|
8
|
+
|
9
|
+
def add_body_postscript_with_mail_header(part, replaced_addresses, call_super = false)
|
10
|
+
# we don't want to call add_body_postscript until after we've added the SendGrid SMTPAPI
|
11
|
+
# replaced addresses to the list of all addresses replaced by Mail Safe
|
12
|
+
@@replaced_addresses.reverse_merge! replaced_addresses
|
13
|
+
if call_super or replaced_addresses[:X_SMTPAPI]
|
14
|
+
add_xsmtpapi_to_address_types
|
15
|
+
add_body_postscript_without_mail_header(part, @@replaced_addresses)
|
16
|
+
remove_xsmtpapi_from_address_types
|
17
|
+
end
|
18
|
+
end
|
19
|
+
alias_method_chain :add_body_postscript, :mail_header
|
20
|
+
|
21
|
+
|
22
|
+
def replace_external_addresses_with_mail_header(mail)
|
23
|
+
replace_external_addresses_without_mail_header(mail)
|
24
|
+
# load X-SMTPAPI headers (json)
|
25
|
+
x_smtpapi = JSON.parse mail.header['X-SMTPAPI'].to_s
|
26
|
+
|
27
|
+
external_indices = []
|
28
|
+
# get a list of indices for external addresses
|
29
|
+
x_smtpapi['to'].each_with_index do |address, index|
|
30
|
+
external_indices << index if !MailSafe::Config.is_internal_address?(address)
|
31
|
+
end
|
32
|
+
deleted_cnt = 0
|
33
|
+
deleted_addresses = []
|
34
|
+
external_indices.each do |i|
|
35
|
+
# remove any external address from :to
|
36
|
+
deleted_addresses << x_smtpapi['to'].delete_at(i - deleted_cnt)
|
37
|
+
# remove the related tokens for every array in :sub
|
38
|
+
x_smtpapi['sub'].each do |k, v|
|
39
|
+
v.delete_at(i-deleted_cnt)
|
40
|
+
x_smtpapi['sub'][k] = v
|
41
|
+
end
|
42
|
+
# the indices decrement as we remove elements, deleted_cnt accounts for this
|
43
|
+
deleted_cnt += 1
|
44
|
+
end
|
45
|
+
# save the headers back to X-SMTPAPI
|
46
|
+
mail.header['X-SMTPAPI'].value = x_smtpapi.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')
|
47
|
+
|
48
|
+
add_body_postscript(mail,{:X_SMTPAPI=>deleted_addresses}, true)
|
49
|
+
# we need to override add_text_postscript to include sendgrid x-smtpapi and not ADDRESS_TYPES only
|
50
|
+
|
51
|
+
end
|
52
|
+
alias_method_chain :replace_external_addresses, :mail_header
|
53
|
+
|
54
|
+
# ADDRESS_TYPES is frozen but we want to add :X_SMTPAPI to it
|
55
|
+
# so the original add_[text|html]_postscript functions have
|
56
|
+
# the SendGrid specific addresses that MailSafe removed as well
|
57
|
+
# when generating their message. Otherwise we would have to
|
58
|
+
# override these functions entirely.
|
59
|
+
def add_xsmtpapi_to_address_types
|
60
|
+
modified_address_types = @@address_types.dup
|
61
|
+
modified_address_types << :X_SMTPAPI
|
62
|
+
# silence already initialized constant ADDRESS_TYPES
|
63
|
+
silence_warnings do
|
64
|
+
MailSafe::AddressReplacer.singleton_class.const_set(:ADDRESS_TYPES,modified_address_types.uniq)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# We then need to reset ADDRESS_TYPES back to the frozen value
|
69
|
+
# for MailSafe to be able to process the next message properly
|
70
|
+
def remove_xsmtpapi_from_address_types
|
71
|
+
|
72
|
+
# silence already initialized constant ADDRESS_TYPES
|
73
|
+
silence_warnings do
|
74
|
+
MailSafe::AddressReplacer.singleton_class.const_set(:ADDRESS_TYPES,@@address_types)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mail_safe-sendgrid'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2012-03-13'
|
5
|
+
s.summary = 'Sendgrid support for Mail Safe'
|
6
|
+
s.description = 'Extends Mail Safe to look for and sanitize Sendgrid specific X-SMTPAPI headers'
|
7
|
+
s.authors = ['Ed Shadi', 'Vijay Ramesh']
|
8
|
+
s.email = ['ed@change.org', 'vijay@change.org']
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
12
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
|
15
|
+
s.add_runtime_dependency 'json'
|
16
|
+
s.add_runtime_dependency 'mail_safe'
|
17
|
+
s.add_runtime_dependency 'sendgrid'
|
18
|
+
|
19
|
+
s.add_development_dependency 'actionmailer'
|
20
|
+
s.add_development_dependency 'rake'
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MailSafe::AddressReplacer do
|
4
|
+
before do
|
5
|
+
default_internal_address = /.*@internal\.com/i
|
6
|
+
|
7
|
+
if defined?(MailSafe::Config)
|
8
|
+
MailSafe::Config.internal_address_definition = lambda { |addr|
|
9
|
+
addr =~ default_internal_address
|
10
|
+
}
|
11
|
+
MailSafe::Config.replacement_address = "replacement@internal.com"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
describe '#replace_external_addresses' do
|
15
|
+
before(:each) do
|
16
|
+
@mail = TestMailer.create_test
|
17
|
+
MailSafe::AddressReplacer.replace_external_addresses(@mail)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should generate one email using the content's template, and with Sendgrid SMTP headers for each user's info" do
|
21
|
+
|
22
|
+
sendgrid_header = JSON.parse(@mail['X-SMTPAPI'].to_s)
|
23
|
+
sendgrid_header['sub']['{username}'].should == ['internal_1','internal_2']
|
24
|
+
sendgrid_header['sub']['{email_address}'].should == ['internal_1@internal.com', 'internal_2@internal.com']
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should create the appropriate postscript including X-SMTPAPI addresses that were removed" do
|
29
|
+
postscript = %r(The original recipients were:\s*- to:\s*- donotreply@foobar.com\s*- X_SMTPAPI:\s*- external_1@external.com\s*- external_2@external.com)
|
30
|
+
@mail.parts[0].body.should =~ postscript
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$:.unshift(File.expand_path("../../lib", __FILE__))
|
2
|
+
require 'rubygems'
|
3
|
+
require 'mail_safe'
|
4
|
+
require 'sendgrid'
|
5
|
+
require 'mail_safe-sendgrid'
|
6
|
+
require 'action_mailer'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
class TestMailer < ActionMailer::Base
|
10
|
+
include SendGrid
|
11
|
+
public
|
12
|
+
def create_test
|
13
|
+
user_data = [
|
14
|
+
{:email_address =>'external_1@external.com', :name =>'external_1'},
|
15
|
+
{:email_address =>'external_2@external.com', :name =>'external_2'},
|
16
|
+
{:email_address =>'internal_1@internal.com', :name =>'internal_1'},
|
17
|
+
{:email_address =>'internal_2@internal.com', :name =>'internal_2'}
|
18
|
+
]
|
19
|
+
subject = 'Test'
|
20
|
+
content = 'This is a test email.'
|
21
|
+
|
22
|
+
email_addresses, usernames = transpose_user_data(user_data)
|
23
|
+
|
24
|
+
sendgrid_recipients email_addresses
|
25
|
+
sendgrid_substitute '{username}', usernames
|
26
|
+
sendgrid_substitute '{email_address}', email_addresses
|
27
|
+
sendgrid_category 'Test Email'
|
28
|
+
|
29
|
+
mail(:to => 'donotreply@foobar.com', :from => 'donotreply@foobar.com', :subject => subject) do |format|
|
30
|
+
format.text { render :text => content }
|
31
|
+
format.html { render :text => content }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def transpose_user_data(user_data)
|
36
|
+
user_data.collect { |u| [ u[:email_address], u[:name]] }.transpose
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mail_safe-sendgrid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ed Shadi
|
9
|
+
- Vijay Ramesh
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-13 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
requirement: &70275505733960 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70275505733960
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mail_safe
|
28
|
+
requirement: &70275505733020 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70275505733020
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: sendgrid
|
39
|
+
requirement: &70275505732220 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70275505732220
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: actionmailer
|
50
|
+
requirement: &70275505731220 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70275505731220
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rake
|
61
|
+
requirement: &70275505749140 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *70275505749140
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: &70275505747520 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *70275505747520
|
81
|
+
description: Extends Mail Safe to look for and sanitize Sendgrid specific X-SMTPAPI
|
82
|
+
headers
|
83
|
+
email:
|
84
|
+
- ed@change.org
|
85
|
+
- vijay@change.org
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- Rakefile
|
93
|
+
- lib/core_ext/kernel.rb
|
94
|
+
- lib/mail_safe-sendgrid.rb
|
95
|
+
- mail_safe-sendgrid.gemspec
|
96
|
+
- spec/mail_safe-sendgrid_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage:
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.15
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Sendgrid support for Mail Safe
|
122
|
+
test_files:
|
123
|
+
- spec/mail_safe-sendgrid_spec.rb
|
124
|
+
- spec/spec_helper.rb
|