Browse Source

Follow up 65d3bed8c8947c014ae192d83dc41158258fce24 - Fixes #4826 - Unprocessible mail output for mails with missing from should have a specific output

Timo Triebensky 6 months ago
parent
commit
4c3e6b7aab

+ 3 - 3
app/models/channel/email_parser.rb

@@ -15,7 +15,7 @@ class Channel::EmailParser
 =begin
 
   parser = Channel::EmailParser.new
-  mail = parser.parse(msg_as_string)
+  mail = parser.parse(msg_as_string, allow_missing_attribute_exceptions: true | false)
 
   mail = {
     from:              'Some Name <some@example.com>',
@@ -74,7 +74,7 @@ class Channel::EmailParser
 
 =end
 
-  def parse(msg)
+  def parse(msg, allow_missing_attribute_exceptions: true)
     msg = msg.force_encoding('binary')
     # mail 2.6 and earlier accepted non-conforming mails that lacked the correct CRLF seperators,
     # mail 2.7 and above require CRLF so we force it on using binary_unsafe_to_crlf
@@ -89,7 +89,7 @@ class Channel::EmailParser
     body = message_body_hash(mail)
     sender_attributes = self.class.sender_attributes(headers)
 
-    if sender_attributes.blank?
+    if allow_missing_attribute_exceptions && sender_attributes.blank?
       msg = __('Could not parse any sender attribute from the email. Checked fields:')
       msg += ' '
       msg += SENDER_FIELDS.map { |f| f.split('-').map(&:capitalize).join('-') }.join(', ')

+ 1 - 1
app/models/channel/filter/bounce_delivery_permanent_failed.rb

@@ -15,7 +15,7 @@ module Channel::Filter::BounceDeliveryPermanentFailed
       next if attachment[:preferences]['Mime-Type'] != 'message/rfc822'
       next if !attachment[:data]
 
-      result = Channel::EmailParser.new.parse(attachment[:data])
+      result = Channel::EmailParser.new.parse(attachment[:data], allow_missing_attribute_exceptions: false)
       next if !result[:message_id]
 
       message_id_md5 = Digest::MD5.hexdigest(result[:message_id])

+ 1 - 1
app/models/channel/filter/bounce_follow_up_check.rb

@@ -14,7 +14,7 @@ module Channel::Filter::BounceFollowUpCheck
       next if attachment[:preferences]['Mime-Type'] != 'message/rfc822'
       next if !attachment[:data]
 
-      result = Channel::EmailParser.new.parse(attachment[:data])
+      result = Channel::EmailParser.new.parse(attachment[:data], allow_missing_attribute_exceptions: false)
       next if !result[:message_id]
 
       message_id_md5 = Digest::MD5.hexdigest(result[:message_id])

+ 16 - 0
spec/models/channel/email_parser_spec.rb

@@ -39,6 +39,22 @@ RSpec.describe Channel::EmailParser, type: :model do
       end
     end
 
+    describe 'when mail does not contain any sender specification with disabled missing attribute exceptions' do
+      subject(:instance) { described_class.new }
+
+      let(:raw_mail) { <<~RAW.chomp }
+        To: baz@qux.net
+        Subject: Foo
+
+        Lorem ipsum dolor
+      RAW
+
+      it 'prevents raising an error' do
+        expect { described_class.new.parse(raw_mail, allow_missing_attribute_exceptions: false) }
+          .not_to raise_error
+      end
+    end
+
     # To write new .yml files for emails you can use the following code:
     #
     # File.write('test/data/mail/mailXXX.yml', Channel::EmailParser.new.parse(File.read('test/data/mail/mailXXX.box')).slice(:from, :from_email, :from_display_name, :to, :cc, :subject, :body, :content_type, :'reply-to', :attachments).to_yaml)