From e469cbaeed97ab4186f992aed8bd3027bac86efe Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 6 Sep 2021 11:22:00 +1000 Subject: [PATCH] FIX: Make sure CC addresses for forwarded emails are used When forwarding emails into the group inbox we are currently ignoring the original CC addresses on the forwarded email. This commit makes sure we record those CC addresses on the incoming email record for the topic, as well as inviting those CC addresses as staged users on the topic when it is created. --- lib/email/receiver.rb | 26 ++++++++++++------- spec/components/email/receiver_spec.rb | 15 +++++++++-- .../emails/forwarded_by_group_to_group.eml | 1 + 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index adf07cd182..f77d359a8a 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -143,13 +143,17 @@ module Email end def create_incoming_email + cc_addresses = Array.wrap(@mail.cc) + if embedded_email&.cc + cc_addresses.concat(embedded_email.cc) + end IncomingEmail.create( message_id: @message_id, raw: Email::Cleaner.new(@raw_email).execute, subject: subject, from_address: @from_email, to_addresses: @mail.to, - cc_addresses: @mail.cc, + cc_addresses: cc_addresses, imap_uid_validity: @opts[:imap_uid_validity], imap_uid: @opts[:imap_uid], imap_group_id: @opts[:imap_group_id], @@ -959,7 +963,7 @@ module Email email, display_name = parse_from_field(embedded) if @forwarded_by_address && @forwarded_by_name - forwarded_by_user = stage_sender_user(@forwarded_by_address, @forwarded_by_name) + @forwarded_by_user = stage_sender_user(@forwarded_by_address, @forwarded_by_name) end return false if email.blank? || !email["@"] @@ -988,10 +992,10 @@ module Email post_type: post_type, skip_validations: user.staged?) else - if forwarded_by_user - post.topic.topic_allowed_users.find_or_create_by!(user_id: forwarded_by_user.id) + if @forwarded_by_user + post.topic.topic_allowed_users.find_or_create_by!(user_id: @forwarded_by_user.id) end - post.topic.add_small_action(forwarded_by_user || user, "forwarded") + post.topic.add_small_action(@forwarded_by_user || user, "forwarded") end end @@ -1315,7 +1319,11 @@ module Email if result.post @incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id) if result.post.topic&.private_message? && !is_bounce? - add_other_addresses(result.post, user) + add_other_addresses(result.post, user, @mail) + + if has_been_forwarded? + add_other_addresses(result.post, @forwarded_by_user || user, embedded_email) + end end # Alert the people involved in the topic now that the incoming email @@ -1337,11 +1345,11 @@ module Email html end - def add_other_addresses(post, sender) + def add_other_addresses(post, sender, mail_object) %i(to cc bcc).each do |d| - next if @mail[d].blank? + next if mail_object[d].blank? - @mail[d].each do |address_field| + mail_object[d].each do |address_field| begin address_field.decoded email = address_field.address.downcase diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 777ac2ec71..511992d3b8 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -1064,13 +1064,24 @@ describe Email::Receiver do end it "does not say the email was forwarded by the original sender, it says the email is forwarded by the group" do - expect { process(:forwarded_by_group_to_group) }.to change { User.where(staged: true).count }.by(2) + expect { process(:forwarded_by_group_to_group) }.to change { User.where(staged: true).count }.by(4) topic = Topic.last forwarded_small_post = topic.ordered_posts.last expect(forwarded_small_post.action_code).to eq("forwarded") expect(forwarded_small_post.user).to eq(User.find_by_email("team@somesmtpaddress.com")) end + it "keeps the CC addresses from the forwarded email" do + expect { process(:forwarded_by_group_to_group) }.to change { User.where(staged: true).count }.by(4) + topic = Topic.last + cc_user1 = User.find_by_email("terry@ccland.com") + cc_user2 = User.find_by_email("don@mmtest.com") + expect(cc_user1).not_to eq(nil) + expect(cc_user2).not_to eq(nil) + expect(topic.topic_allowed_users.pluck(:user_id)).to include(cc_user1.id) + expect(topic.incoming_email.first.cc_addresses).to eq("terry@ccland.com;don@mmtest.com") + end + context "when staged user for the team email already exists" do let!(:staged_team_user) do User.create!( @@ -1082,7 +1093,7 @@ describe Email::Receiver do end it "uses that and does not create another staged user" do - expect { process(:forwarded_by_group_to_group) }.to change { User.where(staged: true).count }.by(1) + expect { process(:forwarded_by_group_to_group) }.to change { User.where(staged: true).count }.by(3) topic = Topic.last forwarded_small_post = topic.ordered_posts.last expect(forwarded_small_post.action_code).to eq("forwarded") diff --git a/spec/fixtures/emails/forwarded_by_group_to_group.eml b/spec/fixtures/emails/forwarded_by_group_to_group.eml index fe170e5d49..9efbc1ea6f 100644 --- a/spec/fixtures/emails/forwarded_by_group_to_group.eml +++ b/spec/fixtures/emails/forwarded_by_group_to_group.eml @@ -17,6 +17,7 @@ From: Fred Flintstone Date: Mon, 1 Dec 2016 13:37:42 +0100 Subject: Re: Login problems To: Discourse Team +CC: Terry Jones , don@mmtest.com Hello I am having some issues with my forum.