diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index dccf05d..057a343 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -359,12 +359,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{^ +Content-Type: multipart/mixed; boundary="Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9" +Message-Id: +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-- diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 92d007c..4a15306 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -778,6 +778,11 @@ class MailHandlerTest < ActiveSupport::TestCase assert_equal str2, user.lastname end + def test_multiple_text_parts + issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'}) + assert_match /first.*second.*third/m, issue.description + end + private def submit_email(filename, options={})