actionmailer 2.3.18 → 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionmailer might be problematic. Click here for more details.

Files changed (154) hide show
  1. data/CHANGELOG +13 -16
  2. data/README +39 -21
  3. data/lib/action_mailer/adv_attr_accessor.rb +14 -18
  4. data/lib/action_mailer/base.rb +437 -560
  5. data/lib/action_mailer/collector.rb +36 -0
  6. data/lib/action_mailer/delivery_methods.rb +86 -0
  7. data/lib/action_mailer/deprecated_api.rb +139 -0
  8. data/lib/action_mailer/mail_helper.rb +25 -13
  9. data/lib/action_mailer/old_api.rb +248 -0
  10. data/lib/action_mailer/quoting.rb +4 -2
  11. data/lib/action_mailer/railtie.rb +25 -0
  12. data/lib/action_mailer/railties/subscriber.rb +20 -0
  13. data/lib/action_mailer/test_case.rb +4 -6
  14. data/lib/action_mailer/test_helper.rb +0 -1
  15. data/lib/action_mailer/tmail_compat.rb +34 -0
  16. data/lib/action_mailer/version.rb +3 -3
  17. data/lib/action_mailer.rb +25 -30
  18. metadata +41 -161
  19. data/Rakefile +0 -97
  20. data/install.rb +0 -30
  21. data/lib/action_mailer/helpers.rb +0 -113
  22. data/lib/action_mailer/part.rb +0 -107
  23. data/lib/action_mailer/part_container.rb +0 -55
  24. data/lib/action_mailer/utils.rb +0 -7
  25. data/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb +0 -1466
  26. data/lib/action_mailer/vendor/text_format.rb +0 -10
  27. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/Makefile +0 -18
  28. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/address.rb +0 -392
  29. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/attachments.rb +0 -65
  30. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/base64.rb +0 -46
  31. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/compat.rb +0 -41
  32. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/config.rb +0 -67
  33. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/core_extensions.rb +0 -63
  34. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/encode.rb +0 -590
  35. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/header.rb +0 -962
  36. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/index.rb +0 -9
  37. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/interface.rb +0 -1162
  38. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/loader.rb +0 -3
  39. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mail.rb +0 -578
  40. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mailbox.rb +0 -496
  41. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/main.rb +0 -6
  42. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mbox.rb +0 -3
  43. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/net.rb +0 -250
  44. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/obsolete.rb +0 -132
  45. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/parser.rb +0 -1060
  46. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/parser.y +0 -416
  47. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/port.rb +0 -379
  48. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/quoting.rb +0 -164
  49. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/require_arch.rb +0 -58
  50. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/scanner.rb +0 -49
  51. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/scanner_r.rb +0 -262
  52. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/stringio.rb +0 -280
  53. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/utils.rb +0 -362
  54. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/COPYING +0 -504
  55. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/README +0 -12
  56. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/big5freq.rb +0 -927
  57. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/big5prober.rb +0 -42
  58. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/chardistribution.rb +0 -238
  59. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/charsetgroupprober.rb +0 -112
  60. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/charsetprober.rb +0 -75
  61. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/codingstatemachine.rb +0 -64
  62. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/constants.rb +0 -42
  63. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/escprober.rb +0 -89
  64. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/escsm.rb +0 -244
  65. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/eucjpprober.rb +0 -88
  66. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euckrfreq.rb +0 -596
  67. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euckrprober.rb +0 -42
  68. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euctwfreq.rb +0 -430
  69. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euctwprober.rb +0 -42
  70. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/gb2312freq.rb +0 -474
  71. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/gb2312prober.rb +0 -42
  72. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/hebrewprober.rb +0 -289
  73. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/jisfreq.rb +0 -570
  74. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/jpcntx.rb +0 -229
  75. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langbulgarianmodel.rb +0 -229
  76. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langcyrillicmodel.rb +0 -330
  77. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langgreekmodel.rb +0 -227
  78. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langhebrewmodel.rb +0 -202
  79. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langhungarianmodel.rb +0 -226
  80. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langthaimodel.rb +0 -201
  81. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/latin1prober.rb +0 -147
  82. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcharsetprober.rb +0 -89
  83. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcsgroupprober.rb +0 -45
  84. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcssm.rb +0 -542
  85. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sbcharsetprober.rb +0 -124
  86. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sbcsgroupprober.rb +0 -56
  87. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sjisprober.rb +0 -88
  88. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/universaldetector.rb +0 -168
  89. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/utf8prober.rb +0 -87
  90. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet.rb +0 -67
  91. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/version.rb +0 -39
  92. data/lib/action_mailer/vendor/tmail-1.2.7/tmail.rb +0 -6
  93. data/lib/action_mailer/vendor/tmail.rb +0 -17
  94. data/lib/actionmailer.rb +0 -2
  95. data/test/abstract_unit.rb +0 -62
  96. data/test/asset_host_test.rb +0 -54
  97. data/test/delivery_method_test.rb +0 -51
  98. data/test/fixtures/asset_host_mailer/email_with_asset.html.erb +0 -1
  99. data/test/fixtures/auto_layout_mailer/hello.html.erb +0 -1
  100. data/test/fixtures/auto_layout_mailer/multipart.text.html.erb +0 -1
  101. data/test/fixtures/auto_layout_mailer/multipart.text.plain.erb +0 -1
  102. data/test/fixtures/explicit_layout_mailer/logout.html.erb +0 -1
  103. data/test/fixtures/explicit_layout_mailer/signup.html.erb +0 -1
  104. data/test/fixtures/first_mailer/share.erb +0 -1
  105. data/test/fixtures/helper_mailer/use_example_helper.erb +0 -1
  106. data/test/fixtures/helper_mailer/use_helper.erb +0 -1
  107. data/test/fixtures/helper_mailer/use_helper_method.erb +0 -1
  108. data/test/fixtures/helper_mailer/use_mail_helper.erb +0 -5
  109. data/test/fixtures/helpers/example_helper.rb +0 -5
  110. data/test/fixtures/layouts/auto_layout_mailer.html.erb +0 -1
  111. data/test/fixtures/layouts/auto_layout_mailer.text.erb +0 -1
  112. data/test/fixtures/layouts/spam.html.erb +0 -1
  113. data/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb +0 -1
  114. data/test/fixtures/raw_email +0 -14
  115. data/test/fixtures/raw_email10 +0 -20
  116. data/test/fixtures/raw_email12 +0 -32
  117. data/test/fixtures/raw_email13 +0 -29
  118. data/test/fixtures/raw_email2 +0 -114
  119. data/test/fixtures/raw_email3 +0 -70
  120. data/test/fixtures/raw_email4 +0 -59
  121. data/test/fixtures/raw_email5 +0 -19
  122. data/test/fixtures/raw_email6 +0 -20
  123. data/test/fixtures/raw_email7 +0 -66
  124. data/test/fixtures/raw_email8 +0 -47
  125. data/test/fixtures/raw_email9 +0 -28
  126. data/test/fixtures/raw_email_quoted_with_0d0a +0 -14
  127. data/test/fixtures/raw_email_with_invalid_characters_in_content_type +0 -104
  128. data/test/fixtures/raw_email_with_nested_attachment +0 -100
  129. data/test/fixtures/raw_email_with_partially_quoted_subject +0 -14
  130. data/test/fixtures/second_mailer/share.erb +0 -1
  131. data/test/fixtures/templates/signed_up.erb +0 -3
  132. data/test/fixtures/test_mailer/_subtemplate.text.plain.erb +0 -1
  133. data/test/fixtures/test_mailer/body_ivar.erb +0 -2
  134. data/test/fixtures/test_mailer/custom_templating_extension.text.html.haml +0 -6
  135. data/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml +0 -6
  136. data/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb +0 -1
  137. data/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak +0 -1
  138. data/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb +0 -10
  139. data/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ +0 -10
  140. data/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb +0 -2
  141. data/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb +0 -1
  142. data/test/fixtures/test_mailer/included_subtemplate.text.plain.erb +0 -1
  143. data/test/fixtures/test_mailer/rxml_template.builder +0 -2
  144. data/test/fixtures/test_mailer/rxml_template.rxml +0 -2
  145. data/test/fixtures/test_mailer/signed_up.html.erb +0 -3
  146. data/test/fixtures/test_mailer/signed_up_with_url.erb +0 -5
  147. data/test/mail_helper_test.rb +0 -95
  148. data/test/mail_layout_test.rb +0 -123
  149. data/test/mail_render_test.rb +0 -116
  150. data/test/mail_service_test.rb +0 -1145
  151. data/test/quoting_test.rb +0 -105
  152. data/test/test_helper_test.rb +0 -129
  153. data/test/tmail_test.rb +0 -22
  154. data/test/url_test.rb +0 -76
@@ -1,60 +1,86 @@
1
+ require 'mail'
2
+ require 'action_mailer/tmail_compat'
3
+ require 'action_mailer/collector'
4
+
1
5
  module ActionMailer #:nodoc:
2
6
  # Action Mailer allows you to send email from your application using a mailer model and views.
3
7
  #
4
- #
5
8
  # = Mailer Models
6
9
  #
7
10
  # To use Action Mailer, you need to create a mailer model.
8
11
  #
9
12
  # $ script/generate mailer Notifier
10
13
  #
11
- # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods within the model which are then
12
- # used to set variables to be used in the mail template, to change options on the mail, or
13
- # to add attachments.
14
+ # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods
15
+ # within the model which are then used to set variables to be used in the mail template, to
16
+ # change options on the mail, or to add attachments.
14
17
  #
15
18
  # Examples:
16
19
  #
17
20
  # class Notifier < ActionMailer::Base
18
- # def signup_notification(recipient)
19
- # recipients recipient.email_address_with_name
20
- # bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"]
21
- # from "system@example.com"
22
- # subject "New account information"
23
- # body :account => recipient
21
+ # default :from => 'no-reply@example.com',
22
+ # :return_path => 'system@example.com'
23
+ #
24
+ # def welcome(recipient)
25
+ # @account = recipient
26
+ # mail(:to => recipient.email_address_with_name,
27
+ # :bcc => ["bcc@example.com", "Order Watcher <watcher@example.com>"])
28
+ # end
24
29
  # end
25
- # end
26
- #
27
- # Mailer methods have the following configuration methods available.
28
- #
29
- # * <tt>recipients</tt> - Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the <tt>To:</tt> header.
30
- # * <tt>subject</tt> - The subject of your email. Sets the <tt>Subject:</tt> header.
31
- # * <tt>from</tt> - Who the email you are sending is from. Sets the <tt>From:</tt> header.
32
- # * <tt>cc</tt> - Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the <tt>Cc:</tt> header.
33
- # * <tt>bcc</tt> - Takes one or more email addresses. These addresses will receive a blind carbon copy of your email. Sets the <tt>Bcc:</tt> header.
34
- # * <tt>reply_to</tt> - Takes one or more email addresses. These addresses will be listed as the default recipients when replying to your email. Sets the <tt>Reply-To:</tt> header.
35
- # * <tt>sent_on</tt> - The date on which the message was sent. If not set, the header wil be set by the delivery agent.
36
- # * <tt>content_type</tt> - Specify the content type of the message. Defaults to <tt>text/plain</tt>.
37
- # * <tt>headers</tt> - Specify additional headers to be set for the message, e.g. <tt>headers 'X-Mail-Count' => 107370</tt>.
30
+ #
31
+ # Within the mailer method, you have access to the following methods:
32
+ #
33
+ # * <tt>attachments[]=</tt> - Allows you to add attachments to your email in an intuitive
34
+ # manner; <tt>attachments['filename.png'] = File.read('path/to/filename.png')</tt>
35
+ #
36
+ # * <tt>headers[]=</tt> - Allows you to specify any header field in your email such
37
+ # as <tt>headers['X-No-Spam'] = 'True'</tt>. Note, while most fields (like <tt>To:</tt>
38
+ # <tt>From:</tt> can only appear once in an email header, other fields like <tt>X-Anything</tt>
39
+ # can appear multiple times. If you want to change a field that can appear multiple times,
40
+ # you need to set it to nil first so that Mail knows you are replacing it, not adding
41
+ # another field of the same name.)
42
+ #
43
+ # * <tt>headers(hash)</tt> - Allows you to specify multiple headers in your email such
44
+ # as <tt>headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'})</tt>
45
+ #
46
+ # * <tt>mail</tt> - Allows you to specify your email to send.
47
+ #
48
+ # The hash passed to the mail method allows you to specify any header that a Mail::Message
49
+ # will accept (any valid Email header including optional fields).
50
+ #
51
+ # The mail method, if not passed a block, will inspect your views and send all the views with
52
+ # the same name as the method, so the above action would send the +welcome.text.plain.erb+ view
53
+ # file as well as the +welcome.text.html.erb+ view file in a +multipart/alternative+ email.
54
+ #
55
+ # If you want to explicitly render only certain templates, pass a block:
56
+ #
57
+ # mail(:to => user.emai) do |format|
58
+ # format.text
59
+ # format.html
60
+ # end
38
61
  #
39
- # When a <tt>headers 'return-path'</tt> is specified, that value will be used as the 'envelope from'
40
- # address. Setting this is useful when you want delivery notifications sent to a different address than
41
- # the one in <tt>from</tt>.
62
+ # The block syntax is useful if also need to specify information specific to a part:
42
63
  #
43
- # The <tt>body</tt> method has special behavior. It takes a hash which generates an instance variable
44
- # named after each key in the hash containing the value that that key points to.
64
+ # mail(:to => user.emai) do |format|
65
+ # format.text(:content_transfer_encoding => "base64")
66
+ # format.html
67
+ # end
45
68
  #
46
- # So, for example, <tt>body :account => recipient</tt> would result
47
- # in an instance variable <tt>@account</tt> with the value of <tt>recipient</tt> being accessible in the
48
- # view.
69
+ # Or even to render a special view:
49
70
  #
71
+ # mail(:to => user.emai) do |format|
72
+ # format.text
73
+ # format.html { render "some_other_template" }
74
+ # end
50
75
  #
51
76
  # = Mailer views
52
77
  #
53
- # Like Action Controller, each mailer class has a corresponding view directory
54
- # in which each method of the class looks for a template with its name.
55
- # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same name as the method
56
- # in your mailer model. For example, in the mailer defined above, the template at
57
- # <tt>app/views/notifier/signup_notification.erb</tt> would be used to generate the email.
78
+ # Like Action Controller, each mailer class has a corresponding view directory in which each
79
+ # method of the class looks for a template with its name.
80
+ #
81
+ # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same
82
+ # name as the method in your mailer model. For example, in the mailer defined above, the template at
83
+ # <tt>app/views/notifier/signup_notification.text.plain.erb</tt> would be used to generate the email.
58
84
  #
59
85
  # Variables defined in the model are accessible as instance variables in the view.
60
86
  #
@@ -66,14 +92,19 @@ module ActionMailer #:nodoc:
66
92
  # You can even use Action Pack helpers in these views. For example:
67
93
  #
68
94
  # You got a new note!
69
- # <%= truncate(note.body, 25) %>
95
+ # <%= truncate(@note.body, 25) %>
96
+ #
97
+ # If you need to access the subject, from or the recipients in the view, you can do that through message object:
98
+ #
99
+ # You got a new note from <%= message.from %>!
100
+ # <%= truncate(@note.body, 25) %>
70
101
  #
71
102
  #
72
103
  # = Generating URLs
73
104
  #
74
- # URLs can be generated in mailer views using <tt>url_for</tt> or named routes.
75
- # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request,
76
- # so you'll need to provide all of the details needed to generate a URL.
105
+ # URLs can be generated in mailer views using <tt>url_for</tt> or named routes. Unlike controllers from
106
+ # Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need
107
+ # to provide all of the details needed to generate a URL.
77
108
  #
78
109
  # When using <tt>url_for</tt> you'll need to provide the <tt>:host</tt>, <tt>:controller</tt>, and <tt>:action</tt>:
79
110
  #
@@ -83,11 +114,11 @@ module ActionMailer #:nodoc:
83
114
  #
84
115
  # <%= users_url(:host => "example.com") %>
85
116
  #
86
- # You will want to avoid using the <tt>name_of_route_path</tt> form of named routes because it doesn't make sense to
87
- # generate relative URLs in email messages.
117
+ # You will want to avoid using the <tt>name_of_route_path</tt> form of named routes because it doesn't
118
+ # make sense to generate relative URLs in email messages.
88
119
  #
89
- # It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt> option in
90
- # the <tt>ActionMailer::Base.default_url_options</tt> hash as follows:
120
+ # It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt>
121
+ # option in the <tt>ActionMailer::Base.default_url_options</tt> hash as follows:
91
122
  #
92
123
  # ActionMailer::Base.default_url_options[:host] = "example.com"
93
124
  #
@@ -96,62 +127,22 @@ module ActionMailer #:nodoc:
96
127
  # config.action_mailer.default_url_options = { :host => "example.com" }
97
128
  #
98
129
  # If you do decide to set a default <tt>:host</tt> for your mailers you will want to use the
99
- # <tt>:only_path => false</tt> option when using <tt>url_for</tt>. This will ensure that absolute URLs are generated because
100
- # the <tt>url_for</tt> view helper will, by default, generate relative URLs when a <tt>:host</tt> option isn't
101
- # explicitly provided.
130
+ # <tt>:only_path => false</tt> option when using <tt>url_for</tt>. This will ensure that absolute URLs are
131
+ # generated because the <tt>url_for</tt> view helper will, by default, generate relative URLs when a
132
+ # <tt>:host</tt> option isn't explicitly provided.
102
133
  #
103
134
  # = Sending mail
104
135
  #
105
136
  # Once a mailer action and template are defined, you can deliver your message or create it and save it
106
137
  # for delivery later:
107
138
  #
108
- # Notifier.deliver_signup_notification(david) # sends the email
109
- # mail = Notifier.create_signup_notification(david) # => a tmail object
110
- # Notifier.deliver(mail)
111
- #
112
- # You never instantiate your mailer class. Rather, your delivery instance
113
- # methods are automatically wrapped in class methods that start with the word
114
- # <tt>deliver_</tt> followed by the name of the mailer method that you would
115
- # like to deliver. The <tt>signup_notification</tt> method defined above is
116
- # delivered by invoking <tt>Notifier.deliver_signup_notification</tt>.
117
- #
118
- #
119
- # = HTML email
120
- #
121
- # To send mail as HTML, make sure your view (the <tt>.erb</tt> file) generates HTML and
122
- # set the content type to html.
123
- #
124
- # class MyMailer < ActionMailer::Base
125
- # def signup_notification(recipient)
126
- # recipients recipient.email_address_with_name
127
- # subject "New account information"
128
- # from "system@example.com"
129
- # body :account => recipient
130
- # content_type "text/html"
131
- # end
132
- # end
133
- #
139
+ # Notifier.welcome(david).deliver # sends the email
140
+ # mail = Notifier.welcome(david) # => a Mail::Message object
141
+ # mail.deliver # sends the email
134
142
  #
135
- # = Multipart email
143
+ # You never instantiate your mailer class. Rather, you just call the method you defined on the class itself.
136
144
  #
137
- # You can explicitly specify multipart messages:
138
- #
139
- # class ApplicationMailer < ActionMailer::Base
140
- # def signup_notification(recipient)
141
- # recipients recipient.email_address_with_name
142
- # subject "New account information"
143
- # from "system@example.com"
144
- # content_type "multipart/alternative"
145
- #
146
- # part :content_type => "text/html",
147
- # :body => render_message("signup-as-html", :account => recipient)
148
- #
149
- # part "text/plain" do |p|
150
- # p.body = render_message("signup-as-plain", :account => recipient)
151
- # p.transfer_encoding = "base64"
152
- # end
153
- # end
154
- # end
145
+ # = Multipart Emails
155
146
  #
156
147
  # Multipart messages can also be used implicitly because Action Mailer will automatically
157
148
  # detect and use multipart templates, where each template is named after the name of the action, followed
@@ -161,13 +152,12 @@ module ActionMailer #:nodoc:
161
152
  # * signup_notification.text.plain.erb
162
153
  # * signup_notification.text.html.erb
163
154
  # * signup_notification.text.xml.builder
164
- # * signup_notification.text.x-yaml.erb
155
+ # * signup_notification.text.yaml.erb
165
156
  #
166
- # Each would be rendered and added as a separate part to the message,
167
- # with the corresponding content type. The content type for the entire
168
- # message is automatically set to <tt>multipart/alternative</tt>, which indicates
169
- # that the email contains multiple different representations of the same email
170
- # body. The same body hash is passed to each template.
157
+ # Each would be rendered and added as a separate part to the message, with the corresponding content
158
+ # type. The content type for the entire message is automatically set to <tt>multipart/alternative</tt>,
159
+ # which indicates that the email contains multiple different representations of the same email
160
+ # body. The same instance variables defined in the action are passed to all email templates.
171
161
  #
172
162
  # Implicit template rendering is not performed if any attachments or parts have been added to the email.
173
163
  # This means that you'll have to manually add each part to the email and set the content type of the email
@@ -175,565 +165,452 @@ module ActionMailer #:nodoc:
175
165
  #
176
166
  # = Attachments
177
167
  #
178
- # Attachments can be added by using the +attachment+ method.
179
- #
180
- # Example:
168
+ # You can see above how to make a multipart HTML / Text email, to send attachments is just
169
+ # as easy:
181
170
  #
182
171
  # class ApplicationMailer < ActionMailer::Base
183
- # # attachments
184
- # def signup_notification(recipient)
185
- # recipients recipient.email_address_with_name
186
- # subject "New account information"
187
- # from "system@example.com"
188
- #
189
- # attachment :content_type => "image/jpeg",
190
- # :body => File.read("an-image.jpg")
191
- #
192
- # attachment "application/pdf" do |a|
193
- # a.body = generate_your_pdf_here()
194
- # end
172
+ # def welcome(recipient)
173
+ # attachments['free_book.pdf'] = { :data => File.read('path/to/file.pdf') }
174
+ # mail(:to => recipient, :subject => "New account information")
195
175
  # end
196
176
  # end
177
+ #
178
+ # Which will (if it had both a <tt>welcome.text.plain.erb</tt> and <tt>welcome.text.html.erb</tt>
179
+ # tempalte in the view directory), send a complete <tt>multipart/mixed</tt> email with two parts,
180
+ # the first part being a <tt>multipart/alternative</tt> with the text and HTML email parts inside,
181
+ # and the second being a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book
182
+ # with the filename +free_book.pdf+.
197
183
  #
198
- # = Multipart Emails with Attachments
199
- #
200
- # Multipart emails that also have attachments can be created by nesting a "multipart/alternative" part
201
- # within an email that has its content type set to "multipart/mixed". This would also need two templates
202
- # in place within +app/views/mailer+ called "welcome_email.text.html.erb" and "welcome_email.text.plain.erb"
203
- #
204
- # class ApplicationMailer < ActionMailer::Base
205
- # def signup_notification(recipient)
206
- # recipients recipient.email_address_with_name
207
- # subject "New account information"
208
- # from "system@example.com"
209
- # content_type "multipart/mixed"
210
- #
211
- # part "multipart/alternative" do |alternative|
212
- #
213
- # alternative.part "text/html" do |html|
214
- # html.body = render_message("welcome_email.text.html", :message => "<h1>HTML content</h1>")
215
- # end
216
- #
217
- # alternative.part "text/plain" do |plain|
218
- # plain.body = render_message("welcome_email.text.plain", :message => "text content")
219
- # end
220
- #
221
- # end
222
- #
223
- # attachment :content_type => "image/png",
224
- # :body => File.read(File.join(RAILS_ROOT, 'public/images/rails.png'))
225
- #
226
- # attachment "application/pdf" do |a|
227
- # a.body = File.read('/Users/mikel/Code/mail/spec/fixtures/attachments/test.pdf')
228
- # end
229
- # end
230
- # end
231
184
  #
232
185
  # = Configuration options
233
186
  #
234
187
  # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
235
188
  #
236
- # * <tt>template_root</tt> - Determines the base from which template references will be made.
189
+ # * <tt>default</tt> - This is a class wide hash of <tt>:key => value</tt> pairs containing
190
+ # default values for the specified header fields of the <tt>Mail::Message</tt>. You can
191
+ # specify a default for any valid header for <tt>Mail::Message</tt> and it will be used if
192
+ # you do not override it. You pass in the header value as a symbol, all lower case with under
193
+ # scores instead of hyphens, so <tt>Content-Transfer-Encoding:</tt>
194
+ # becomes <tt>:content_transfer_encoding</tt>. The defaults set by Action Mailer are:
195
+ # * <tt>:mime_version => "1.0"</tt>
196
+ # * <tt>:charset => "utf-8",</tt>
197
+ # * <tt>:content_type => "text/plain",</tt>
198
+ # * <tt>:parts_order => [ "text/plain", "text/enriched", "text/html" ]</tt>
237
199
  #
238
200
  # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
239
201
  # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
240
202
  #
241
203
  # * <tt>smtp_settings</tt> - Allows detailed configuration for <tt>:smtp</tt> delivery method:
242
- # * <tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
204
+ # * <tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default
205
+ # "localhost" setting.
243
206
  # * <tt>:port</tt> - On the off chance that your mail server doesn't run on port 25, you can change it.
244
207
  # * <tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
245
208
  # * <tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
246
209
  # * <tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
247
- # * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here.
210
+ # * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the
211
+ # authentication type here.
248
212
  # This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
249
- # * <tt>:enable_starttls_auto</tt> - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it.
250
- # It works only on Ruby >= 1.8.7 and Ruby >= 1.9. Default is true.
213
+ # * <tt>:enable_starttls_auto</tt> - When set to true, detects if STARTTLS is enabled in your SMTP server
214
+ # and starts to use it.
251
215
  #
252
216
  # * <tt>sendmail_settings</tt> - Allows you to override options for the <tt>:sendmail</tt> delivery method.
253
217
  # * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
254
- # * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
218
+ # * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt> with <tt>-f sender@addres</tt>
219
+ # added automatically before the message is sent.
220
+ #
221
+ # * <tt>file_settings</tt> - Allows you to override options for the <tt>:file</tt> delivery method.
222
+ # * <tt>:location</tt> - The directory into which emails will be written. Defaults to the application
223
+ # <tt>tmp/mails</tt>.
255
224
  #
256
225
  # * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered.
257
226
  #
258
- # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.
227
+ # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default),
228
+ # <tt>:sendmail</tt>, <tt>:test</tt>, and <tt>:file</tt>. Or you may provide a custom delivery method
229
+ # object eg. MyOwnDeliveryMethodClass.new. See the Mail gem documentation on the interface you need to
230
+ # implement for a custom delivery agent.
259
231
  #
260
- # * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are,
261
- # but this can be turned off to help functional testing.
232
+ # * <tt>perform_deliveries</tt> - Determines whether emails are actually sent from Action Mailer when you
233
+ # call <tt>.deliver</tt> on an mail message or on an Action Mailer method. This is on by default but can
234
+ # be turned off to aid in functional testing.
262
235
  #
263
- # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with <tt>delivery_method :test</tt>. Most useful
264
- # for unit and functional testing.
236
+ # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with
237
+ # <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
265
238
  #
266
- # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
267
- # pick a different charset from inside a method with +charset+.
239
+ # * <tt>default_charset</tt> - This is now deprecated, use the +default+ method above to
240
+ # set the default +:charset+.
268
241
  #
269
- # * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
270
- # can also pick a different content type from inside a method with +content_type+.
242
+ # * <tt>default_content_type</tt> - This is now deprecated, use the +default+ method above
243
+ # to set the default +:content_type+.
271
244
  #
272
- # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to <tt>1.0</tt>. You
273
- # can also pick a different value from inside a method with +mime_version+.
245
+ # * <tt>default_mime_version</tt> - This is now deprecated, use the +default+ method above
246
+ # to set the default +:mime_version+.
274
247
  #
275
- # * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
276
- # which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
277
- # <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
278
- # and appear last in the mime encoded message. You can also pick a different order from inside a method with
279
- # +implicit_parts_order+.
280
- class Base
281
- include AdvAttrAccessor, PartContainer, Quoting, Utils
282
- if Object.const_defined?(:ActionController)
283
- include ActionController::UrlWriter
284
- include ActionController::Layout
285
- end
286
-
287
- private_class_method :new #:nodoc:
288
-
289
- class_inheritable_accessor :view_paths
290
- self.view_paths = []
291
-
292
- cattr_accessor :logger
293
-
294
- @@smtp_settings = {
295
- :address => "localhost",
296
- :port => 25,
297
- :domain => 'localhost.localdomain',
298
- :user_name => nil,
299
- :password => nil,
300
- :authentication => nil,
301
- :enable_starttls_auto => true,
302
- }
303
- cattr_accessor :smtp_settings
304
-
305
- @@sendmail_settings = {
306
- :location => '/usr/sbin/sendmail',
307
- :arguments => '-i -t'
308
- }
309
- cattr_accessor :sendmail_settings
310
-
311
- @@raise_delivery_errors = true
312
- cattr_accessor :raise_delivery_errors
313
-
314
- class_attribute :delivery_method
315
- self.delivery_method = :smtp
316
-
317
- @@perform_deliveries = true
318
- cattr_accessor :perform_deliveries
319
-
320
- @@deliveries = []
321
- cattr_accessor :deliveries
322
-
323
- @@default_charset = "utf-8"
324
- cattr_accessor :default_charset
325
-
326
- @@default_content_type = "text/plain"
327
- cattr_accessor :default_content_type
328
-
329
- @@default_mime_version = "1.0"
330
- cattr_accessor :default_mime_version
331
-
332
- @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
333
- cattr_accessor :default_implicit_parts_order
248
+ # * <tt>default_implicit_parts_order</tt> - This is now deprecated, use the +default+ method above
249
+ # to set the default +:parts_order+. Parts Order is used when a message is built implicitly
250
+ # (i.e. multiple parts are assembled from templates which specify the content type in their
251
+ # filenames) this variable controls how the parts are ordered.
252
+ class Base < AbstractController::Base
253
+ include DeliveryMethods, Quoting
254
+ abstract!
334
255
 
335
- cattr_reader :protected_instance_variables
336
- @@protected_instance_variables = %w(@body)
256
+ include AbstractController::Logger
257
+ include AbstractController::Rendering
258
+ include AbstractController::LocalizedCache
259
+ include AbstractController::Layouts
260
+ include AbstractController::Helpers
261
+ include AbstractController::Translation
262
+ include AbstractController::Compatibility
337
263
 
338
- # Specify the BCC addresses for the message
339
- adv_attr_accessor :bcc
264
+ helper ActionMailer::MailHelper
340
265
 
341
- # Define the body of the message. This is either a Hash (in which case it
342
- # specifies the variables to pass to the template when it is rendered),
343
- # or a string, in which case it specifies the actual text of the message.
344
- adv_attr_accessor :body
345
-
346
- # Specify the CC addresses for the message.
347
- adv_attr_accessor :cc
348
-
349
- # Specify the charset to use for the message. This defaults to the
350
- # +default_charset+ specified for ActionMailer::Base.
351
- adv_attr_accessor :charset
352
-
353
- # Specify the content type for the message. This defaults to <tt>text/plain</tt>
354
- # in most cases, but can be automatically set in some situations.
355
- adv_attr_accessor :content_type
356
-
357
- # Specify the from address for the message.
358
- adv_attr_accessor :from
359
-
360
- # Specify the address (if different than the "from" address) to direct
361
- # replies to this message.
362
- adv_attr_accessor :reply_to
363
-
364
- # Specify additional headers to be added to the message.
365
- adv_attr_accessor :headers
366
-
367
- # Specify the order in which parts should be sorted, based on content-type.
368
- # This defaults to the value for the +default_implicit_parts_order+.
369
- adv_attr_accessor :implicit_parts_order
370
-
371
- # Defaults to "1.0", but may be explicitly given if needed.
372
- adv_attr_accessor :mime_version
373
-
374
- # The recipient addresses for the message, either as a string (for a single
375
- # address) or an array (for multiple addresses).
376
- adv_attr_accessor :recipients
377
-
378
- # The date on which the message was sent. If not set (the default), the
379
- # header will be set by the delivery agent.
380
- adv_attr_accessor :sent_on
381
-
382
- # Specify the subject of the message.
383
- adv_attr_accessor :subject
384
-
385
- # Specify the template name to use for current message. This is the "base"
386
- # template name, without the extension or directory, and may be used to
387
- # have multiple mailer methods share the same template.
388
- adv_attr_accessor :template
389
-
390
- # Override the mailer name, which defaults to an inflected version of the
391
- # mailer's class name. If you want to use a template in a non-standard
392
- # location, you can use this to specify that location.
393
- def mailer_name(value = nil)
394
- if value
395
- self.mailer_name = value
396
- else
397
- self.class.mailer_name
398
- end
399
- end
266
+ include ActionMailer::OldApi
267
+ include ActionMailer::DeprecatedApi
400
268
 
401
- def mailer_name=(value)
402
- self.class.mailer_name = value
403
- end
269
+ private_class_method :new #:nodoc:
404
270
 
405
- # The mail object instance referenced by this mailer.
406
- attr_reader :mail
407
- attr_reader :template_name, :default_template_name, :action_name
271
+ class_attribute :default_params
272
+ self.default_params = {
273
+ :mime_version => "1.0",
274
+ :charset => "utf-8",
275
+ :content_type => "text/plain",
276
+ :parts_order => [ "text/plain", "text/enriched", "text/html" ]
277
+ }.freeze
408
278
 
409
279
  class << self
410
- attr_writer :mailer_name
411
280
 
412
281
  def mailer_name
413
282
  @mailer_name ||= name.underscore
414
283
  end
284
+ attr_writer :mailer_name
285
+ alias :controller_path :mailer_name
415
286
 
416
- # for ActionView compatibility
417
- alias_method :controller_name, :mailer_name
418
- alias_method :controller_path, :mailer_name
419
-
420
- def respond_to?(method_symbol, include_private = false) #:nodoc:
421
- matches_dynamic_method?(method_symbol) || super
422
- end
423
-
424
- def method_missing(method_symbol, *parameters) #:nodoc:
425
- if match = matches_dynamic_method?(method_symbol)
426
- case match[1]
427
- when 'create' then new(match[2], *parameters).mail
428
- when 'deliver' then new(match[2], *parameters).deliver!
429
- when 'new' then nil
430
- else super
431
- end
432
- else
433
- super
434
- end
287
+ def default(value = nil)
288
+ self.default_params = default_params.merge(value).freeze if value
289
+ default_params
435
290
  end
436
291
 
437
292
  # Receives a raw email, parses it into an email object, decodes it,
438
293
  # instantiates a new mailer, and passes the email object to the mailer
439
294
  # object's +receive+ method. If you want your mailer to be able to
440
295
  # process incoming messages, you'll need to implement a +receive+
441
- # method that accepts the email object as a parameter:
296
+ # method that accepts the raw email string as a parameter:
442
297
  #
443
298
  # class MyMailer < ActionMailer::Base
444
299
  # def receive(mail)
445
300
  # ...
446
301
  # end
447
302
  # end
448
- def receive(raw_email)
449
- logger.info "Received mail:\n #{raw_email}" unless logger.nil?
450
- mail = TMail::Mail.parse(raw_email)
451
- mail.base64_decode
452
- new.receive(mail)
303
+ def receive(raw_mail)
304
+ ActiveSupport::Notifications.instrument("action_mailer.receive") do |payload|
305
+ mail = Mail.new(raw_mail)
306
+ set_payload_for_mail(payload, mail)
307
+ new.receive(mail)
308
+ end
453
309
  end
454
310
 
455
- # Deliver the given mail object directly. This can be used to deliver
456
- # a preconstructed mail object, like:
457
- #
458
- # email = MyMailer.create_some_mail(parameters)
459
- # email.set_some_obscure_header "frobnicate"
460
- # MyMailer.deliver(email)
461
- def deliver(mail)
462
- new.deliver!(mail)
311
+ # Wraps an email delivery inside of Active Support Notifications instrumentation. This
312
+ # method is actually called by the <tt>Mail::Message</tt> object itself through a call back
313
+ # when you call <tt>:deliver</tt> on the Mail::Message, calling +deliver_mail+ directly
314
+ # and passing a Mail::Message will do nothing except tell the logger you sent the email.
315
+ def deliver_mail(mail) #:nodoc:
316
+ ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload|
317
+ self.set_payload_for_mail(payload, mail)
318
+ yield # Let Mail do the delivery actions
319
+ end
463
320
  end
464
321
 
465
- def template_root
466
- self.view_paths && self.view_paths.first
322
+ def respond_to?(method, *args) #:nodoc:
323
+ super || action_methods.include?(method.to_s)
467
324
  end
468
325
 
469
- def template_root=(root)
470
- self.view_paths = ActionView::Base.process_view_paths(root)
326
+ protected
327
+
328
+ def set_payload_for_mail(payload, mail) #:nodoc:
329
+ payload[:mailer] = self.name
330
+ payload[:message_id] = mail.message_id
331
+ payload[:subject] = mail.subject
332
+ payload[:to] = mail.to
333
+ payload[:from] = mail.from
334
+ payload[:bcc] = mail.bcc if mail.bcc.present?
335
+ payload[:cc] = mail.cc if mail.cc.present?
336
+ payload[:date] = mail.date
337
+ payload[:mail] = mail.encoded
471
338
  end
472
339
 
473
- private
474
- def matches_dynamic_method?(method_name) #:nodoc:
475
- method_name = method_name.to_s
476
- /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name)
340
+ def method_missing(method, *args) #:nodoc:
341
+ if action_methods.include?(method.to_s)
342
+ new(method, *args).message
343
+ else
344
+ super
477
345
  end
346
+ end
478
347
  end
479
348
 
349
+ attr_internal :message
350
+
480
351
  # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
481
352
  # will be initialized according to the named method. If not, the mailer will
482
353
  # remain uninitialized (useful when you only need to invoke the "receive"
483
354
  # method, for instance).
484
- def initialize(method_name=nil, *parameters) #:nodoc:
485
- create!(method_name, *parameters) if method_name
355
+ def initialize(method_name=nil, *args)
356
+ super()
357
+ @_message = Mail.new
358
+ process(method_name, *args) if method_name
486
359
  end
487
360
 
488
- # Initialize the mailer via the given +method_name+. The body will be
489
- # rendered and a new TMail::Mail object created.
490
- def create!(method_name, *parameters) #:nodoc:
491
- initialize_defaults(method_name)
492
- __send__(method_name, *parameters)
493
-
494
- # If an explicit, textual body has not been set, we check assumptions.
495
- unless String === @body
496
- # First, we look to see if there are any likely templates that match,
497
- # which include the content-type in their file name (i.e.,
498
- # "the_template_file.text.html.erb", etc.). Only do this if parts
499
- # have not already been specified manually.
500
- if @parts.empty?
501
- Dir.glob("#{template_path}/#{@template}.*").each do |path|
502
- template = template_root["#{mailer_name}/#{File.basename(path)}"]
503
-
504
- # Skip unless template has a multipart format
505
- next unless template && template.multipart?
506
-
507
- @parts << Part.new(
508
- :content_type => template.content_type,
509
- :disposition => "inline",
510
- :charset => charset,
511
- :body => render_message(template, @body)
512
- )
513
- end
514
- unless @parts.empty?
515
- @content_type = "multipart/alternative" if @content_type !~ /^multipart/
516
- @parts = sort_parts(@parts, @implicit_parts_order)
517
- end
518
- end
519
-
520
- # Then, if there were such templates, we check to see if we ought to
521
- # also render a "normal" template (without the content type). If a
522
- # normal template exists (or if there were no implicit parts) we render
523
- # it.
524
- template_exists = @parts.empty?
525
- template_exists ||= template_root["#{mailer_name}/#{@template}"]
526
- @body = render_message(@template, @body) if template_exists
527
-
528
- # Finally, if there are other message parts and a textual body exists,
529
- # we shift it onto the front of the parts and set the body to nil (so
530
- # that create_mail doesn't try to render it in addition to the parts).
531
- if !@parts.empty? && String === @body
532
- @parts.unshift Part.new(:charset => charset, :body => @body)
533
- @body = nil
534
- end
361
+ # Allows you to pass random and unusual headers to the new +Mail::Message+ object
362
+ # which will add them to itself.
363
+ #
364
+ # headers['X-Special-Domain-Specific-Header'] = "SecretValue"
365
+ #
366
+ # You can also pass a hash into headers of header field names and values, which
367
+ # will then be set on the Mail::Message object:
368
+ #
369
+ # headers 'X-Special-Domain-Specific-Header' => "SecretValue",
370
+ # 'In-Reply-To' => incoming.message_id
371
+ #
372
+ # The resulting Mail::Message will have the following in it's header:
373
+ #
374
+ # X-Special-Domain-Specific-Header: SecretValue
375
+ def headers(args=nil)
376
+ if args
377
+ @_message.headers(args)
378
+ else
379
+ @_message
535
380
  end
536
-
537
- # If this is a multipart e-mail add the mime_version if it is not
538
- # already set.
539
- @mime_version ||= "1.0" if !@parts.empty?
540
-
541
- # build the mail object itself
542
- @mail = create_mail
543
381
  end
544
382
 
545
- # Delivers a TMail::Mail object. By default, it delivers the cached mail
546
- # object (from the <tt>create!</tt> method). If no cached mail object exists, and
547
- # no alternate has been given as the parameter, this will fail.
548
- def deliver!(mail = @mail)
549
- raise "no mail object available for delivery!" unless mail
550
- unless logger.nil?
551
- logger.info "Sent mail to #{Array(recipients).join(', ')}"
552
- logger.debug "\n#{mail.encoded}"
553
- end
554
-
555
- begin
556
- __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
557
- rescue Exception => e # Net::SMTP errors or sendmail pipe errors
558
- raise e if raise_delivery_errors
559
- end
560
-
561
- return mail
383
+ # Allows you to add attachments to an email, like so:
384
+ #
385
+ # mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
386
+ #
387
+ # If you do this, then Mail will take the file name and work out the mime type
388
+ # set the Content-Type, Content-Disposition, Content-Transfer-Encoding and
389
+ # base64 encode the contents of the attachment all for you.
390
+ #
391
+ # You can also specify overrides if you want by passing a hash instead of a string:
392
+ #
393
+ # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
394
+ # :content => File.read('/path/to/filename.jpg')}
395
+ #
396
+ # If you want to use a different encoding than Base64, you can pass an encoding in,
397
+ # but then it is up to you to pass in the content pre-encoded, and don't expect
398
+ # Mail to know how to decode this data:
399
+ #
400
+ # file_content = SpecialEncode(File.read('/path/to/filename.jpg'))
401
+ # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
402
+ # :encoding => 'SpecialEncoding',
403
+ # :content => file_content }
404
+ #
405
+ # You can also search for specific attachments:
406
+ #
407
+ # # By Filename
408
+ # mail.attachments['filename.jpg'] #=> Mail::Part object or nil
409
+ #
410
+ # # or by index
411
+ # mail.attachments[0] #=> Mail::Part (first attachment)
412
+ #
413
+ def attachments
414
+ @_message.attachments
562
415
  end
563
416
 
564
- private
565
- # Set up the default values for the various instance variables of this
566
- # mailer. Subclasses may override this method to provide different
567
- # defaults.
568
- def initialize_defaults(method_name)
569
- @charset ||= @@default_charset.dup
570
- @content_type ||= @@default_content_type.dup
571
- @implicit_parts_order ||= @@default_implicit_parts_order.dup
572
- @template ||= method_name
573
- @default_template_name = @action_name = @template
574
- @mailer_name ||= self.class.name.underscore
575
- @parts ||= []
576
- @headers ||= {}
577
- @body ||= {}
578
- @mime_version = @@default_mime_version.dup if @@default_mime_version
579
- @sent_on ||= Time.now
580
- end
581
-
582
- def render_message(method_name, body)
583
- if method_name.respond_to?(:content_type)
584
- @current_template_content_type = method_name.content_type
585
- end
586
- render :file => method_name, :body => body
587
- ensure
588
- @current_template_content_type = nil
417
+ # The main method that creates the message and renders the email templates. There are
418
+ # two ways to call this method, with a block, or without a block.
419
+ #
420
+ # Both methods accept a headers hash. This hash allows you to specify the most used headers
421
+ # in an email message, these are:
422
+ #
423
+ # * <tt>:subject</tt> - The subject of the message, if this is omitted, ActionMailer will
424
+ # ask the Rails I18n class for a translated <tt>:subject</tt> in the scope of
425
+ # <tt>[:actionmailer, mailer_scope, action_name]</tt> or if this is missing, will translate the
426
+ # humanized version of the <tt>action_name</tt>
427
+ # * <tt>:to</tt> - Who the message is destined for, can be a string of addresses, or an array
428
+ # of addresses.
429
+ # * <tt>:from</tt> - Who the message is from
430
+ # * <tt>:cc</tt> - Who you would like to Carbon-Copy on this email, can be a string of addresses,
431
+ # or an array of addresses.
432
+ # * <tt>:bcc</tt> - Who you would like to Blind-Carbon-Copy on this email, can be a string of
433
+ # addresses, or an array of addresses.
434
+ # * <tt>:reply_to</tt> - Who to set the Reply-To header of the email to.
435
+ # * <tt>:date</tt> - The date to say the email was sent on.
436
+ #
437
+ # You can set default values for any of the above headers (except :date) by using the <tt>default</tt>
438
+ # class method:
439
+ #
440
+ # class Notifier < ActionMailer::Base
441
+ # self.default :from => 'no-reply@test.lindsaar.net',
442
+ # :bcc => 'email_logger@test.lindsaar.net',
443
+ # :reply_to => 'bounces@test.lindsaar.net'
444
+ # end
445
+ #
446
+ # If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
447
+ #
448
+ # When a <tt>:return_path</tt> is specified as header, that value will be used as the 'envelope from'
449
+ # address for the Mail message. Setting this is useful when you want delivery notifications
450
+ # sent to a different address than the one in <tt>:from</tt>. Mail will actually use the
451
+ # <tt>:return_path</tt> in preference to the <tt>:sender</tt> in preference to the <tt>:from</tt>
452
+ # field for the 'envelope from' value.
453
+ #
454
+ # If you do not pass a block to the +mail+ method, it will find all templates in the
455
+ # template path that match the method name that it is being called from, it will then
456
+ # create parts for each of these templates intelligently, making educated guesses
457
+ # on correct content type and sequence, and return a fully prepared Mail::Message
458
+ # ready to call <tt>:deliver</tt> on to send.
459
+ #
460
+ # If you do pass a block, you can render specific templates of your choice:
461
+ #
462
+ # mail(:to => 'mikel@test.lindsaar.net') do |format|
463
+ # format.text
464
+ # format.html
465
+ # end
466
+ #
467
+ # You can even render text directly without using a template:
468
+ #
469
+ # mail(:to => 'mikel@test.lindsaar.net') do |format|
470
+ # format.text { render :text => "Hello Mikel!" }
471
+ # format.html { render :text => "<h1>Hello Mikel!</h1>" }
472
+ # end
473
+ #
474
+ # Which will render a <tt>multipart/alternative</tt> email with <tt>text/plain</tt> and
475
+ # <tt>text/html</tt> parts.
476
+ #
477
+ # The block syntax also allows you to customize the part headers if desired:
478
+ #
479
+ # mail(:to => 'mikel@test.lindsaar.net') do |format|
480
+ # format.text(:content_transfer_encoding => "base64")
481
+ # format.html
482
+ # end
483
+ #
484
+ def mail(headers={}, &block)
485
+ # Guard flag to prevent both the old and the new API from firing
486
+ # Should be removed when old API is removed
487
+ @mail_was_called = true
488
+ m = @_message
489
+
490
+ # At the beginning, do not consider class default for parts order neither content_type
491
+ content_type = headers[:content_type]
492
+ parts_order = headers[:parts_order]
493
+
494
+ # Merge defaults from class
495
+ headers = headers.reverse_merge(self.class.default)
496
+ charset = headers[:charset]
497
+
498
+ # Quote fields
499
+ headers[:subject] ||= default_i18n_subject
500
+ quote_fields!(headers, charset)
501
+
502
+ # Render the templates and blocks
503
+ responses, explicit_order = collect_responses_and_parts_order(headers, &block)
504
+ create_parts_from_responses(m, responses, charset)
505
+
506
+ # Finally setup content type and parts order
507
+ m.content_type = set_content_type(m, content_type, headers[:content_type])
508
+ m.charset = charset
509
+
510
+ if m.multipart?
511
+ parts_order ||= explicit_order || headers[:parts_order]
512
+ m.body.set_sort_order(parts_order)
513
+ m.body.sort_parts!
589
514
  end
590
515
 
591
- def render(opts)
592
- body = opts.delete(:body)
593
- if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render))
594
- opts[:file] = "#{mailer_name}/#{opts[:file]}"
595
- end
516
+ # Set configure delivery behavior
517
+ wrap_delivery_behavior!(headers[:delivery_method])
596
518
 
597
- begin
598
- old_template, @template = @template, initialize_template_class(body)
599
- layout = respond_to?(:pick_layout, true) ? pick_layout(opts) : false
600
- @template.render(opts.merge(:layout => layout))
601
- ensure
602
- @template = old_template
603
- end
604
- end
519
+ # Remove headers already treated and assign all others
520
+ headers.except!(:subject, :to, :from, :cc, :bcc, :reply_to)
521
+ headers.except!(:body, :parts_order, :content_type, :charset, :delivery_method)
522
+ headers.each { |k, v| m[k] = v }
605
523
 
606
- def default_template_format
607
- if @current_template_content_type
608
- Mime::Type.lookup(@current_template_content_type).to_sym
609
- else
610
- :html
611
- end
612
- end
613
-
614
- def candidate_for_layout?(options)
615
- !self.view_paths.find_template(default_template_name, default_template_format).exempt_from_layout?
616
- rescue ActionView::MissingTemplate
617
- return true
618
- end
619
-
620
- def template_root
621
- self.class.template_root
622
- end
524
+ m
525
+ end
623
526
 
624
- def template_root=(root)
625
- self.class.template_root = root
527
+ protected
528
+
529
+ def set_content_type(m, user_content_type, class_default)
530
+ params = m.content_type_parameters || {}
531
+ case
532
+ when user_content_type.present?
533
+ user_content_type
534
+ when m.has_attachments?
535
+ ["multipart", "mixed", params]
536
+ when m.multipart?
537
+ ["multipart", "alternative", params]
538
+ else
539
+ m.content_type || class_default
626
540
  end
541
+ end
627
542
 
628
- def template_path
629
- File.join(template_root, mailer_name)
630
- end
543
+ def default_i18n_subject #:nodoc:
544
+ mailer_scope = self.class.mailer_name.gsub('/', '.')
545
+ I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
546
+ end
631
547
 
632
- def initialize_template_class(assigns)
633
- template = ActionView::Base.new(self.class.view_paths, assigns, self)
634
- template.template_format = default_template_format
635
- template
636
- end
548
+ # TODO: Move this into Mail
549
+ def quote_fields!(headers, charset) #:nodoc:
550
+ m = @_message
551
+ m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject]
552
+ m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to]
553
+ m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from]
554
+ m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc]
555
+ m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc]
556
+ m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to]
557
+ end
637
558
 
638
- def sort_parts(parts, order = [])
639
- order = order.collect { |s| s.downcase }
640
-
641
- parts = parts.sort do |a, b|
642
- a_ct = a.content_type.downcase
643
- b_ct = b.content_type.downcase
644
-
645
- a_in = order.include? a_ct
646
- b_in = order.include? b_ct
647
-
648
- s = case
649
- when a_in && b_in
650
- order.index(a_ct) <=> order.index(b_ct)
651
- when a_in
652
- -1
653
- when b_in
654
- 1
655
- else
656
- a_ct <=> b_ct
657
- end
658
-
659
- # reverse the ordering because parts that come last are displayed
660
- # first in mail clients
661
- (s * -1)
559
+ def collect_responses_and_parts_order(headers) #:nodoc:
560
+ responses, parts_order = [], nil
561
+
562
+ if block_given?
563
+ collector = ActionMailer::Collector.new(self) { render(action_name) }
564
+ yield(collector)
565
+ parts_order = collector.responses.map { |r| r[:content_type] }
566
+ responses = collector.responses
567
+ elsif headers[:body]
568
+ responses << {
569
+ :body => headers[:body],
570
+ :content_type => self.class.default[:content_type] || "text/plain"
571
+ }
572
+ else
573
+ each_template do |template|
574
+ responses << {
575
+ :body => render_to_body(:_template => template),
576
+ :content_type => template.mime_type.to_s
577
+ }
662
578
  end
663
-
664
- parts
665
579
  end
666
580
 
667
- def create_mail
668
- m = TMail::Mail.new
669
-
670
- m.subject, = quote_any_if_necessary(charset, subject)
671
- m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
672
- m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
673
- m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
674
- m.reply_to = quote_address_if_necessary(reply_to, charset) unless reply_to.nil?
675
- m.mime_version = mime_version unless mime_version.nil?
676
- m.date = sent_on.to_time rescue sent_on if sent_on
677
-
678
- headers.each { |k, v| m[k] = v }
679
-
680
- real_content_type, ctype_attrs = parse_content_type
681
-
682
- if @parts.empty?
683
- m.set_content_type(real_content_type, nil, ctype_attrs)
684
- m.body = normalize_new_lines(body)
685
- else
686
- if String === body
687
- part = TMail::Mail.new
688
- part.body = normalize_new_lines(body)
689
- part.set_content_type(real_content_type, nil, ctype_attrs)
690
- part.set_content_disposition "inline"
691
- m.parts << part
692
- end
693
-
694
- @parts.each do |p|
695
- part = (TMail::Mail === p ? p : p.to_mail(self))
696
- m.parts << part
697
- end
698
-
699
- if real_content_type =~ /multipart/
700
- ctype_attrs.delete "charset"
701
- m.set_content_type(real_content_type, nil, ctype_attrs)
702
- end
703
- end
704
-
705
- @mail = m
706
- end
581
+ [responses, parts_order]
582
+ end
707
583
 
708
- def perform_delivery_smtp(mail)
709
- destinations = mail.destinations
710
- mail.ready_to_send
711
- sender = (mail['return-path'] && mail['return-path'].spec) || Array(mail.from).first
584
+ def each_template(&block) #:nodoc:
585
+ self.class.view_paths.each do |load_paths|
586
+ templates = load_paths.find_all(action_name, {}, self.class.mailer_name)
587
+ templates = templates.uniq_by { |t| t.details[:formats] }
712
588
 
713
- smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
714
- smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
715
- smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
716
- smtp_settings[:authentication]) do |smtp|
717
- smtp.sendmail(mail.encoded, sender, destinations)
589
+ unless templates.empty?
590
+ templates.each(&block)
591
+ return
718
592
  end
719
593
  end
594
+ end
720
595
 
721
- def perform_delivery_sendmail(mail)
722
- sendmail_args = sendmail_settings[:arguments]
723
- sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path']
724
- IO.popen("#{sendmail_settings[:location]} #{sendmail_args}","w+") do |sm|
725
- sm.print(mail.encoded.gsub(/\r/, ''))
726
- sm.flush
727
- end
596
+ def create_parts_from_responses(m, responses, charset) #:nodoc:
597
+ if responses.size == 1 && !m.has_attachments?
598
+ responses[0].each { |k,v| m[k] = v }
599
+ elsif responses.size > 1 && m.has_attachments?
600
+ container = Mail::Part.new
601
+ container.content_type = "multipart/alternative"
602
+ responses.each { |r| insert_part(container, r, charset) }
603
+ m.add_part(container)
604
+ else
605
+ responses.each { |r| insert_part(m, r, charset) }
728
606
  end
607
+ end
729
608
 
730
- def perform_delivery_test(mail)
731
- deliveries << mail
732
- end
733
- end
609
+ def insert_part(container, response, charset) #:nodoc:
610
+ response[:charset] ||= charset
611
+ part = Mail::Part.new(response)
612
+ container.add_part(part)
613
+ end
734
614
 
735
- Base.class_eval do
736
- include Helpers
737
- helper MailHelper
738
615
  end
739
616
  end