FIX: Strip Auto-Submitted email header from group SMTP emails (#15057)

FIX: Strip Auto-Submitted email header from group SMTP emails (#15057)

Remove Auto-Submitted header for group private message emails, it does not make sense there and may hurt deliverability.

From https://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml:

Indicates that a message was generated by an automatic process, and is not a direct response to another message.

diff --git a/lib/email/sender.rb b/lib/email/sender.rb
index f6547a4..cf2c98b 100644
--- a/lib/email/sender.rb
+++ b/lib/email/sender.rb
@@ -213,9 +213,20 @@ module Email
         @message.header[Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER] = nil
       end
 
-      # Replace reply_key in custom headers or remove
       MessageBuilder.custom_headers(SiteSetting.email_custom_headers).each do |key, _|
         value = header_value(key)
+
+        # Remove Auto-Submitted header for group private message emails, it does
+        # not make sense there and may hurt deliverability.
+        #
+        # From https://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml:
+        #
+        # > Indicates that a message was generated by an automatic process, and is not a direct response to another message.
+        if key.downcase == "auto-submitted" && smtp_group_id
+          @message.header[key] = nil
+        end
+
+        # Replace reply_key in custom headers or remove
         if value&.include?('%{reply_key}')
           # Delete old header first or else the same header will be added twice
           @message.header[key] = nil
diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb
index 783b70c..eedb7e8 100644
--- a/spec/components/email/sender_spec.rb
+++ b/spec/components/email/sender_spec.rb
@@ -414,6 +414,13 @@ describe Email::Sender do
           expect(message.header['Precedence']).to eq(nil)
           expect(message.header['List-Unsubscribe']).to eq(nil)
         end
+
+        it "removes the Auto-Submitted header" do
+          TopicAllowedGroup.create!(topic: post.topic, group: group)
+          email_sender.send
+
+          expect(message.header['Auto-Submitted']).to eq(nil)
+        end
       end
     end
 

GitHub sha: 44be79f095268bfe11856ee4132197a6ab595fe0

This commit appears in #15057 which was approved by tgxworld. It was merged by martin.