Project

General

Profile

Patch #13646 » fix-multiple-text-parts-email-handling.diff

Alex Shulgin, 2013-03-30 00:55

View differences:

app/models/mail_handler.rb
359 359
  def plain_text_body
360 360
    return @plain_text_body unless @plain_text_body.nil?
361 361

  
362
    part = email.text_part || email.html_part || email
363
    @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset)
362
    parts = if (text_parts = email.all_parts.select {|p| p.mime_type == 'text/plain'}).present?
363
              text_parts
364
            elsif (html_parts = email.all_parts.select {|p| p.mime_type == 'text/html'}).present?
365
              html_parts
366
            else
367
              [email]
368
            end
369
    @plain_text_body = parts.map {|p| Redmine::CodesetUtil.to_utf8(p.body.decoded, p.charset)}.join("\r\n")
364 370

  
365 371
    # strip html tags and remove doctype directive
366
    @plain_text_body = strip_tags(@plain_text_body.strip)
367
    @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
372
    if parts.any? {|p| p.mime_type == 'text/html'}
373
      @plain_text_body = strip_tags(@plain_text_body.strip)
374
      @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
375
    end
376

  
368 377
    @plain_text_body
369 378
  end
370 379

  
test/unit/mail_handler_test.rb
778 778
    assert_equal str2, user.lastname
779 779
  end
780 780

  
781
  def test_multiple_text_parts
782
    issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
783
    assert_match /first.*second.*third/m, issue.description
784
  end
785

  
781 786
  private
782 787

  
783 788
  def submit_email(filename, options={})
(1-1/2)