30838-preferred-part-multipart-email.patch

Yuichi HARADA, 2019-02-22 05:51

Download (5.52 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 the text/plain or text/html(the body with tags removed) part of the email
451
  # The preferred of parse depends on Setting.mail_handler_preferred_body_part
452
  # * 'html'           - first text/html, then text/plain
453
  # * 'plain'(Default) - first text/plain, then text/html
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 Setting.mail_handler_preferred_body_part == 'html'
459
        # HTML parts, plain-text parts
460
        ['text/html', 'text/plain']
461
      else
462
        # (Default) plain-text parts, HTML parts
463
        ['text/plain', 'text/html']
464
      end
465
    multipart_parse_order.each do |mime_type|
466
      @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == mime_type}).presence
467
      return @plain_text_body unless @plain_text_body.nil?
468
    end
460 469

  
461 470
    # If there is still no body found, and there are no mime-parts defined,
462 471
    # we use the whole raw mail body
app/views/settings/_mail_handler.html.erb
18 18
    <em class="info"><%= l(:text_comma_separated) %>
19 19
    <%= l(:label_example) %>: smime.p7s, *.vcf</em>
20 20
  </p>
21
  <p><%= setting_select :mail_handler_preferred_body_part, [['Text', 'plain'], ['HTML', 'html']] %></p>
21 22
</div>
22 23

  
23 24
<div class="box tabular settings">
config/locales/en.yml
422 422
  setting_mail_handler_enable_regex: "Enable regular expressions"
423 423
  setting_mail_handler_api_enabled: Enable WS for incoming emails
424 424
  setting_mail_handler_api_key: Incoming email WS API key
425
  setting_mail_handler_preferred_body_part: Preferred part of multipart (HTML) emails
425 426
  setting_sys_api_key: Repository management WS API key
426 427
  setting_sequential_project_identifiers: Generate sequential project identifiers
427 428
  setting_gravatar_enabled: Use Gravatar user icons
config/settings.yml
195 195
mail_handler_api_key:
196 196
  default:
197 197
  security_notifications: 1
198
mail_handler_preferred_body_part:
199
  default: plain
198 200
issue_list_default_columns:
199 201
  serialized: true
200 202
  default:
test/fixtures/mail_handler/text_and_html_part.eml
1
From JSmith@somenet.foo  Fri Mar 22 08:30:28 2013
2
From: John Smith <JSmith@somenet.foo>
3
Content-Type: multipart/mixed; boundary="Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9"
4
Message-Id: <BB533668-3CC8-41CA-A951-0A5D8EA37FB0@somenet.foo>
5
Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\))
6
Subject: Test with an empty text part
7
Date: Fri, 22 Mar 2013 17:30:20 +0200
8
To: redmine@somenet.foo
9
X-Mailer: Apple Mail (2.1503)
10

  
11

  
12

  
13
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
14
Content-Transfer-Encoding: quoted-printable
15
Content-Type: text/plain;
16
	charset=us-ascii
17

  
18
The text part.
19

  
20

  
21
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
22
Content-Transfer-Encoding: quoted-printable
23
Content-Type: text/html;
24
	charset=us-ascii
25

  
26
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww=
27
w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
28
<html xmlns=3D"http://www.w3.org/1999/xhtml">
29
<head>
30
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
31
</head>
32
<body>
33
<p>The html part.</p>
34
</body>
35
</html>
36

  
37

  
38
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
test/unit/mail_handler_test.rb
662 662
    assert_equal '', issue.description
663 663
  end
664 664

  
665
  def test_preferred_body_part_setting
666
    with_settings :mail_handler_preferred_body_part => 'plain' do
667
      issue = submit_email('text_and_html_part.eml', :issue => {:project => 'ecookbook'})
668
      assert_equal 'The text part.', issue.description
669
    end
670
    with_settings :mail_handler_preferred_body_part => 'html' do
671
      issue = submit_email('text_and_html_part.eml', :issue => {:project => 'ecookbook'})
672
      assert_equal 'The html part.', issue.description
673
    end
674
  end
675

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