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..1ccef56df --- /dev/null +++ b/lib/mail/network/delivery_methods/override_recipient_smtp.rb @@ -0,0 +1,69 @@ +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 + # + # === 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] + raise ArgumentError.new('A :to option is required when using :override_recipient_smtp') + end + + super(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 new file mode 100644 index 000000000..4cf998d82 --- /dev/null +++ b/spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb @@ -0,0 +1,49 @@ +# 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 [] + 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 + 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