]> source.dussan.org Git - redmine.git/commitdiff
Fix handling multiple text parts in email (#13646).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 12 May 2013 09:38:20 +0000 (09:38 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 12 May 2013 09:38:20 +0000 (09:38 +0000)
Patch by Alex Shulgin.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11834 e93f8b46-1217-0410-a6f0-8f06a7374b81

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

index ef67e575d4839e660090ab3a2552f065994749bc..90850dffbd487a3a181d64b59794613c818afe91 100644 (file)
@@ -377,12 +377,21 @@ class MailHandler < ActionMailer::Base
   def plain_text_body
     return @plain_text_body unless @plain_text_body.nil?
 
-    part = email.text_part || email.html_part || email
-    @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset)
+    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 = parts.map {|p| Redmine::CodesetUtil.to_utf8(p.body.decoded, p.charset)}.join("\r\n")
 
     # strip html tags and remove doctype directive
-    @plain_text_body = strip_tags(@plain_text_body.strip)
-    @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
+    if parts.any? {|p| p.mime_type == 'text/html'}
+      @plain_text_body = strip_tags(@plain_text_body.strip)
+      @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
+    end
+
     @plain_text_body
   end
 
diff --git a/test/fixtures/mail_handler/multiple_text_parts.eml b/test/fixtures/mail_handler/multiple_text_parts.eml
new file mode 100644 (file)
index 0000000..c61bcb7
--- /dev/null
@@ -0,0 +1,55 @@
+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 multiple text parts
+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
+
+The first text part.
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Disposition: inline;
+       filename=1st.pdf
+Content-Type: application/pdf;
+       x-unix-mode=0644;
+       name="1st.pdf"
+Content-Transfer-Encoding: base64
+
+JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+       charset=us-ascii
+
+The second text part.
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Disposition: inline;
+       filename=2nd.pdf
+Content-Type: application/pdf;
+       x-unix-mode=0644;
+       name="2nd.pdf"
+Content-Transfer-Encoding: base64
+
+JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+       charset=us-ascii
+
+The third one.
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
index d1eabca4295d7ea64f862c26d4c27c5cc758cc6b..013da992ecebd396cf8a5fadd3865c21c031bebc 100644 (file)
@@ -492,6 +492,13 @@ class MailHandlerTest < ActiveSupport::TestCase
     assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
   end
 
+  def test_multiple_text_parts
+    issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
+    assert_include 'first', issue.description
+    assert_include 'second', issue.description
+    assert_include 'third', issue.description
+  end
+
   def test_add_issue_with_iso_8859_1_subject
     issue = submit_email(
               'subject_as_iso-8859-1.eml',