Permalink
Browse files

Added html_part and text_part now return the first matching part they…

… find
  • Loading branch information...
1 parent a8b7096 commit ad49254a71b46ab51559c608aea433bf6a2f2956 @mikel committed Jan 28, 2010
Showing with 62 additions and 4 deletions.
  1. +6 −0 CHANGELOG.rdoc
  2. +1 −1 Rakefile
  3. +10 −2 lib/mail/message.rb
  4. +1 −1 lib/mail/version.rb
  5. +44 −0 spec/mail/mime_messages_spec.rb
View
@@ -1,3 +1,9 @@
+== Thu Jan 28 00:25:02 UTC 2010 Mikel Lindsaar <raasdnil@gmail.com>
+
+* Added TMM1's patch to not raise errors if a email is not multipart/report
+* Added html_part and text_part now return the first text/html or text/plain part they find. Order is from top to bottom of the email, all parts, flattened.
+* Version bump to 2.1.2
+
== Mon Jan 25 11:36:13 UTC 2010 Mikel Lindsaar <raasdnil@gmail.com>
* Added ability for address fields to init on an array instead of just a string.
View
@@ -12,7 +12,7 @@ require 'bundler'
spec = Gem::Specification.new do |s|
s.name = "mail"
- s.version = "2.1.1"
+ s.version = "2.1.2"
s.author = "Mike Lindsaar"
s.email = "raasdnil@gmail.com"
s.homepage = "http://github.com/mikel/mail"
View
@@ -1465,7 +1465,7 @@ def html_part(&block)
add_multipart_alternate_header unless html_part.blank?
add_part(@html_part)
else
- @html_part
+ @html_part || find_first_mime_type('text/html')
end
end
@@ -1476,7 +1476,7 @@ def text_part(&block)
add_multipart_alternate_header unless html_part.blank?
add_part(@text_part)
else
- @text_part
+ @text_part || find_first_mime_type('text/plain')
end
end
@@ -1649,6 +1649,14 @@ def attachment
def filename
find_attachment
end
+
+ def all_parts
+ parts.map { |p| [p, p.all_parts] }.flatten
+ end
+
+ def find_first_mime_type(mt)
+ all_parts.detect { |p| p.mime_type == mt }
+ end
private
View
@@ -3,7 +3,7 @@ module Mail
module VERSION
MAJOR = 2
MINOR = 1
- TINY = 1
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
@@ -211,6 +211,50 @@
mail.html_part.class.should == Mail::Part
mail.html_part.body.to_s.should == '<h1>This is HTML</h1>'
end
+
+ it "should detect an html_part in an existing email" do
+ m = Mail.new(:content_type => 'mixed/alternative')
+ m.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML TEXT'))
+ m.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN TEXT'))
+ m.text_part.body.decoded.should == 'PLAIN TEXT'
+ m.html_part.body.decoded.should == 'HTML TEXT'
+ end
+
+ it "should detect an html_part in a multi level mime email" do
+ m = Mail.new(:content_type => 'mixed/multipart')
+ a = Mail::Part.new(:content_type => 'text/script', :body => '12345')
+ p = Mail::Part.new(:content_type => 'mixed/alternative')
+ p.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML TEXT'))
+ p.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN TEXT'))
+ m.add_part(p)
+ m.add_part(a)
+ m.text_part.body.decoded.should == 'PLAIN TEXT'
+ m.html_part.body.decoded.should == 'HTML TEXT'
+ end
+
+ it "should only the first part on a stupidly overly complex email" do
+ m = Mail.new(:content_type => 'mixed/multipart')
+ a = Mail::Part.new(:content_type => 'text/script', :body => '12345')
+ m.add_part(a)
+
+ b = Mail::Part.new(:content_type => 'mixed/alternative')
+ b.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML TEXT'))
+ b.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN TEXT'))
+ m.add_part(b)
+
+ c = Mail::Part.new(:content_type => 'mixed/alternative')
+ c.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML 2 TEXT'))
+ c.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN 2 TEXT'))
+ b.add_part(c)
+
+ d = Mail::Part.new(:content_type => 'mixed/alternative')
+ d.add_part(Mail::Part.new(:content_type => 'text/html', :body => 'HTML 3 TEXT'))
+ d.add_part(Mail::Part.new(:content_type => 'text/plain', :body => 'PLAIN 3 TEXT'))
+ b.add_part(d)
+
+ m.text_part.body.decoded.should == 'PLAIN TEXT'
+ m.html_part.body.decoded.should == 'HTML TEXT'
+ end
end

0 comments on commit ad49254

Please sign in to comment.