Skip to content

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

FIX: Prioritize VERP key over final_recipient header in bounced email

David Taylor0644c10bfefe
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 68a5fa817c6..2f5bc9c806b 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -513,13 +513,13 @@ module Email
def parse_from_field(mail = nil)
mail ||= @mail
- if mail.bounced?
+ if email_log.present?
+ email = email_log.to_address || email_log.user&.email
+ return [email, email_log.user&.username]
+ elsif mail.bounced?
Array.wrap(mail.final_recipient).each do |from|
return extract_from_address_and_name(from)
end
- elsif email_log.present?
- email = email_log.user&.email || email_log.to_address
- return [email, email_log.user&.username]
end
return unless mail[:from]
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index d3a89acb13b..71d57a8f6d2 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -212,6 +212,14 @@ describe Email::Receiver do
expect(email_log_2.bounced).to eq(true)
end
+ it "works when the final recipient is different" do
+ expect { process(:verp_bounce_different_final_recipient) }.to raise_error(Email::Receiver::BouncedEmailError)
+
+ email_log.reload
+ expect(email_log.bounced).to eq(true)
+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
+ end
+
it "sends a system message once they reach the 'bounce_score_threshold'" do
expect(user.active).to eq(true)
diff --git a/spec/fixtures/emails/verp_bounce_different_final_recipient.eml b/spec/fixtures/emails/verp_bounce_different_final_recipient.eml
new file mode 100644
index 00000000000..cc6e4480f79
--- /dev/null
+++ b/spec/fixtures/emails/verp_bounce_different_final_recipient.eml
@@ -0,0 +1,39 @@
+Delivered-To: [email protected]
+Date: Thu, 7 Apr 2016 19:04:30 +0900 (JST)
+From: [email protected] (Mail Delivery System)
+Subject: Undelivered Mail Returned to Sender
+MIME-Version: 1.0
+Content-Type: multipart/report; report-type=delivery-status;
+ boundary="18F5D18A0075.1460023470/[email protected]"
+
+This is a MIME-encapsulated message.
+
+--18F5D18A0075.1460023470/[email protected]
+Content-Description: Notification
+Content-Type: text/plain; charset=us-ascii
+
+Your email bounced
+
+--18F5D18A0075.1460023470/[email protected]
+Content-Description: Delivery report
+Content-Type: message/delivery-status
+
+Final-Recipient: rfc822; [email protected]
+Original-Recipient: rfc822;[email protected]
+Action: failed
+Status: 4.1.1
+Diagnostic-Code: X-Postfix; unknown user: "linux-admin"
+
+--18F5D18A0075.1460023470/[email protected]
+Content-Description: Undelivered Message
+Content-Type: message/rfc822
+
+Return-Path: <[email protected]>
+Date: Thu, 07 Apr 2016 03:04:28 -0700 (PDT)
+X-Discourse-Auto-Generated: marked
+
+This is the body
+
+--18F5D18A0075.1460023470/[email protected]