From 0f9553b65c4e6cebd621b2da678e74cd471d58c6 Mon Sep 17 00:00:00 2001 From: Dan Croak and Harlow Ward Date: Thu, 2 Aug 2012 14:25:30 -0400 Subject: [PATCH 1/2] Add OverrideRecipientSMTP delivery method Use the OverrideRecipientSMTP delivery method when you don't want your program to accidentally send emails to addresses other than the overridden recipient which you configure. An example use case is in your web app's staging environment, your development team will receive all staging emails without accidentally emailing users with active email addresses in the database. --- lib/mail/configuration.rb | 2 + lib/mail/network.rb | 1 + .../override_recipient_smtp.rb | 41 +++++++++++++++++ .../override_recipient_smtp_spec.rb | 44 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 lib/mail/network/delivery_methods/override_recipient_smtp.rb create mode 100644 spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb diff --git a/lib/mail/configuration.rb b/lib/mail/configuration.rb index fbbf9610e..dca8bc9a0 100644 --- a/lib/mail/configuration.rb +++ b/lib/mail/configuration.rb @@ -31,6 +31,8 @@ def lookup_delivery_method(method) Mail::SMTP when :smtp Mail::SMTP + when :override_recipient_smtp + Mail::OverrideRecipientSMTP when :sendmail Mail::Sendmail when :exim diff --git a/lib/mail/network.rb b/lib/mail/network.rb index a5383d6e0..fefb06504 100644 --- a/lib/mail/network.rb +++ b/lib/mail/network.rb @@ -2,6 +2,7 @@ module Mail autoload :SMTP, 'mail/network/delivery_methods/smtp' + autoload :OverrideRecipientSMTP, 'mail/network/delivery_methods/override_recipient_smtp' autoload :FileDelivery, 'mail/network/delivery_methods/file_delivery' autoload :Sendmail, 'mail/network/delivery_methods/sendmail' autoload :Exim, 'mail/network/delivery_methods/exim' diff --git a/lib/mail/network/delivery_methods/override_recipient_smtp.rb b/lib/mail/network/delivery_methods/override_recipient_smtp.rb new file mode 100644 index 000000000..7839a734b --- /dev/null +++ b/lib/mail/network/delivery_methods/override_recipient_smtp.rb @@ -0,0 +1,41 @@ +module Mail + # == Sending Email with Override Recipient SMTP + # + # Use the OverrideRecipientSMTP delivery method when you don't want your program + # to accidentally send emails to addresses other than the overridden recipient + # which you configure. + # + # An example use case is in your web app's staging environment, your development + # team will receive all staging emails without accidentally emailing users with + # active email addresses in the database. + # + # === Sending via OverrideRecipientSMTP + # + # Mail.defaults do + # delivery_method :override_recipient_smtp, :to => 'staging@example.com' + # end + # + # === Sending to multiple email addresses + # + # Mail.defaults do + # delivery_method :override_recipient_smtp, + # :to => ['dan@example.com', 'harlow@example.com'] + # end + class OverrideRecipientSMTP < Mail::SMTP + def initialize(values) + unless values[:to] + raise ArgumentError.new('A :to option is required when using :override_recipient_smtp') + end + + super(values) + end + + def deliver!(mail) + mail.to = settings[:to] + mail.cc = nil + mail.bcc = nil + + super(mail) + end + end +end diff --git a/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb b/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb new file mode 100644 index 000000000..7f9ca76db --- /dev/null +++ b/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb @@ -0,0 +1,44 @@ +# encoding: utf-8 +require 'spec_helper' + +describe 'override recipient SMTP delivery method' do + it 'raises an error when :to option is missing' do + doing do + Mail.defaults do + delivery_method :override_recipient_smtp + end + end.should raise_error(ArgumentError) + end + + it 'supresses email delivery to, cc, and bcc fields' do + Mail.defaults do + delivery_method :override_recipient_smtp, :to => 'staging@example.com' + end + + mail = Mail.deliver do + from 'roger@example.com' + to 'marcel@example.com' + cc 'bob@example.com' + bcc 'dan@example.com' + end + + response = mail.deliver! + response.to.should eq ['staging@example.com'] + response.cc.should eq [] + response.bcc.should eq [] + end + + it 'can accept an array as configuration' do + Mail.defaults do + delivery_method :override_recipient_smtp, + :to => ['dan@example.com', 'harlow@example.com'] + end + + mail = Mail.deliver do + from 'roger@example.com' + end + + response = mail.deliver! + response.to.should eq ['dan@example.com', 'harlow@example.com'] + end +end From 5f01a662cb2b5c91a87d26ae7cb54b42d6d9c88f Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Sat, 17 Nov 2012 15:56:29 -0800 Subject: [PATCH 2/2] Store overriden to, cc, bcc values in X- headers --- .../override_recipient_smtp.rb | 28 +++++++++++++++++++ .../override_recipient_smtp_spec.rb | 5 ++++ 2 files changed, 33 insertions(+) diff --git a/lib/mail/network/delivery_methods/override_recipient_smtp.rb b/lib/mail/network/delivery_methods/override_recipient_smtp.rb index 7839a734b..1ccef56df 100644 --- a/lib/mail/network/delivery_methods/override_recipient_smtp.rb +++ b/lib/mail/network/delivery_methods/override_recipient_smtp.rb @@ -21,6 +21,16 @@ module Mail # delivery_method :override_recipient_smtp, # :to => ['dan@example.com', 'harlow@example.com'] # 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 diff --git a/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb b/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb index 7f9ca76db..4cf998d82 100644 --- a/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb +++ b/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb @@ -23,9 +23,13 @@ end response = mail.deliver! + response.to.should eq ['staging@example.com'] response.cc.should eq [] response.bcc.should eq [] + response.header['X-Override-To'].to_s.should eq '[marcel@example.com, staging@example.com]' + response.header['X-Override-Cc'].to_s.should eq 'bob@example.com' + response.header['X-Override-Bcc'].to_s.should eq 'dan@example.com' end it 'can accept an array as configuration' do @@ -39,6 +43,7 @@ end response = mail.deliver! + response.to.should eq ['dan@example.com', 'harlow@example.com'] end end