Project

General

Profile

Defect #25269 » 25269-intentionaly_blank_emails.patch

Felix Schäfer, 2017-03-07 08:20

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
    # check if we have any plain-text parts with content
448 449
    @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
449 450

  
451
    # if not, we try to parse the body from the HTML-parts
450 452
    @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/html'}).presence
451 453

  
452
    @plain_text_body ||= email_parts_to_text([email])
454
    # If there is still no body found, and there are no mime-parts defined,
455
    # we use the whole raw mail body
456
    @plain_text_body ||= email_parts_to_text([email]).presence if email.all_parts.empty?
453 457

  
454
    @plain_text_body
458
    # As a fallback we return an empty plain text body (e.g. if we have only
459
    # empty text parts but a non-text attachment)
460
    @plain_text_body ||= ""
455 461
  end
456 462

  
457 463
  def email_parts_to_text(parts)
test/fixtures/mail_handler/empty_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

  
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
</body>
32
</html>
33

  
34

  
35
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
test/unit/mail_handler_test.rb
628 628
    assert_equal 'The html part.', issue.description
629 629
  end
630 630

  
631
  def test_empty_text_and_html_part_should_make_an_empty_description
632
    issue = submit_email('empty_text_and_html_part.eml', :issue => {:project => 'ecookbook'})
633
    assert_equal '', issue.description
634
  end
635

  
631 636
  def test_attachment_text_part_should_be_added_as_issue_attachment
632 637
    issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
633 638
    assert_not_include 'Plain text attachment', issue.description
(2-2/2)