Skip to content
Browse files

Assigning html_ or text_part sets a default content type.

Fixes #116
  • Loading branch information...
1 parent 3c8de1f commit 9a902b386d80ca9d413df0e7aed96a7e77de5de3 @jeremy jeremy committed Jan 26, 2013
Showing with 29 additions and 2 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +4 −2 lib/mail/message.rb
  3. +24 −0 spec/mail/mime_messages_spec.rb
View
1 CHANGELOG.rdoc
@@ -1,6 +1,7 @@
== HEAD
Features:
+* Setting the html_ or text_part sets a default text/html or text/plain content type (jeremy)
* Setting the html_ or text_part to nil removes it (jeremy)
* Close pull request 399 - Accept :ca_path and :ca_file options for SMTP delivery (ndbroadbent)
View
6 lib/mail/message.rb
@@ -1559,7 +1559,7 @@ def has_attachments?
# Accessor for html_part
def html_part(&block)
if block_given?
- self.html_part = Mail::Part.new(&block)
+ self.html_part = Mail::Part.new(:content_type => 'text/html', &block)
else
@html_part || find_first_mime_type('text/html')
end
@@ -1568,7 +1568,7 @@ def html_part(&block)
# Accessor for text_part
def text_part(&block)
if block_given?
- self.text_part = Mail::Part.new(&block)
+ self.text_part = Mail::Part.new(:content_type => 'text/plain', &block)
else
@text_part || find_first_mime_type('text/plain')
end
@@ -1581,6 +1581,7 @@ def html_part=(msg)
# Assign the html part and set multipart/alternative if there's a text part.
if msg
@html_part = msg
+ @html_part.content_type = 'text/html' unless @html_part.has_content_type?
add_multipart_alternate_header if text_part
add_part @html_part
@@ -1602,6 +1603,7 @@ def text_part=(msg)
# Assign the text part and set multipart/alternative if there's an html part.
if msg
@text_part = msg
+ @text_part.content_type = 'text/plain' unless @text_part.has_content_type?
add_multipart_alternate_header if html_part
add_part @text_part
View
24 spec/mail/mime_messages_spec.rb
@@ -163,6 +163,30 @@
mail.html_part.should be_nil
end
+ it "should set default content type on assigned text and html parts" do
+ mail = Mail.new
+ mail.text_part = Mail.new
+ mail.text_part.content_type.should eq 'text/plain'
+ mail.html_part = Mail.new
+ mail.html_part.content_type.should eq 'text/html'
+ end
+
+ it "should set default content type on declared text and html parts" do
+ mail = Mail.new
+ mail.text_part { }
+ mail.text_part.content_type.should eq 'text/plain'
+ mail.html_part { }
+ mail.html_part.content_type.should eq 'text/html'
+ end
+
+ it "should not override content type" do
+ mail = Mail.new
+ mail.text_part { content_type 'text/plain+foo' }
+ mail.text_part.content_type.should eq 'text/plain+foo'
+ mail.html_part { content_type 'text/html+foo' }
+ mail.html_part.content_type.should eq 'text/html+foo'
+ end
+
it "should add the html part and text part" do
mail = Mail.new
mail.text_part = Mail::Part.new do

0 comments on commit 9a902b3

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