Browse Source

Fixes #3870 - Text rendering does not work for e-mails (no new lines)

Mantas 3 years ago
parent
commit
be407c23da
3 changed files with 22 additions and 18 deletions
  1. 14 3
      app/models/channel/email_parser.rb
  2. 6 0
      test/data/mail/mail102.box
  3. 2 15
      test/data/mail/mail102.yml

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

@@ -607,6 +607,20 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again
     if mail.html_part&.body.present?
       content_type = mail.html_part.mime_type || 'text/plain'
       body = body_text(mail.html_part, strict_html: true)
+    elsif mail.text_part.present? && mail.all_parts.any? { |elem| elem.inline? && elem.content_type&.start_with?('image') }
+      content_type = 'text/html'
+
+      body = mail
+        .all_parts
+        .reduce('') do |memo, part|
+          if part.mime_type == 'text/plain' && !part.attachment?
+            memo += body_text(part, strict_html: false).text2html
+          elsif part.inline? && part.content_type&.start_with?('image')
+            memo += "<img src=\'cid:#{part.cid}\'>"
+          end
+
+          memo
+        end
     elsif mail.text_part.present?
       content_type = 'text/plain'
 
@@ -615,9 +629,6 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again
         .reduce('') do |memo, part|
           if part.mime_type == 'text/plain' && !part.attachment?
             memo += body_text(part, strict_html: false)
-          elsif part.inline? && part.content_type&.start_with?('image')
-            content_type = 'text/html'
-            memo += "<img src=\'cid:#{part.cid}\'>"
           end
 
           memo

+ 6 - 0
test/data/mail/mail102.box

@@ -29,6 +29,9 @@ Content-Type: text/plain;
 
 Text
 
+new line
+
+Test 123
 
 --Apple-Mail=_57B3D7B4-58AD-4E7A-9A4A-5053521E2862
 Content-Disposition: inline;
@@ -6646,5 +6649,8 @@ Content-Type: text/plain;
 
 Text
 
+Text line
+
+end
 
 --Apple-Mail=_57B3D7B4-58AD-4E7A-9A4A-5053521E2862--

+ 2 - 15
test/data/mail/mail102.yml

@@ -4,21 +4,8 @@ from_email: top@secret.tld
 from_display_name: Mr. Top Secret
 to: zammad@XXXXXXXXX.de
 subject: Text - Bild - Text - PDF - Text - Bild - Text
-body: |+
-  Text
-
-  <img src='cid:inline_attachment_placeholder_1'>
-
-  Text
-
-
-
-  Text
-
-  <img src='cid:inline_attachment_placeholder_2'>
-
-  Text
-
+body: Text<br><br>new line<br><br>Test 123<br><img src='cid:inline_attachment_placeholder_1'><br><br>Text<br><br><br><br>Text<br><br><img
+  src='cid:inline_attachment_placeholder_2'><br><br>Text<br><br>Text line<br><br>end<br>
 content_type: text/html
 attachments:
 - !ruby/hash:ActiveSupport::HashWithIndifferentAccess