]> source.dussan.org Git - redmine.git/commitdiff
Emails with no text or html Content not handled properly (#25269, #25256).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 7 Mar 2017 17:54:09 +0000 (17:54 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 7 Mar 2017 17:54:09 +0000 (17:54 +0000)
Patch by Felix Schäfer.

git-svn-id: http://svn.redmine.org/redmine/trunk@16379 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/mail_handler.rb
test/fixtures/mail_handler/empty_text_and_html_part.eml [new file with mode: 0644]
test/unit/mail_handler_test.rb

index 53f37a29ef7939a0521af866197f8226c4a121f4..dc033de756251e641086932424c7e7e6ff4c86a2 100644 (file)
@@ -445,13 +445,19 @@ class MailHandler < ActionMailer::Base
   def plain_text_body
     return @plain_text_body unless @plain_text_body.nil?
 
+    # check if we have any plain-text parts with content
     @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
 
+    # if not, we try to parse the body from the HTML-parts
     @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/html'}).presence
 
-    @plain_text_body ||= email_parts_to_text([email])
+    # If there is still no body found, and there are no mime-parts defined,
+    # we use the whole raw mail body
+    @plain_text_body ||= email_parts_to_text([email]).presence if email.all_parts.empty?
 
-    @plain_text_body
+    # As a fallback we return an empty plain text body (e.g. if we have only
+    # empty text parts but a non-text attachment)
+    @plain_text_body ||= ""
   end
 
   def email_parts_to_text(parts)
diff --git a/test/fixtures/mail_handler/empty_text_and_html_part.eml b/test/fixtures/mail_handler/empty_text_and_html_part.eml
new file mode 100644 (file)
index 0000000..744a3f6
--- /dev/null
@@ -0,0 +1,35 @@
+From JSmith@somenet.foo  Fri Mar 22 08:30:28 2013
+From: John Smith <JSmith@somenet.foo>
+Content-Type: multipart/mixed; boundary="Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9"
+Message-Id: <BB533668-3CC8-41CA-A951-0A5D8EA37FB0@somenet.foo>
+Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\))
+Subject: Test with an empty text part
+Date: Fri, 22 Mar 2013 17:30:20 +0200
+To: redmine@somenet.foo
+X-Mailer: Apple Mail (2.1503)
+
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+       charset=us-ascii
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/html;
+       charset=us-ascii
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww=
+w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns=3D"http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
+</head>
+<body>
+</body>
+</html>
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
index 73040f766eb5b07f2f77f4ce48363fe652b77d84..5f0b3e3e0947515f0884a5e6c228ed527c07dc79 100644 (file)
@@ -628,6 +628,11 @@ class MailHandlerTest < ActiveSupport::TestCase
     assert_equal 'The html part.', issue.description
   end
 
+  def test_empty_text_and_html_part_should_make_an_empty_description
+    issue = submit_email('empty_text_and_html_part.eml', :issue => {:project => 'ecookbook'})
+    assert_equal '', issue.description
+  end
+
   def test_attachment_text_part_should_be_added_as_issue_attachment
     issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
     assert_not_include 'Plain text attachment', issue.description