-
Notifications
You must be signed in to change notification settings - Fork 942
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store overriden to, cc, bcc values in X- headers
- Loading branch information
Dan Croak
committed
Nov 17, 2012
1 parent
0f9553b
commit 5f01a66
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,16 @@ module Mail | |
# delivery_method :override_recipient_smtp, | ||
# :to => ['[email protected]', '[email protected]'] | ||
# end | ||
# | ||
# === Debug | ||
# | ||
# The original to, cc, and bcc fields are stored in these custom email headers | ||
# for debugging: | ||
# | ||
# X-Override-To | ||
# X-Override-Cc | ||
# X-Override-Bcc | ||
# | ||
class OverrideRecipientSMTP < Mail::SMTP | ||
def initialize(values) | ||
unless values[:to] | ||
|
@@ -31,11 +41,29 @@ def initialize(values) | |
end | ||
|
||
def deliver!(mail) | ||
store_in_custom_headers(mail) | ||
|
||
mail.to = settings[:to] | ||
mail.cc = nil | ||
mail.bcc = nil | ||
|
||
super(mail) | ||
end | ||
|
||
private | ||
|
||
def store_in_custom_headers(mail) | ||
{ | ||
'X-Override-To' => mail.to, | ||
'X-Override-Cc' => mail.cc, | ||
'X-Override-Bcc' => mail.bcc | ||
}.each do |header, addresses| | ||
if addresses | ||
addresses.each do |address| | ||
mail.header = "#{mail.header}\n#{header}: #{address}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,13 @@ | |
end | ||
|
||
response = mail.deliver! | ||
|
||
response.to.should eq ['[email protected]'] | ||
response.cc.should eq [] | ||
response.bcc.should eq [] | ||
response.header['X-Override-To'].to_s.should eq '[[email protected], [email protected]]' | ||
response.header['X-Override-Cc'].to_s.should eq '[email protected]' | ||
response.header['X-Override-Bcc'].to_s.should eq '[email protected]' | ||
end | ||
|
||
it 'can accept an array as configuration' do | ||
|
@@ -39,6 +43,7 @@ | |
end | ||
|
||
response = mail.deliver! | ||
|
||
response.to.should eq ['[email protected]', '[email protected]'] | ||
end | ||
end |