Skip to content

Commit

Permalink
Add support for recipient lists
Browse files Browse the repository at this point in the history
  • Loading branch information
dgoerlich committed Apr 6, 2016
1 parent 8d45c29 commit aab8d8e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
31 changes: 20 additions & 11 deletions lib/sparkpost_rails/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def deliver!(mail)

sparkpost_data = find_sparkpost_data_from mail

prepare_recipients_from mail
prepare_recipients_from mail, sparkpost_data

if sparkpost_data.has_key?(:template_id)
prepare_template_content_from sparkpost_data
Expand All @@ -22,7 +22,7 @@ def deliver!(mail)
prepare_reply_to_address_from mail

prepare_subject_from mail
prepare_cc_headers_from mail
prepare_cc_headers_from mail, sparkpost_data
prepare_inline_content_from mail
prepare_attachments_from mail
end
Expand All @@ -45,14 +45,21 @@ def find_sparkpost_data_from mail
end
end

def prepare_recipients_from mail
@data[:recipients] = prepare_addresses(mail.to, mail[:to].display_names)
if !mail.cc.nil?
@data[:recipients] += prepare_copy_addresses(mail.cc, mail[:cc].display_names, mail.to.first).flatten
end
if !mail.bcc.nil?
@data[:recipients] += prepare_copy_addresses(mail.bcc, mail[:bcc].display_names, mail.to.first).flatten
def prepare_recipients_from mail, sparkpost_data
if sparkpost_data.has_key?(:recipient_list_id)
@data[:recipients] = {list_id: sparkpost_data[:recipient_list_id]}
else
@data[:recipients] = prepare_addresses(mail.to, mail[:to].display_names)

if !mail.cc.nil?
@data[:recipients] += prepare_copy_addresses(mail.cc, mail[:cc].display_names, mail.to.first).flatten
end

if !mail.bcc.nil?
@data[:recipients] += prepare_copy_addresses(mail.bcc, mail[:bcc].display_names, mail.to.first).flatten
end
end

end

def prepare_addresses emails, names
Expand Down Expand Up @@ -116,13 +123,15 @@ def prepare_subject_from mail
@data[:content][:subject] = mail.subject
end

def prepare_cc_headers_from mail
if !mail[:cc].nil?
def prepare_cc_headers_from mail, sparkpost_data
if !mail[:cc].nil? && !sparkpost_data.has_key?(:recipient_list_id)
copies = prepare_addresses(mail.cc, mail[:cc].display_names)
emails = []

copies.each do |copy|
emails << copy[:address][:email]
end

@data[:content][:headers] = { cc: emails }
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/bcc_recipients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
it "handles email only" do
test_email = Mailer.test_email bcc: "[email protected], [email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content]).not_to include(:headers)
end

it "handles name and email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>", bcc: "Brock Test <[email protected]>, Brack Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]", name: "Brock Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Brack Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content]).not_to include(:headers)
end
Expand All @@ -56,23 +56,23 @@
it "handles email only" do
test_email = Mailer.test_email to: "[email protected], [email protected]", cc: "[email protected]", bcc: "[email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]"}}, {address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]"]})
end

it "handles name and email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>, Sam Test <[email protected]>", cc: "Carl Test <[email protected]>", bcc: "Brock Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]", name: "Sam Test"}}, {address: {email: "[email protected]", name: "Carl Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Brock Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]"]})
end

it "handles mix of email only and name/email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>, [email protected]", cc: "[email protected], Chris Test <[email protected]>", bcc: "Brock Test <[email protected]>, [email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Chris Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Brock Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end
Expand Down
14 changes: 7 additions & 7 deletions spec/cc_recipients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
it "handles email only" do
test_email = Mailer.test_email cc: "[email protected], [email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end

it "handles name and email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>", cc: "Carl Test <[email protected]>, Chris Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]", name: "Carl Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Chris Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end
Expand All @@ -55,15 +55,15 @@
it "handles email only" do
test_email = Mailer.test_email to: "[email protected], [email protected]", cc: "[email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]"}}, {address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]"]})
end

it "handles name and email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>, Sam Test <[email protected]>", cc: "Carl Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]", name: "Sam Test"}}, {address: {email: "[email protected]", name: "Carl Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]"]})
end
Expand All @@ -73,23 +73,23 @@
it "handles email only" do
test_email = Mailer.test_email to: "[email protected], [email protected]", cc: "[email protected], [email protected]"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]"}}, {address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end

it "handles name and email" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>, Sam Test <[email protected]>", cc: "Carl Test <[email protected]>, Chris Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]", name: "Sam Test"}}, {address: {email: "[email protected]", name: "Carl Test", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Chris Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end

it "handles mix of email only and name/email for to recipients" do
test_email = Mailer.test_email to: "Joe Test <[email protected]>, [email protected]", cc: "[email protected], Chris Test <[email protected]>"
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq([{address: {email: "[email protected]", name: "Joe Test"}}, {address: {email: "[email protected]"}}, {address: {email: "[email protected]", header_to: "[email protected]"}}, {address: {email: "[email protected]", name: "Chris Test", header_to: "[email protected]"}}])
expect(@delivery_method.data[:content][:headers]).to eq({cc: ["[email protected]", "[email protected]"]})
end
Expand Down
34 changes: 34 additions & 0 deletions spec/recipients_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe SparkPostRails::DeliveryMethod do

before(:each) do
@delivery_method = SparkPostRails::DeliveryMethod.new
end

context "Recipients List" do

it "handles a list_id" do
test_email = Mailer.test_email sparkpost_data: {recipient_list_id: "List1"}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
end

it "ignores any CC addresses" do
test_email = Mailer.test_email cc: "[email protected]", sparkpost_data: {recipient_list_id: "List1"}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
expect(@delivery_method.data[:content].has_key?(:headers)).to eq(false)
end

it "ignores any BCC addresses" do
test_email = Mailer.test_email bcc: "[email protected]", sparkpost_data: {recipient_list_id: "List1"}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:recipients]).to eq({list_id: "List1"})
end
end

end

0 comments on commit aab8d8e

Please sign in to comment.