]> source.dussan.org Git - redmine.git/commitdiff
Mail parts with empty content should be ignored (#25256).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Mar 2017 08:41:20 +0000 (08:41 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 5 Mar 2017 08:41:20 +0000 (08:41 +0000)
Patch by Felix Schäfer.

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

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

index 402be4f51a59cfc930ec07c1ffc4fe7b7ebbbb0f..53f37a29ef7939a0521af866197f8226c4a121f4 100644 (file)
@@ -445,19 +445,21 @@ class MailHandler < ActionMailer::Base
   def plain_text_body
     return @plain_text_body unless @plain_text_body.nil?
 
-    parts = if (text_parts = email.all_parts.select {|p| p.mime_type == 'text/plain'}).present?
-              text_parts
-            elsif (html_parts = email.all_parts.select {|p| p.mime_type == 'text/html'}).present?
-              html_parts
-            else
-              [email]
-            end
+    @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
 
+    @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])
+
+    @plain_text_body
+  end
+
+  def email_parts_to_text(parts)
     parts.reject! do |part|
       part.attachment?
     end
 
-    @plain_text_body = parts.map do |p|
+    parts.map do |p|
       body_charset = Mail::RubyVer.respond_to?(:pick_encoding) ?
                        Mail::RubyVer.pick_encoding(p.charset).to_s : p.charset
 
@@ -465,8 +467,6 @@ class MailHandler < ActionMailer::Base
       # convert html parts to text
       p.mime_type == 'text/html' ? self.class.html_body_to_text(body) : self.class.plain_text_body_to_text(body)
     end.join("\r\n")
-
-    @plain_text_body
   end
 
   def cleaned_up_text_body
diff --git a/test/fixtures/mail_handler/empty_text_part.eml b/test/fixtures/mail_handler/empty_text_part.eml
new file mode 100644 (file)
index 0000000..7e77024
--- /dev/null
@@ -0,0 +1,36 @@
+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>
+<p>The html part.</p>
+</body>
+</html>
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
index 562359a38e09e172257c9be2504beb150d1a821b..73040f766eb5b07f2f77f4ce48363fe652b77d84 100644 (file)
@@ -623,6 +623,11 @@ class MailHandlerTest < ActiveSupport::TestCase
     assert_include 'third', issue.description
   end
 
+  def test_empty_text_part_should_not_stop_looking_for_content
+    issue = submit_email('empty_text_part.eml', :issue => {:project => 'ecookbook'})
+    assert_equal 'The html part.', 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