Project

General

Profile

Defect #25256 » 25256-ignore_empty_email_parts.patch

Felix Schäfer, 2017-03-03 15:35

View differences:

app/models/mail_handler.rb
445 445
  def plain_text_body
446 446
    return @plain_text_body unless @plain_text_body.nil?
447 447

  
448
    parts = if (text_parts = email.all_parts.select {|p| p.mime_type == 'text/plain'}).present?
449
              text_parts
450
            elsif (html_parts = email.all_parts.select {|p| p.mime_type == 'text/html'}).present?
451
              html_parts
452
            else
453
              [email]
454
            end
448
    @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
455 449

  
450
    @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/html'}).presence
451

  
452
    @plain_text_body ||= email_parts_to_text([email])
453

  
454
    @plain_text_body
455
  end
456

  
457
  def email_parts_to_text(parts)
456 458
    parts.reject! do |part|
457 459
      part.attachment?
458 460
    end
459 461

  
460
    @plain_text_body = parts.map do |p|
462
    parts.map do |p|
461 463
      body_charset = Mail::RubyVer.respond_to?(:pick_encoding) ?
462 464
                       Mail::RubyVer.pick_encoding(p.charset).to_s : p.charset
463 465

  
......
465 467
      # convert html parts to text
466 468
      p.mime_type == 'text/html' ? self.class.html_body_to_text(body) : self.class.plain_text_body_to_text(body)
467 469
    end.join("\r\n")
468

  
469
    @plain_text_body
470 470
  end
471 471

  
472 472
  def cleaned_up_text_body
test/fixtures/mail_handler/empty_text_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

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

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

  
35

  
36
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
test/unit/mail_handler_test.rb
623 623
    assert_include 'third', issue.description
624 624
  end
625 625

  
626
  def test_empty_text_part_should_not_stop_looking_for_content
627
    issue = submit_email('empty_text_part.eml', :issue => {:project => 'ecookbook'})
628
    assert_equal 'The html part.', issue.description
629
  end
630

  
626 631
  def test_attachment_text_part_should_be_added_as_issue_attachment
627 632
    issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
628 633
    assert_not_include 'Plain text attachment', issue.description
    (1-1/1)