Project

General

Profile

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

Alex Shulgin, 2013-03-30 00:57

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/fixtures/mail_handler/multiple_text_parts.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 multiple text parts
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 first text part.
19

  
20
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
21
Content-Disposition: inline;
22
	filename=1st.pdf
23
Content-Type: application/pdf;
24
	x-unix-mode=0644;
25
	name="1st.pdf"
26
Content-Transfer-Encoding: base64
27

  
28
JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G
29

  
30
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
31
Content-Transfer-Encoding: quoted-printable
32
Content-Type: text/plain;
33
	charset=us-ascii
34

  
35
The second text part.
36

  
37
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
38
Content-Disposition: inline;
39
	filename=2nd.pdf
40
Content-Type: application/pdf;
41
	x-unix-mode=0644;
42
	name="2nd.pdf"
43
Content-Transfer-Encoding: base64
44

  
45
JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G
46

  
47

  
48
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
49
Content-Transfer-Encoding: quoted-printable
50
Content-Type: text/plain;
51
	charset=us-ascii
52

  
53
The third one.
54

  
55
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
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={})
(2-2/2)