Skip to content
Browse files

Merge branch '497-dont-reorder-parsed' into nmaster

  • Loading branch information...
2 parents fffb25a + 781474d commit f2f542e16e9b6c1704c2364feb6f17bdeeaefecb @ConradIrwin committed Jan 24, 2013
Showing with 98 additions and 2 deletions.
  1. +8 −1 lib/mail/body.rb
  2. +1 −1 lib/mail/message.rb
  3. +74 −0 spec/fixtures/emails/mime_emails/image_in_the_middle.eml
  4. +15 −0 spec/mail/message_spec.rb
View
9 lib/mail/body.rb
@@ -32,6 +32,7 @@ def initialize(string = '')
@epilogue = nil
@charset = nil
@part_sort_order = [ "text/plain", "text/enriched", "text/html" ]
+ @need_to_sort = false
@parts = Mail::PartsList.new
if string.blank?
@raw_source = ''
@@ -127,7 +128,8 @@ def set_sort_order(order)
def sort_parts!
@parts.each do |p|
p.body.set_sort_order(@part_sort_order)
- @parts.sort!(@part_sort_order)
+ @parts.sort!(@part_sort_order) if need_to_sort?
+ @need_to_sort = false
p.body.sort_parts!
end
end
@@ -248,6 +250,7 @@ def parts
end
def <<( val )
+ @need_to_sort = true
if @parts
@parts << val
else
@@ -273,6 +276,10 @@ def only_us_ascii?
def empty?
!!raw_source.to_s.empty?
end
+
+ def need_to_sort?
+ @need_to_sort
+ end
private
View
2 lib/mail/message.rb
@@ -1687,7 +1687,7 @@ def convert_to_multipart
# ready to send
def ready_to_send!
identify_and_set_transfer_encoding
- parts.sort!([ "text/plain", "text/enriched", "text/html", "multipart/alternative" ])
+ parts.sort!([ "text/plain", "text/enriched", "text/html", "multipart/alternative" ]) if body.need_to_sort?
parts.each do |part|
part.transport_encoding = transport_encoding
part.ready_to_send!
View
74 spec/fixtures/emails/mime_emails/image_in_the_middle.eml
@@ -0,0 +1,74 @@
+From: A User <a.user@example.com>
+To: A Nother <a.nother@example.com>
+Content-Type: multipart/alternative; boundary=Apple-Mail-89-565160509
+Subject: just testing
+Date: Fri, 1 Jul 2011 22:34:02 -0700
+Mime-Version: 1.0 (Apple Message framework v1084)
+X-Mailer: Apple Mail (2.1084)
+
+
+--Apple-Mail-89-565160509
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+ charset=us-ascii
+
+With Apple Mail, you can put a picture in the middle of a message. This =
+is what it looks like:
+
+
+
+(that's a picture of Fooooo)
+
+Oh look I am PLaying around with fonts!=
+
+--Apple-Mail-89-565160509
+Content-Type: multipart/mixed;
+ boundary=Apple-Mail-90-565160509
+
+
+--Apple-Mail-90-565160509
+Content-Transfer-Encoding: 7bit
+Content-Type: text/html;
+ charset=us-ascii
+
+
+<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>With&nbsp;<a href="http://en.wikipedia.org/wiki/Mail_(application)">Apple Mail</a>, you can put a picture in the middle of a message. <b>This</b> is what it looks like:</div><div><br></div></body></html>
+--Apple-Mail-90-565160509
+Content-Disposition: inline;
+ filename=IMG_2583.jpeg
+Content-Type: image/JPG;
+ name="IMG_2583.jpeg"
+Content-Transfer-Encoding: base64
+
+/9j/4AAQSkZJRgABAQAAAQABAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdC
+REDACTED
+
+
+--Apple-Mail-90-565160509
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/html;
+ charset=us-ascii
+
+<html><head></head><body style=3D"word-wrap: break-word; =
+-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; =
+"><div><br></div><div>(that's a picture of&nbsp;<a =
+href=3D"https://example.com/foo">Fo=
+oooo</a>)</div><div><br></div><div>Oh <i>look</i> I am <font =
+class=3D"Apple-style-span" face=3D"Desdemona" size=3D"6"><span =
+class=3D"Apple-style-span" style=3D"font-size: 28px; ">PLaying =
+</span><font class=3D"Apple-style-span" face=3D"'Edwardian Script ITC'" =
+size=3D"6" style=3D"font-size: 34px; "><span class=3D"Apple-style-span" =
+style=3D"font-size: 28px; ">around</span></font><span =
+class=3D"Apple-style-span" style=3D""> </span><font =
+class=3D"Apple-style-span" face=3D"'Engravers MT'" size=3D"6" =
+style=3D"font-size: 34px; "><span class=3D"Apple-style-span" =
+style=3D"font-size: 28px; ">with</span></font><span =
+class=3D"Apple-style-span" style=3D""> </span><font =
+class=3D"Apple-style-span" face=3D"'Gill Sans Ultra Bold'" size=3D"6" =
+style=3D"font-size: 34px; "><span class=3D"Apple-style-span" =
+style=3D"font-size: 28px; =
+"><b>fonts!</b></span></font></font></div></body></html>=
+
+--Apple-Mail-90-565160509--
+
+--Apple-Mail-89-565160509--
View
15 spec/mail/message_spec.rb
@@ -1578,6 +1578,21 @@ def self.delivering_email(mail)
mail.parts[0].parts[1].mime_type.should eq "text/html"
mail.parts[1].mime_type.should eq "image/png"
end
+
+ it "should not reorder messages that were parsed" do
+ ensure_correct_structure = lambda{ |mail|
+ mail.mime_type.should eq 'multipart/alternative'
+ mail.parts[0].mime_type.should eq 'text/plain'
+ mail.parts[1].mime_type.should eq 'multipart/mixed'
+ mail.parts[1].parts[0].mime_type.should eq 'text/html'
+ mail.parts[1].parts[1].mime_type.should eq 'image/jpg'
+ mail.parts[1].parts[2].mime_type.should eq 'text/html'
+ }
+
+ original = Mail.new(File.read(fixture('emails', 'mime_emails', 'image_in_the_middle.eml')))
+ ensure_correct_structure.call original
+ ensure_correct_structure.call Mail.new(original.encoded)
+ end
end
describe "attachment query methods" do

0 comments on commit f2f542e

Please sign in to comment.
Something went wrong with that request. Please try again.