A platform for community discussion. Free, open, simple.
FIX: Use reply-to address for incoming emails if present (#13896)
Martin Brennanb88d8c889432
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rbindex a6da67cbaa1..a6db1e27638 100644--- a/lib/email/receiver.rb+++ b/lib/email/receiver.rb@@ -570,11 +570,28 @@ module Emailreturn unless mail[:from]+ # 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+ # header in more cases.+ if mail['X-Original-From'].present?+ if mail[:reply_to] && mail[:reply_to].errors.blank?+ mail[:reply_to].each do |address_field|+ from_address = address_field.address+ from_display_name = address_field.display_name&.to_s+ next if address_field.to_s != mail['X-Original-From'].to_s+ next if !from_address&.include?("@")+ return [from_address&.downcase, from_display_name&.strip]+ end+ end+ end+if mail[:from].errors.blank?mail[:from].each do |address_field|from_address = address_field.address- from_display_name = address_field.display_name.try(:to_s)- return [from_address&.downcase, from_display_name&.strip] if from_address["@"]+ from_display_name = address_field.display_name&.to_s+ next if !from_address&.include?("@")+ return [from_address&.downcase, from_display_name&.strip]endenddiff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rbindex 79e14445bcb..4f7820daac9 100644--- a/spec/components/email/receiver_spec.rb+++ b/spec/components/email/receiver_spec.rb@@ -854,6 +854,38 @@ describe Email::Receiver doexpect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])end+ describe "reply-to header" do+ it "handles emails where there is a reply-to address, using that instead of the from address" do+ SiteSetting.block_auto_generated_emails = false+ expect { process(:reply_to_different_to_from) }.to change(Topic, :count)+ user = User.last+ incoming = IncomingEmail.find_by(message_id: "[email protected]")+ topic = incoming.topic+ expect(incoming.from_address).to eq("[email protected]")+ expect(user.email).to eq("[email protected]")+ end++ it "does not use the reply-to address if an X-Original-From header is not present" do+ SiteSetting.block_auto_generated_emails = false+ expect { process(:reply_to_different_to_from_no_x_original) }.to change(Topic, :count)+ user = User.last+ incoming = IncomingEmail.find_by(message_id: "[email protected]")+ topic = incoming.topic+ expect(incoming.from_address).to eq("[email protected]")+ expect(user.email).to eq("[email protected]")+ end++ it "does not use the reply-to address if the X-Original-From header is different from the reply-to address" do+ SiteSetting.block_auto_generated_emails = false+ expect { process(:reply_to_different_to_from_x_original_different) }.to change(Topic, :count)+ user = User.last+ incoming = IncomingEmail.find_by(message_id: "[email protected]")+ topic = incoming.topic+ expect(incoming.from_address).to eq("[email protected]")+ expect(user.email).to eq("[email protected]")+ end+ end+describe "when 'find_related_post_with_key' is disabled" dobefore doSiteSetting.find_related_post_with_key = falsediff --git a/spec/fixtures/emails/reply_to_different_to_from.eml b/spec/fixtures/emails/reply_to_different_to_from.emlnew file mode 100644index 00000000000..91cd93810fc--- /dev/null+++ b/spec/fixtures/emails/reply_to_different_to_from.eml@@ -0,0 +1,16 @@+From: 'Arthur Morgan' via Western Support <[email protected]>+Reply-To: Arthur Morgan <[email protected]>+To: [email protected]+Subject: I need some support+Date: Fri, 15 Jan 2016 00:12:43 +0100+Message-ID: <[email protected]>+Mime-Version: 1.0+Content-Type: text/plain+Content-Transfer-Encoding: 7bit+X-Original-From: Arthur Morgan <[email protected]>+Precedence: list+Mailing-list: list [email protected]; contact+List-ID: <[email protected]>++You sir, are a fish.diff --git a/spec/fixtures/emails/reply_to_different_to_from_no_x_original.eml b/spec/fixtures/emails/reply_to_different_to_from_no_x_original.emlnew file mode 100644index 00000000000..c1202b01016--- /dev/null+++ b/spec/fixtures/emails/reply_to_different_to_from_no_x_original.eml@@ -0,0 +1,11 @@+From: 'Arthur Morgan' via Western Support <[email protected]>+Reply-To: Arthur Morgan <[email protected]>+To: [email protected]+Subject: I need some support+Date: Fri, 15 Jan 2016 00:12:43 +0100+Message-ID: <[email protected]>+Mime-Version: 1.0+Content-Type: text/plain+Content-Transfer-Encoding: 7bit++You sir, are a fish.diff --git a/spec/fixtures/emails/reply_to_different_to_from_x_original_different.eml b/spec/fixtures/emails/reply_to_different_to_from_x_original_different.emlnew file mode 100644index 00000000000..209f3ed3b81--- /dev/null+++ b/spec/fixtures/emails/reply_to_different_to_from_x_original_different.eml@@ -0,0 +1,16 @@+From: 'Arthur Morgan' via Western Support <[email protected]>+Reply-To: Arthur Morgan <[email protected]>+To: [email protected]+Subject: I need some support+Date: Fri, 15 Jan 2016 00:12:43 +0100+Message-ID: <[email protected]>+Mime-Version: 1.0+Content-Type: text/plain+Content-Transfer-Encoding: 7bit+X-Original-From: Dutch Van der Linde <[email protected]>+Precedence: list+Mailing-list: list [email protected]; contact+List-ID: <[email protected]>++We need more money. I have a plan.