Skip to content

A platform for community discussion. Free, open, simple.

FIX: Use original from address when forwarding to group inbox (#14114)

Martin Brennan2ac9fd9dff5a
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index f513a37f722..cba22085c89 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -584,6 +584,20 @@ module Email
return unless mail[:from]
+ # For forwarded emails, where the from address matches a group incoming
+ # email, we want to use the from address of the original email sender,
+ # which we can extract from embedded_email_raw.
+ if has_been_forwarded?
+ if mail[:from].to_s =~ group_incoming_emails_regex && embedded_email[:from].errors.blank?
+ embedded_email[:from].each do |address_field|
+ from_address = address_field.address
+ from_display_name = address_field.display_name&.to_s
+ next if !from_address&.include?("@")
+ return [from_address&.downcase, from_display_name&.strip]
+ end
+ end
+ end
+
# For now we are only using the Reply-To header if the email has
# been forwarded via Google Groups, which is why we are checking the
# X-Original-From header too. In future we may want to use the Reply-To
@@ -885,6 +899,10 @@ module Email
@embedded_email_raw
end
+ def embedded_email
+ @embedded_email ||= embedded_email_raw.present? ? Mail.new(embedded_email_raw) : nil
+ end
+
def process_forwarded_email(destination, user)
user ||= stage_from_user
case SiteSetting.forwarded_emails_behaviour
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index d14b01b1d51..cef30c146dc 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -1032,10 +1032,31 @@ describe Email::Receiver do
end
end
+ context "when a group forwards an email to its inbox" do
+ let!(:topic) do
+ group.update!(
+ email_username: "[email protected]",
+ smtp_server: "smtp.test.com",
+ smtp_port: 587,
+ smtp_ssl: true,
+ smtp_enabled: true
+ )
+ process(:forwarded_by_group_to_group)
+ Topic.last
+ end
+
+ it "does not use the team's address as the from_address; it uses the original sender address" do
+ expect(topic.incoming_email.first.to_addresses).to include("[email protected]")
+ expect(topic.incoming_email.first.from_address).to eq("[email protected]")
+ end
+ end
+
context "emailing a group by email_username and following reply flow" do
let!(:original_inbound_email_topic) do
group.update!(
email_username: "[email protected]",
smtp_server: "smtp.test.com",
smtp_port: 587,
smtp_ssl: true,
diff --git a/spec/fixtures/emails/forwarded_by_group_to_group.eml b/spec/fixtures/emails/forwarded_by_group_to_group.eml
new file mode 100644
index 00000000000..fe170e5d490
--- /dev/null
+++ b/spec/fixtures/emails/forwarded_by_group_to_group.eml
@@ -0,0 +1,34 @@
+Message-ID: <[email protected]>
+From: Discourse Team <[email protected]>
+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 <[email protected]>
+Date: Mon, 1 Dec 2016 13:37:42 +0100
+Subject: Re: Login problems
+To: Discourse Team <[email protected]>
+
+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