Skip to content
This repository
Browse code

Remove old files, add some information to docs and improve test suite.

  • Loading branch information...
commit 6589976533b7a6850390ed5d6526ca719e56c5ca 1 parent 1b3cb54
authored January 26, 2010
2  actionmailer/README
@@ -22,7 +22,6 @@ the email.
22 22
 This can be as simple as:
23 23
 
24 24
   class Notifier < ActionMailer::Base
25  
-  
26 25
     delivers_from 'system@loudthinking.com'
27 26
   
28 27
     def welcome(recipient)
@@ -30,7 +29,6 @@ This can be as simple as:
30 29
       mail(:to => recipient,
31 30
            :subject => "[Signed up] Welcome #{recipient}")
32 31
     end
33  
-
34 32
   end
35 33
 
36 34
 The body of the email is created by using an Action View template (regular
48  actionmailer/lib/action_mailer/base.rb
@@ -27,9 +27,8 @@ module ActionMailer #:nodoc:
27 27
   # 
28 28
   #    def welcome(recipient)
29 29
   #      @account = recipient
30  
-  #      mail { :to => recipient.email_address_with_name,
31  
-  #             :bcc => ["bcc@example.com", "Order Watcher <watcher@example.com>"],
32  
-  #             :subject => "New account information" }
  30
+  #      mail(:to => recipient.email_address_with_name,
  31
+  #           :bcc => ["bcc@example.com", "Order Watcher <watcher@example.com>"])
33 32
   #      end
34 33
   #    end
35 34
   # 
@@ -37,13 +36,15 @@ module ActionMailer #:nodoc:
37 36
   # 
38 37
   # * <tt>attachments[]=</tt> - Allows you to add attachments to your email in an intuitive
39 38
   #   manner; <tt>attachments['filename.png'] = File.read('path/to/filename.png')</tt>
  39
+  #
40 40
   # * <tt>headers[]=</tt> - Allows you to specify non standard headers in your email such
41 41
   #   as <tt>headers['X-No-Spam'] = 'True'</tt>
  42
+  #
42 43
   # * <tt>mail</tt> - Allows you to specify your email to send.
43 44
   # 
44 45
   # The hash passed to the mail method allows you to specify the most used headers in an email
45 46
   # message, such as <tt>Subject</tt>, <tt>To</tt>, <tt>From</tt>, <tt>Cc</tt>, <tt>Bcc</tt>,
46  
-  # <tt>Reply-To</tt> and <tt>Date</tt>.  See the <tt>ActionMailer#mail</tt> method for more details.
  47
+  # <tt>Reply-To</tt> and <tt>Date</tt>. See the <tt>ActionMailer#mail</tt> method for more details.
47 48
   # 
48 49
   # If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
49 50
   #
@@ -58,6 +59,20 @@ module ActionMailer #:nodoc:
58 59
   #     format.html
59 60
   #   end
60 61
   #
  62
+  # The block syntax is useful if also need to specify information specific to a part:
  63
+  #
  64
+  #   mail(:to => user.emai) do |format|
  65
+  #     format.text(:content_transfer_encoding => "base64")
  66
+  #     format.html
  67
+  #   end
  68
+  #
  69
+  # Or even to renderize a special view:
  70
+  #
  71
+  #   mail(:to => user.emai) do |format|
  72
+  #     format.text
  73
+  #     format.html { render "some_other_template" }
  74
+  #   end
  75
+  #
61 76
   # = Mailer views
62 77
   #
63 78
   # Like Action Controller, each mailer class has a corresponding view directory in which each
@@ -79,9 +94,9 @@ module ActionMailer #:nodoc:
79 94
   #   You got a new note!
80 95
   #   <%= truncate(@note.body, 25) %>
81 96
   #
82  
-  # If you need to access the subject, from or the recipients in the view, you can do that through mailer object:
  97
+  # If you need to access the subject, from or the recipients in the view, you can do that through message object:
83 98
   #
84  
-  #   You got a new note from <%= mailer.from %>!
  99
+  #   You got a new note from <%= message.from %>!
85 100
   #   <%= truncate(@note.body, 25) %>
86 101
   #
87 102
   #
@@ -137,7 +152,7 @@ module ActionMailer #:nodoc:
137 152
   # * signup_notification.text.plain.erb
138 153
   # * signup_notification.text.html.erb
139 154
   # * signup_notification.text.xml.builder
140  
-  # * signup_notification.text.x-yaml.erb
  155
+  # * signup_notification.text.yaml.erb
141 156
   #
142 157
   # Each would be rendered and added as a separate part to the message, with the corresponding content
143 158
   # type. The content type for the entire message is automatically set to <tt>multipart/alternative</tt>,
@@ -174,8 +189,6 @@ module ActionMailer #:nodoc:
174 189
   # * <tt>delivers_from</tt> - Pass this the address that then defaults as the +from+ address on all the
175 190
   #   emails sent.  Can be overridden on a per mail basis by passing <tt>:from => 'another@address'</tt> in
176 191
   #   the +mail+ method.
177  
-  # 
178  
-  # * <tt>template_root</tt> - Determines the base from which template references will be made.
179 192
   #
180 193
   # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
181 194
   #   Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
@@ -300,9 +313,7 @@ def receive(raw_mail)
300 313
       def deliver_mail(mail) #:nodoc:
301 314
         ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload|
302 315
           self.set_payload_for_mail(payload, mail)
303  
-
304 316
           yield # Let Mail do the delivery actions
305  
-
306 317
         end
307 318
       end
308 319
 
@@ -399,7 +410,7 @@ def attachments
399 410
     # The main method that creates the message and renders the email templates. There are
400 411
     # two ways to call this method, with a block, or without a block.
401 412
     # 
402  
-    # Both methods accept a headers hash.  This hash allows you to specify the most used headers
  413
+    # Both methods accept a headers hash. This hash allows you to specify the most used headers
403 414
     # in an email message, these are:
404 415
     # 
405 416
     # * <tt>:subject</tt> - The subject of the message, if this is omitted, ActionMailer will
@@ -419,7 +430,7 @@ def attachments
419 430
     # 
420 431
     # If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
421 432
     #
422  
-    # When a <tt>:return_path</tt> is specified, that value will be used as the 'envelope from'
  433
+    # When a <tt>:return_path</tt> is specified as header, that value will be used as the 'envelope from'
423 434
     # address for the Mail message.  Setting this is useful when you want delivery notifications
424 435
     # sent to a different address than the one in <tt>:from</tt>.  Mail will actually use the 
425 436
     # <tt>:return_path</tt> in preference to the <tt>:sender</tt> in preference to the <tt>:from</tt>
@@ -447,6 +458,14 @@ def attachments
447 458
     # 
448 459
     # Which will render a <tt>multipart/alternate</tt> email with <tt>text/plain</tt> and
449 460
     # <tt>text/html</tt> parts.
  461
+    #
  462
+    # The block syntax also allows you to customize the part headers if desired:
  463
+    #
  464
+    #   mail(:to => 'mikel@test.lindsaar.net') do |format|
  465
+    #     format.text(:content_transfer_encoding => "base64")
  466
+    #     format.html
  467
+    #   end
  468
+    #
450 469
     def mail(headers={}, &block)
451 470
       # Guard flag to prevent both the old and the new API from firing
452 471
       # Should be removed when old API is removed
@@ -541,7 +560,8 @@ def each_template(&block) #:nodoc:
541 560
 
542 561
     def create_parts_from_responses(m, responses, charset) #:nodoc:
543 562
       if responses.size == 1 && !m.has_attachments?
544  
-        m.body = responses[0][:body]
  563
+        headers = responses[0]
  564
+        headers.each { |k,v| m[k] = v }
545 565
         return responses[0][:content_type]
546 566
       elsif responses.size > 1 && m.has_attachments? 
547 567
         container = Mail::Part.new
24  actionmailer/test/base_test.rb
@@ -56,6 +56,13 @@ def explicit_multipart_with_any(hash = {})
56 56
         format.any(:text, :html){ render :text => "Format with any!" }
57 57
       end
58 58
     end
  59
+
  60
+    def custom_block(include_html=false)
  61
+      mail(DEFAULT_HEADERS) do |format|
  62
+        format.text(:content_transfer_encoding => "base64"){ render "welcome" }
  63
+        format.html{ render "welcome" } if include_html
  64
+      end
  65
+    end
59 66
   end
60 67
 
61 68
   test "method call to mail does not raise error" do
@@ -337,6 +344,23 @@ def explicit_multipart_with_any(hash = {})
337 344
     assert_equal("Format with any!", email.parts[1].body.encoded)
338 345
   end
339 346
 
  347
+  test "explicit multipart with options" do
  348
+    email = BaseMailer.custom_block(true).deliver
  349
+    assert_equal(2, email.parts.size)
  350
+    assert_equal("multipart/alternate", email.mime_type)
  351
+    assert_equal("text/plain", email.parts[0].mime_type)
  352
+    assert_equal("base64", email.parts[0].content_transfer_encoding)
  353
+    assert_equal("text/html", email.parts[1].mime_type)
  354
+    assert_equal("7bit", email.parts[1].content_transfer_encoding)
  355
+  end
  356
+
  357
+  test "explicit multipart with one part is rendered as body" do
  358
+    email = BaseMailer.custom_block.deliver
  359
+    assert_equal(0, email.parts.size)
  360
+    assert_equal("text/plain", email.mime_type)
  361
+    assert_equal("base64", email.content_transfer_encoding)
  362
+  end
  363
+
340 364
   # Class level API with method missing
341 365
   test "should respond to action methods" do
342 366
     assert BaseMailer.respond_to?(:welcome)
22  actionmailer/test/mail_test.rb
... ...
@@ -1,22 +0,0 @@
1  
-require 'abstract_unit'
2  
-
3  
-class MailTest < Test::Unit::TestCase
4  
-  def test_body
5  
-    m = Mail.new
6  
-    expected = 'something_with_underscores'
7  
-    m.content_transfer_encoding = 'quoted-printable'
8  
-    quoted_body = [expected].pack('*M')
9  
-    m.body = quoted_body
10  
-    assert_equal "something_with_underscores=\r\n", m.body.encoded
11  
-    assert_equal expected, m.body.to_s
12  
-  end
13  
-
14  
-  def test_nested_attachments_are_recognized_correctly
15  
-    fixture = File.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_nested_attachment")
16  
-    mail = Mail.new(fixture)
17  
-    assert_equal 2, mail.attachments.length
18  
-    assert_equal "image/png", mail.attachments.first.mime_type
19  
-    assert_equal 1902, mail.attachments.first.decoded.length
20  
-    assert_equal "application/pkcs7-signature", mail.attachments.last.mime_type
21  
-  end
22  
-end

0 notes on commit 6589976

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