Skip to content
This repository
Browse code

Added a "content_type" accessor to allow messages to explicitly speci…

…fy a content-type other than "text/plain" (the default).

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1382 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 100fd7269990d5ecb6dc0f9731cce6714d5ebf76 1 parent 887497b
Jamis Buck authored June 03, 2005
2  actionmailer/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added 'content_type' accessor, to allow content type to be set on a per-message basis. content_type defaults to "text/plain".
  4
+
3 5
 * Silently ignore Iconv::IllegalSequence errors when converting text #1341 [lon@speedymac.com]
4 6
 
5 7
 * Support attachments and multipart messages.
10  actionmailer/lib/action_mailer/base.rb
@@ -138,8 +138,11 @@ class Base
138 138
     @@default_charset = "utf-8"
139 139
     cattr_accessor :default_charset
140 140
 
  141
+    @@default_content_type = "text/plain"
  142
+    cattr_accessor :default_content_type
  143
+
141 144
     adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
142  
-                      :bcc, :cc, :charset
  145
+                      :bcc, :cc, :charset, :content_type
143 146
 
144 147
     attr_reader       :mail
145 148
 
@@ -156,6 +159,7 @@ def initialize(method_name=nil, *parameters)
156 159
     def create!(method_name, *parameters)
157 160
       @bcc = @cc = @from = @recipients = @sent_on = @subject = nil
158 161
       @charset = @@default_charset.dup
  162
+      @content_type = @@default_content_type.dup
159 163
       @parts = []
160 164
       @headers = {}
161 165
       @body = {}
@@ -255,13 +259,13 @@ def create_mail
255 259
         headers.each { |k, v| m[k] = v }
256 260
 
257 261
         if @parts.empty?
258  
-          m.set_content_type "text", "plain", { "charset" => charset }
  262
+          m.set_content_type content_type, nil, { "charset" => charset }
259 263
           m.body = body
260 264
         else
261 265
           if String === body
262 266
             part = TMail::Mail.new
263 267
             part.body = body
264  
-            part.set_content_type "text", "plain", { "charset" => charset }
  268
+            part.set_content_type content_type, nil, { "charset" => charset }
265 269
             part.set_content_disposition "inline"
266 270
             m.parts << part
267 271
           end
4  actionmailer/lib/action_mailer/part.rb
@@ -9,7 +9,7 @@ class Part #:nodoc:
9 9
     adv_attr_accessor :filename, :transfer_encoding, :headers
10 10
 
11 11
     def initialize(params)
12  
-      @content_type = params[:content_type] || "text/plain"
  12
+      @content_type = params[:content_type]
13 13
       @content_disposition = params[:disposition] || "inline"
14 14
       @charset = params[:charset]
15 15
       @body = params[:body]
@@ -20,7 +20,7 @@ def initialize(params)
20 20
 
21 21
     def to_mail(defaults)
22 22
       part = TMail::Mail.new
23  
-      part.set_content_type(content_type, nil,
  23
+      part.set_content_type(content_type || defaults.content_type, nil,
24 24
         "charset" => (content_disposition == "attachment" ?
25 25
                         nil : (charset || defaults.charset)),
26 26
         "name" => filename)
13  actionmailer/test/mail_service_test.rb
@@ -98,6 +98,14 @@ def implicitly_multipart_example(recipient)
98 98
     @body       = { "recipient" => recipient }
99 99
   end
100 100
 
  101
+  def html_mail(recipient)
  102
+    recipients   recipient
  103
+    subject      "html mail"
  104
+    from         "test@example.com"
  105
+    body         "<em>Emphasize</em> <strong>this</strong>"
  106
+    content_type "text/html"
  107
+  end
  108
+
101 109
   class <<self
102 110
     attr_accessor :received_body
103 111
   end
@@ -454,5 +462,10 @@ def test_implicitly_multipart_messages
454 462
     assert_equal "text/plain", mail.parts[1].content_type
455 463
   end
456 464
 
  465
+  def test_html_mail
  466
+    mail = TestMailer.create_html_mail(@recipient)
  467
+    assert_equal "text/html", mail.content_type
  468
+  end
  469
+
457 470
 end
458 471
 

0 notes on commit 100fd72

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