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 email and domain whitelist options #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ Optionally prefix the subject line:
subject_prefix: '[STAGING]'
)

Optionally white-list emails:

Mail.register_interceptor RecipientInterceptor.new(
ENV['EMAIL_RECIPIENTS'],
email_whitelist: ['[email protected]']
)

Optionally white-list domains:

Mail.register_interceptor RecipientInterceptor.new(
ENV['EMAIL_RECIPIENTS'],
domain_whitelist: ['example.com']
)

Note: If one or more of the original recipients match the email white-list or domain white-list,
the email's to field will not be overridden. However, non-white-listed emails will be removed from
the email's to field.

Credits
-------

Expand Down
26 changes: 25 additions & 1 deletion lib/recipient_interceptor.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
require 'mail'

class RecipientInterceptor

def initialize(recipients, options = {})
@recipients = normalize_to_array(recipients)
@email_whitelist = options[:email_whitelist] || []
@domain_whitelist = options[:domain_whitelist] || []
@subject_prefix = options[:subject_prefix]
end

def delivering_email(message)
add_custom_headers message
add_subject_prefix message
message.to = @recipients
message.to = sanitize_recipients(message)
message.cc = []
message.bcc = []
end
Expand Down Expand Up @@ -41,4 +44,25 @@ def add_custom_headers(message)
end
end
end

def sanitize_recipients(message)
recipients = message.to
sanitized_recipients = recipients.select { |email| whitelisted?(email) }
sanitized_recipients.any? ? sanitized_recipients : @recipients
end

def whitelisted?(email)
whitelisted_email?(email) || whitelisted_domain?(email)
end

def whitelisted_email?(email)
@email_whitelist.include?(email)
end

def whitelisted_domain?(email)
@domain_whitelist.any? do |domain|
email =~ /@#{domain}$/
end
end

end
104 changes: 98 additions & 6 deletions spec/recipient_interceptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,98 @@
expect(response.to).to eq [recipient_string]
end

context 'given an email white-list' do
context 'for emails with one recipient' do
it 'does not override the to field for a white-listed email' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
email_whitelist: ['[email protected]']
)

response = deliver_mail to: '[email protected]'
expect(response.to).to eq ['[email protected]']
end

it 'overrides the to field for a non-white-listed email' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
email_whitelist: ['[email protected]']
)

response = deliver_mail to: '[email protected]'
expect(response.to).to eq ['[email protected]']
end
end

context 'for emails with multiple recipients' do
it 'strips non-white-listed emails, leaving white-listed ones' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
email_whitelist: ['[email protected]']
)

response = deliver_mail to: ['[email protected]', '[email protected]']
expect(response.to).to eq ['[email protected]']
end

it 'replaces all recipients when none is white-listed' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
email_whitelist: ['[email protected]']
)

response = deliver_mail to: ['[email protected]', '[email protected]']
expect(response.to).to eq ['[email protected]']
end
end
end

context 'given an domain white-list' do
context 'for emails with one recipient' do
it 'does not override the to field for a white-listed email' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
domain_whitelist: ['whitelisted.com']
)

response = deliver_mail to: '[email protected]'
expect(response.to).to eq ['[email protected]']
end

it 'overrides the to field for a non-white-listed email' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
domain_whitelist: ['whitelisted.com']
)

response = deliver_mail to: '[email protected]'
expect(response.to).to eq ['[email protected]']
end
end

context 'for emails with multiple recipients' do
it 'strips non-white-listed emails, leaving white-listed ones' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
domain_whitelist: ['whitelisted.com']
)

response = deliver_mail to: ['[email protected]', '[email protected]', '[email protected]']
expect(response.to).to eq ['[email protected]', '[email protected]']
end

it 'replaces all recipients when none is white-listed' do
Mail.register_interceptor RecipientInterceptor.new(
'[email protected]',
domain_whitelist: ['whitelisted.com']
)

response = deliver_mail to: ['[email protected]', '[email protected]']
expect(response.to).to eq ['[email protected]']
end
end
end

it 'does not prefix subject by default' do
Mail.register_interceptor RecipientInterceptor.new(recipient_string)

Expand Down Expand Up @@ -67,17 +159,17 @@ def recipient_array
['[email protected]', '[email protected]']
end

def deliver_mail
def deliver_mail(options = {})
Mail.defaults do
delivery_method :test
end

Mail.deliver do
from '[email protected]'
to '[email protected]'
cc '[email protected]'
bcc '[email protected]'
subject 'some subject'
from options.fetch(:from, '[email protected]')
to options.fetch(:to, '[email protected]')
cc options.fetch(:cc, '[email protected]')
bcc options.fetch(:bcc, '[email protected]')
subject options.fetch(:subject, 'some subject')
end
end

Expand Down