Skip to content

Commit

Permalink
Store overriden to, cc, bcc values in X- headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed Nov 17, 2012
1 parent 0f9553b commit 5f01a66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/mail/network/delivery_methods/override_recipient_smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,6 +43,7 @@
end

response = mail.deliver!

response.to.should eq ['[email protected]', '[email protected]']
end
end

0 comments on commit 5f01a66

Please sign in to comment.