commit 715aec2a95b25753bfe96b84d90f81e13f62a0de Author: Felix Scha╠łfer Date: Fri May 23 13:36:35 2014 +0200 Better html-only emails handling #16962 diff --git a/Gemfile b/Gemfile index dcf88ae..eb498f2 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby] gem "builder", "3.0.0" gem "request_store" gem "mime-types" +gem "loofah" # Optional gem for LDAP authentication group :ldap do diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 1b46005..5c96c11 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -423,15 +423,11 @@ class MailHandler < ActionMailer::Base @plain_text_body = parts.map do |p| body_charset = p.charset.respond_to?(:force_encoding) ? Mail::RubyVer.pick_encoding(p.charset).to_s : p.charset - Redmine::CodesetUtil.to_utf8(p.body.decoded, body_charset) + body = Redmine::CodesetUtil.to_utf8(p.body.decoded, body_charset) + # convert html parts to text + p.mime_type == 'text/html' ? Loofah.document(body).to_text : body end.join("\r\n") - # strip html tags and remove doctype directive - if parts.any? {|p| p.mime_type == 'text/html'} - @plain_text_body = strip_tags(@plain_text_body.strip) - @plain_text_body.sub! %r{^ + -This is a html-only email.
+This is a html-only email.

With a title

and a paragraph.

diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 96d16d8..a60bd32 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -763,7 +763,7 @@ class MailHandlerTest < ActiveSupport::TestCase assert !issue.new_record? issue.reload assert_equal 'HTML email', issue.subject - assert_equal 'This is a html-only email.', issue.description + assert_equal "This is a html-only email.\r\nWith a title\r\n\r\nand a paragraph.", issue.description end test "truncate emails with no setting should add the entire email into the issue" do