30838-preferred-part-multipart-email-without-gui.patch

Use configuration.yml instead of adding a new setting to the admin page - Go MAEDA, 2019-02-26 03:03

Download (3.43 KB)

View differences:

app/models/mail_handler.rb
447 447
    end
448 448
  end
449 449

  
450
  # Returns the text/plain part of the email
451
  # If not found (eg. HTML-only email), returns the body with tags removed
450
  # Returns text content of the email. If the value of
451
  # mail_handler_preferred_body_part in configuration.yml is 'html' and the
452
  # email has text/html part, it returns Textile or Markdown converted from
453
  # text/html part. Otherwise, it returns text/plain part.
452 454
  def plain_text_body
453 455
    return @plain_text_body unless @plain_text_body.nil?
454 456

  
455
    # check if we have any plain-text parts with content
456
    @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
457

  
458
    # if not, we try to parse the body from the HTML-parts
459
    @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/html'}).presence
457
    multipart_parse_order =
458
      if Redmine::Configuration['mail_handler_preferred_body_part'].to_s.casecmp('html') == 0
459
        ['text/html', 'text/plain']
460
      else
461
        ['text/plain', 'text/html']
462
      end
463
    multipart_parse_order.each do |mime_type|
464
      @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == mime_type}).presence
465
      return @plain_text_body unless @plain_text_body.nil?
466
    end
460 467

  
461 468
    # If there is still no body found, and there are no mime-parts defined,
462 469
    # we use the whole raw mail body
config/configuration.yml.example
83 83
  autologin_cookie_path:
84 84
  autologin_cookie_secure:
85 85

  
86
  # Configuration of email receiving.
87
  #
88
  # Configures which type of part in a multipart email should be parsed first
89
  # when receiving emails if the email has both text/plain and text/html part.
90
  # Possible values are 'plain' (default) and 'html'.
91
  # Examples:
92
  # mail_handler_preferred_body_part: html
93
  # mail_handler_preferred_body_part: plain
94
  mail_handler_preferred_body_part:
95

  
86 96
  # Configuration of SCM executable command.
87 97
  #
88 98
  # Absolute path (e.g. /usr/local/bin/hg) or command name (e.g. hg.exe, bzr.exe)
test/unit/mail_handler_test.rb
662 662
    assert_equal '', issue.description
663 663
  end
664 664

  
665
  def test_preferred_body_part_configuration
666
    Redmine::Configuration.stubs(:[]).with('mail_handler_preferred_body_part').returns('plain')
667
    issue = submit_email('text_and_html_part.eml', :issue => {:project => 'ecookbook'})
668
    assert_equal 'The text part.', issue.description
669

  
670
    Redmine::Configuration.stubs(:[]).with('mail_handler_preferred_body_part').returns('html')
671
    issue = submit_email('text_and_html_part.eml', :issue => {:project => 'ecookbook'})
672
    assert_equal 'The html part.', issue.description
673
  end
674

  
665 675
  def test_attachment_text_part_should_be_added_as_issue_attachment
666 676
    issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
667 677
    assert_not_include 'Plain text attachment', issue.description