FIX: Capture CC addresses for forwarded emails (#14254)

When forwarding emails into the group inbox, we now use the
original sender email as the from_address since
2ac9fd9dff. However, we have not
been saving the original CC addresses of the forwarded email,
which are needed to include those recipients in on the conversation
when replying via the group inbox.

This commit captures the CC addresses on the incoming email, and
makes sure the emails are created as staged users and added to the
list of topic allowed users so they are included on CC's sent by
the GroupSmtpEmail and other jobs.
This commit is contained in:
Martin Brennan 2021-09-07 08:46:28 +10:00 committed by GitHub
parent 193da4c3b4
commit 9f36d8ad43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 11 deletions

View File

@ -143,13 +143,17 @@ module Email
end end
def create_incoming_email def create_incoming_email
cc_addresses = Array.wrap(@mail.cc)
if has_been_forwarded? && embedded_email&.cc
cc_addresses.concat(embedded_email.cc)
end
IncomingEmail.create( IncomingEmail.create(
message_id: @message_id, message_id: @message_id,
raw: Email::Cleaner.new(@raw_email).execute, raw: Email::Cleaner.new(@raw_email).execute,
subject: subject, subject: subject,
from_address: @from_email, from_address: @from_email,
to_addresses: @mail.to, to_addresses: @mail.to,
cc_addresses: @mail.cc, cc_addresses: cc_addresses,
imap_uid_validity: @opts[:imap_uid_validity], imap_uid_validity: @opts[:imap_uid_validity],
imap_uid: @opts[:imap_uid], imap_uid: @opts[:imap_uid],
imap_group_id: @opts[:imap_group_id], imap_group_id: @opts[:imap_group_id],
@ -956,7 +960,7 @@ module Email
email, display_name = parse_from_field(embedded) email, display_name = parse_from_field(embedded)
if forwarded_by_address && forwarded_by_name 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 end
return false if email.blank? || !email["@"] return false if email.blank? || !email["@"]
@ -985,10 +989,10 @@ module Email
post_type: post_type, post_type: post_type,
skip_validations: user.staged?) skip_validations: user.staged?)
else else
if forwarded_by_user if @forwarded_by_user
post.topic.topic_allowed_users.find_or_create_by!(user_id: forwarded_by_user.id) post.topic.topic_allowed_users.find_or_create_by!(user_id: @forwarded_by_user.id)
end end
post.topic.add_small_action(forwarded_by_user || user, "forwarded") post.topic.add_small_action(@forwarded_by_user || user, "forwarded")
end end
end end
@ -1324,7 +1328,11 @@ module Email
if result.post if result.post
@incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id) @incoming_email.update_columns(topic_id: result.post.topic_id, post_id: result.post.id)
if result.post.topic&.private_message? && !is_bounce? 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 end
# Alert the people involved in the topic now that the incoming email # Alert the people involved in the topic now that the incoming email
@ -1346,11 +1354,11 @@ module Email
html html
end end
def add_other_addresses(post, sender) def add_other_addresses(post, sender, mail_object)
%i(to cc bcc).each do |d| %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 begin
address_field.decoded address_field.decoded
email = address_field.address.downcase email = address_field.address.downcase

View File

@ -1064,13 +1064,40 @@ describe Email::Receiver do
end end
it "does not say the email was forwarded by the original sender, it says the email is forwarded by the group" do 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_inbox) }.to change { User.where(staged: true).count }.by(2) expect { process(:forwarded_by_group_to_inbox) }.to change { User.where(staged: true).count }.by(4)
topic = Topic.last topic = Topic.last
forwarded_small_post = topic.ordered_posts.last forwarded_small_post = topic.ordered_posts.last
expect(forwarded_small_post.action_code).to eq("forwarded") expect(forwarded_small_post.action_code).to eq("forwarded")
expect(forwarded_small_post.user).to eq(User.find_by_email("team@somesmtpaddress.com")) expect(forwarded_small_post.user).to eq(User.find_by_email("team@somesmtpaddress.com"))
end end
it "keeps track of the cc addresses of the forwarded email and creates staged users for them" do
expect { process(:forwarded_by_group_to_inbox) }.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@ccland.com")
fred_user = User.find_by_email("fred@bedrock.com")
team_user = User.find_by_email("team@somesmtpaddress.com")
expect(topic.incoming_email.first.cc_addresses).to eq("terry@ccland.com;don@ccland.com")
expect(topic.topic_allowed_users.pluck(:user_id)).to match_array([
fred_user.id, team_user.id, cc_user1.id, cc_user2.id
])
end
it "keeps track of the cc addresses of the final forwarded email as well" do
expect { process(:forwarded_by_group_to_inbox_double_cc) }.to change { User.where(staged: true).count }.by(5)
topic = Topic.last
cc_user1 = User.find_by_email("terry@ccland.com")
cc_user2 = User.find_by_email("don@ccland.com")
fred_user = User.find_by_email("fred@bedrock.com")
team_user = User.find_by_email("team@somesmtpaddress.com")
someother_user = User.find_by_email("someotherparty@test.com")
expect(topic.incoming_email.first.cc_addresses).to eq("someotherparty@test.com;terry@ccland.com;don@ccland.com")
expect(topic.topic_allowed_users.pluck(:user_id)).to match_array([
fred_user.id, team_user.id, someother_user.id, cc_user1.id, cc_user2.id
])
end
context "when staged user for the team email already exists" do context "when staged user for the team email already exists" do
let!(:staged_team_user) do let!(:staged_team_user) do
User.create!( User.create!(
@ -1082,7 +1109,7 @@ describe Email::Receiver do
end end
it "uses that and does not create another staged user" do it "uses that and does not create another staged user" do
expect { process(:forwarded_by_group_to_inbox) }.to change { User.where(staged: true).count }.by(1) expect { process(:forwarded_by_group_to_inbox) }.to change { User.where(staged: true).count }.by(3)
topic = Topic.last topic = Topic.last
forwarded_small_post = topic.ordered_posts.last forwarded_small_post = topic.ordered_posts.last
expect(forwarded_small_post.action_code).to eq("forwarded") expect(forwarded_small_post.action_code).to eq("forwarded")

View File

@ -17,6 +17,7 @@ From: Fred Flintstone <fred@bedrock.com>
Date: Mon, 1 Dec 2016 13:37:42 +0100 Date: Mon, 1 Dec 2016 13:37:42 +0100
Subject: Re: Login problems Subject: Re: Login problems
To: Discourse Team <team@somesmtpaddress.com> To: Discourse Team <team@somesmtpaddress.com>
CC: Terry Jones <terry@ccland.com>, don@ccland.com
Hello I am having some issues with my forum. Hello I am having some issues with my forum.

View File

@ -0,0 +1,36 @@
Message-ID: <58@somesmtpaddress.mail>
From: Discourse Team <team@somesmtpaddress.com>
To: support+team@bar.com
CC: someotherparty@test.com
Date: Mon, 1 Dec 2016 13:37:42 +0100
Subject: Fwd: Login problems
Content-Type: multipart/related; boundary="00000000000072702105c89858de"
--00000000000072702105c89858de
Content-Type: multipart/alternative; boundary="00000000000072702005c89858dd"
--00000000000072702005c89858dd
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
---------- Forwarded message ---------
From: Fred Flintstone <fred@bedrock.com>
Date: Mon, 1 Dec 2016 13:37:42 +0100
Subject: Re: Login problems
To: Discourse Team <team@somesmtpaddress.com>
CC: Terry Jones <terry@ccland.com>, don@ccland.com
Hello I am having some issues with my forum.
Fred
--00000000000072702005c89858dd
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<p>Hello I am having some issues with my forum.</p>
<br>Fred<br>
--00000000000072702005c89858dd--
--00000000000072702105c89858de