Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OverrideRecipientSMTP delivery method #423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mail/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/mail/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
69 changes: 69 additions & 0 deletions lib/mail/network/delivery_methods/override_recipient_smtp.rb
Original file line number Diff line number Diff line change
@@ -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 => '[email protected]'
# end
#
# === Sending to multiple email addresses
#
# Mail.defaults do
# 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]
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
49 changes: 49 additions & 0 deletions spec/mail/network/delivery_methods/override_recipient_smtp_spec.rb
Original file line number Diff line number Diff line change
@@ -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 => '[email protected]'
end

mail = Mail.deliver do
from '[email protected]'
to '[email protected]'
cc '[email protected]'
bcc '[email protected]'
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
Mail.defaults do
delivery_method :override_recipient_smtp,
:to => ['[email protected]', '[email protected]']
end

mail = Mail.deliver do
from '[email protected]'
end

response = mail.deliver!

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