From: Jean-Philippe Lang Date: Sun, 14 Dec 2008 17:10:16 +0000 (+0000) Subject: Mail handler: strip tags when receiving a html-only email (#2312). X-Git-Tag: 0.9.0~873 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3bb2fccaf11d0213c401147f5edbb1f0d310a291;p=redmine.git Mail handler: strip tags when receiving a html-only email (#2312). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2136 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 5add41d72..a7264d483 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -16,6 +16,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class MailHandler < ActionMailer::Base + include ActionView::Helpers::SanitizeHelper class UnauthorizedAction < StandardError; end class MissingInformation < StandardError; end @@ -88,7 +89,7 @@ class MailHandler < ActionMailer::Base issue.status = status end issue.subject = email.subject.chomp.toutf8 - issue.description = email.plain_text_body.chomp + issue.description = plain_text_body issue.save! add_attachments(issue) logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info @@ -120,7 +121,7 @@ class MailHandler < ActionMailer::Base raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project) # add the note - journal = issue.init_journal(user, email.plain_text_body.chomp) + journal = issue.init_journal(user, plain_text_body) add_attachments(issue) # check workflow if status && issue.new_statuses_allowed_to(user).include?(status) @@ -156,21 +157,30 @@ class MailHandler < ActionMailer::Base end def get_keyword(attr) - if @@handler_options[:allow_override].include?(attr.to_s) && email.plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i + if @@handler_options[:allow_override].include?(attr.to_s) && plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i $1.strip elsif !@@handler_options[:issue][attr].blank? @@handler_options[:issue][attr] end end -end - -class TMail::Mail - # Returns body of the first plain text part found if any + + # Returns the text/plain part of the email + # If not found (eg. HTML-only email), returns the body with tags removed def plain_text_body return @plain_text_body unless @plain_text_body.nil? - p = self.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten - plain = p.detect {|c| c.content_type == 'text/plain'} - @plain_text_body = plain.nil? ? self.body : plain.body + parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten + if parts.empty? + parts << @email + end + plain_text_part = parts.detect {|p| p.content_type == 'text/plain'} + if plain_text_part.nil? + # no text/plain part found, assuming html-only email + # strip html tags and remove doctype directive + @plain_text_body = strip_tags(@email.body.to_s) + @plain_text_body.gsub! %r{^ +x-receiver: +Received: from [127.0.0.1] ([127.0.0.1]) by somenet.foo with Quick 'n Easy Mail Server SMTP (1.0.0.0); + Sun, 14 Dec 2008 16:18:06 GMT +Message-ID: <494531B9.1070709@somenet.foo> +Date: Sun, 14 Dec 2008 17:18:01 +0100 +From: "John Smith" +User-Agent: Thunderbird 2.0.0.18 (Windows/20081105) +MIME-Version: 1.0 +To: redmine@somenet.foo +Subject: HTML email +Content-Type: text/html; charset=ISO-8859-1 +Content-Transfer-Encoding: 7bit + + + + + + +This is a html-only email.
+ + diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index ed7334b3e..80f70a417 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -129,6 +129,15 @@ class MailHandlerTest < Test::Unit::TestCase assert_match /This is reply/, journal.notes assert_equal IssueStatus.find_by_name("Resolved"), issue.status end + + def test_should_strip_tags_of_html_only_emails + issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'}) + assert issue.is_a?(Issue) + assert !issue.new_record? + issue.reload + assert_equal 'HTML email', issue.subject + assert_equal 'This is a html-only email.', issue.description + end private