Browse Source

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

Co-authored-by: Florian Liebe <fl@zammad.com>
Timo Triebensky 6 months ago
parent
commit
65d3bed8c8
3 changed files with 32 additions and 2 deletions
  1. 11 1
      app/models/channel/email_parser.rb
  2. 5 1
      i18n/zammad.pot
  3. 16 0
      spec/models/channel/email_parser_spec.rb

+ 11 - 1
app/models/channel/email_parser.rb

@@ -87,11 +87,21 @@ class Channel::EmailParser
 
     headers = message_header_hash(mail)
     body = message_body_hash(mail)
+    sender_attributes = self.class.sender_attributes(headers)
+
+    if 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(', ')
+
+      raise Exceptions::MissingAttribute.new('email', msg)
+    end
+
     message_attributes = [
       { mail_instance: mail },
       headers,
       body,
-      self.class.sender_attributes(headers),
+      sender_attributes,
       { raw: msg },
     ]
     message_attributes.reduce({}.with_indifferent_access, &:merge)

+ 5 - 1
i18n/zammad.pot

@@ -3295,6 +3295,10 @@ msgstr ""
 msgid "Could not load the two-factor authentication configuration for this user."
 msgstr ""
 
+#: app/models/channel/email_parser.rb:32
+msgid "Could not parse any sender attribute from the email. Checked fields:"
+msgstr ""
+
 #: app/assets/javascripts/app/controllers/signup.coffee:133
 msgid "Could not process your request"
 msgstr ""
@@ -9737,7 +9741,7 @@ msgstr ""
 msgid "No webhook available, please create a new one or activate an existing one at \"Manage > Webhook\""
 msgstr ""
 
-#: app/models/channel/email_parser.rb:108
+#: app/models/channel/email_parser.rb:118
 msgid "No x-zammad-session-user-id, no sender set!"
 msgstr ""
 

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

@@ -23,6 +23,22 @@ RSpec.describe Channel::EmailParser, type: :model do
       end
     end
 
+    describe 'when mail does not contain any sender specification' do
+      subject(:instance) { described_class.new }
+
+      let(:raw_mail) { <<~RAW.chomp }
+        To: baz@qux.net
+        Subject: Foo
+
+        Lorem ipsum dolor
+      RAW
+
+      it 'raises error even if exception is false' do
+        expect { described_class.new.parse(raw_mail) }
+          .to raise_error(Exceptions::MissingAttribute, 'Could not parse any sender attribute from the email. Checked fields: From, Reply-To, Return-Path, Sender')
+      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)