aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb57
-rw-r--r--actionmailer/test/base_test.rb112
2 files changed, 104 insertions, 65 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 95c45ec54b..f677ab629e 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -23,7 +23,7 @@ module ActionMailer #:nodoc:
# Examples:
#
# class Notifier < ActionMailer::Base
- # delivers_from 'system@example.com'
+ # defaults({:from => 'system@example.com'})
#
# def welcome(recipient)
# @account = recipient
@@ -40,13 +40,17 @@ module ActionMailer #:nodoc:
# * <tt>headers[]=</tt> - Allows you to specify non standard headers in your email such
# as <tt>headers['X-No-Spam'] = 'True'</tt>
#
+ # * <tt>headers(hash)</tt> - Allows you to specify multiple headers in your email such
+ # as <tt>headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'})</tt>
+ #
# * <tt>mail</tt> - Allows you to specify your email to send.
#
- # The hash passed to the mail method allows you to specify the most used headers in an email
- # message, such as <tt>Subject</tt>, <tt>To</tt>, <tt>From</tt>, <tt>Cc</tt>, <tt>Bcc</tt>,
- # <tt>Reply-To</tt> and <tt>Date</tt>. See the <tt>ActionMailer#mail</tt> method for more details.
- #
- # If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
+ # The hash passed to the mail method allows you to specify any header that a Mail::Message
+ # will accept (any valid Email header including optional fields). Obviously if you specify
+ # the same header in the headers method and then again in the mail method, the last one
+ # will over write the first, unless you are specifying a header field that can appear more
+ # than once per RFC, in which case, both will be inserted (X-value headers for example can
+ # appear multiple times.)
#
# The mail method, if not passed a block, will inspect your views and send all the views with
# the same name as the method, so the above action would send the +welcome.plain.erb+ view file
@@ -186,9 +190,14 @@ module ActionMailer #:nodoc:
#
# These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
#
- # * <tt>delivers_from</tt> - Pass this the address that then defaults as the +from+ address on all the
- # emails sent. Can be overridden on a per mail basis by passing <tt>:from => 'another@address'</tt> in
- # the +mail+ method.
+ # * <tt>defaults</tt> - This is a class wide hash of <tt>:key => value</tt> pairs containing
+ # default values for the specified header fields of the <tt>Mail::Message</tt>. You can
+ # specify a default for any valid header for <tt>Mail::Message</tt> and it will be used if
+ # you do not override it. The defaults set by Action Mailer are:
+ # * <tt>:mime_version => "1.0"</tt>
+ # * <tt>:charset => "utf-8",</tt>
+ # * <tt>:content_type => "text/plain",</tt>
+ # * <tt>:parts_order => [ "text/plain", "text/enriched", "text/html" ]</tt>
#
# * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
# Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
@@ -222,20 +231,19 @@ module ActionMailer #:nodoc:
# * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with <tt>delivery_method :test</tt>. Most useful
# for unit and functional testing.
#
- # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
- # pick a different charset from inside a method with +charset+.
+ # * <tt>default_charset</tt> - This is now deprecated, use the +defaults+ method above to
+ # set the default +:charset+.
#
- # * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
- # can also pick a different content type from inside a method with +content_type+.
+ # * <tt>default_content_type</tt> - This is now deprecated, use the +defaults+ method above
+ # to set the default +:content_type+.
#
- # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to <tt>1.0</tt>. You
- # can also pick a different value from inside a method with +mime_version+.
+ # * <tt>default_mime_version</tt> - This is now deprecated, use the +defaults+ method above
+ # to set the default +:mime_version+.
#
- # * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
- # which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
- # <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
- # and appear last in the mime encoded message. You can also pick a different order from inside a method with
- # +implicit_parts_order+.
+ # * <tt>default_implicit_parts_order</tt> - This is now deprecated, use the +defaults+ method above
+ # to set the default +:parts_order+. Parts Order is used when a message is built implicitly
+ # (i.e. multiple parts are assembled from templates which specify the content type in their
+ # filenames) this variable controls how the parts are ordered.
class Base < AbstractController::Base
include DeliveryMethods, Quoting
abstract!
@@ -348,13 +356,18 @@ module ActionMailer #:nodoc:
#
# headers['X-Special-Domain-Specific-Header'] = "SecretValue"
#
+ # You can also pass a hash into headers of header field names and values, which
+ # will then be set on the Mail::Message object:
+ #
+ # headers {'X-Special-Domain-Specific-Header' => "SecretValue",
+ # 'In-Reply-To' => incoming.message_id }
+ #
# The resulting Mail::Message will have the following in it's header:
#
# X-Special-Domain-Specific-Header: SecretValue
def headers(args=nil)
if args
- ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller[0,2]
- @headers = args
+ @_message.headers(args)
else
@_message
end
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 7abdf7f4f8..6900e77178 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -5,15 +5,24 @@ class BaseTest < ActiveSupport::TestCase
class BaseMailer < ActionMailer::Base
self.mailer_name = "base_mailer"
- self.defaults :to => 'system@test.lindsaar.net',
- :from => 'jose@test.plataformatec.com',
- :reply_to => 'mikel@test.lindsaar.net'
+ defaults({:to => 'system@test.lindsaar.net',
+ :from => 'jose@test.plataformatec.com',
+ :reply_to => 'mikel@test.lindsaar.net'})
def welcome(hash = {})
headers['X-SPAM'] = "Not SPAM"
mail({:subject => "The first email on new API!"}.merge!(hash))
end
+ def simple(hash = {})
+ mail(hash)
+ end
+
+ def simple_with_headers(hash = {})
+ headers hash
+ mail
+ end
+
def attachment_with_content(hash = {})
attachments['invoice.pdf'] = 'This is test File content'
mail(hash)
@@ -64,12 +73,12 @@ class BaseTest < ActiveSupport::TestCase
end
test "method call to mail does not raise error" do
- assert_nothing_raised { BaseMailer.welcome.deliver }
+ assert_nothing_raised { BaseMailer.welcome }
end
# Basic mail usage without block
test "mail() should set the headers of the mail message" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal(['system@test.lindsaar.net'], email.to)
assert_equal(['jose@test.plataformatec.com'], email.from)
assert_equal('The first email on new API!', email.subject)
@@ -77,7 +86,7 @@ class BaseTest < ActiveSupport::TestCase
test "mail() with from overwrites the class level default" do
email = BaseMailer.welcome(:from => 'someone@example.com',
- :to => 'another@example.org').deliver
+ :to => 'another@example.org')
assert_equal(['someone@example.com'], email.from)
assert_equal(['another@example.org'], email.to)
end
@@ -90,7 +99,7 @@ class BaseTest < ActiveSupport::TestCase
:charset => 'iso-8559-1',
:mime_version => '2.0',
:reply_to => 'reply-to@test.lindsaar.net',
- :date => @time).deliver
+ :date => @time)
assert_equal(['bcc@test.lindsaar.net'], email.bcc)
assert_equal(['cc@test.lindsaar.net'], email.cc)
assert_equal('multipart/mixed', email.content_type)
@@ -101,50 +110,50 @@ class BaseTest < ActiveSupport::TestCase
end
test "mail() renders the template using the method being processed" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
end
test "can pass in :body to the mail method hash" do
- email = BaseMailer.welcome(:body => "Hello there").deliver
+ email = BaseMailer.welcome(:body => "Hello there")
assert_equal("text/plain", email.mime_type)
assert_equal("Hello there", email.body.encoded)
end
# Custom headers
test "custom headers" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Not SPAM", email['X-SPAM'].decoded)
end
# Attachments
test "attachment with content" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal('invoice.pdf', email.attachments[0].filename)
assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded)
end
test "attachment gets content type from filename" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal('invoice.pdf', email.attachments[0].filename)
end
test "attachment with hash" do
- email = BaseMailer.attachment_with_hash.deliver
+ email = BaseMailer.attachment_with_hash
assert_equal(1, email.attachments.length)
assert_equal('invoice.jpg', email.attachments[0].filename)
assert_equal("\312\213\254\232)b", email.attachments['invoice.jpg'].decoded)
end
test "sets mime type to multipart/mixed when attachment is included" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal("multipart/mixed", email.mime_type)
end
test "adds the rendered template as part" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@@ -154,7 +163,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "adds the given :body as part" do
- email = BaseMailer.attachment_with_content(:body => "I'm the eggman").deliver
+ email = BaseMailer.attachment_with_content(:body => "I'm the eggman")
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -166,54 +175,54 @@ class BaseTest < ActiveSupport::TestCase
# Defaults values
test "uses default charset from class" do
with_default BaseMailer, :charset => "US-ASCII" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("US-ASCII", email.charset)
- email = BaseMailer.welcome(:charset => "iso-8559-1").deliver
+ email = BaseMailer.welcome(:charset => "iso-8559-1")
assert_equal("iso-8559-1", email.charset)
end
end
test "uses default content type from class" do
with_default BaseMailer, :content_type => "text/html" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("text/html", email.mime_type)
- email = BaseMailer.welcome(:content_type => "text/plain").deliver
+ email = BaseMailer.welcome(:content_type => "text/plain")
assert_equal("text/plain", email.mime_type)
end
end
test "uses default mime version from class" do
with_default BaseMailer, :mime_version => "2.0" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("2.0", email.mime_version)
- email = BaseMailer.welcome(:mime_version => "1.0").deliver
+ email = BaseMailer.welcome(:mime_version => "1.0")
assert_equal("1.0", email.mime_version)
end
end
- test "uses default headers from class" do
+ test "uses random default headers from class" do
with_default BaseMailer, "X-SPAM" => "Not spam" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.simple
assert_equal("Not spam", email["X-SPAM"].decoded)
end
end
test "subject gets default from I18n" do
BaseMailer.defaults[:subject] = nil
- email = BaseMailer.welcome(:subject => nil).deliver
+ email = BaseMailer.welcome(:subject => nil)
assert_equal "Welcome", email.subject
I18n.backend.store_translations('en', :actionmailer => {:base_mailer => {:welcome => {:subject => "New Subject!"}}})
- email = BaseMailer.welcome(:subject => nil).deliver
+ email = BaseMailer.welcome(:subject => nil)
assert_equal "New Subject!", email.subject
end
# Implicit multipart
test "implicit multipart" do
- email = BaseMailer.implicit_multipart.deliver
+ email = BaseMailer.implicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -225,18 +234,18 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with sort order" do
order = ["text/html", "text/plain"]
with_default BaseMailer, :parts_order => order do
- email = BaseMailer.implicit_multipart.deliver
+ email = BaseMailer.implicit_multipart
assert_equal("text/html", email.parts[0].mime_type)
assert_equal("text/plain", email.parts[1].mime_type)
- email = BaseMailer.implicit_multipart(:parts_order => order.reverse).deliver
+ email = BaseMailer.implicit_multipart(:parts_order => order.reverse)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
end
end
test "implicit multipart with attachments creates nested parts" do
- email = BaseMailer.implicit_multipart(:attachments => true).deliver
+ email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@@ -248,7 +257,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with attachments and sort order" do
order = ["text/html", "text/plain"]
with_default BaseMailer, :parts_order => order do
- email = BaseMailer.implicit_multipart(:attachments => true).deliver
+ email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[1].mime_type)
@@ -257,7 +266,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "implicit multipart with default locale" do
- email = BaseMailer.implicit_with_locale.deliver
+ email = BaseMailer.implicit_with_locale
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -268,7 +277,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with other locale" do
swap I18n, :locale => :pl do
- email = BaseMailer.implicit_with_locale.deliver
+ email = BaseMailer.implicit_with_locale
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -281,7 +290,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with several view paths uses the first one with template" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path"))
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome from another path", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@@ -291,7 +300,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with inexistent templates uses the next view path" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown"))
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@@ -300,7 +309,7 @@ class BaseTest < ActiveSupport::TestCase
# Explicit multipart
test "explicit multipart" do
- email = BaseMailer.explicit_multipart.deliver
+ email = BaseMailer.explicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -312,18 +321,18 @@ class BaseTest < ActiveSupport::TestCase
test "explicit multipart does not sort order" do
order = ["text/html", "text/plain"]
with_default BaseMailer, :parts_order => order do
- email = BaseMailer.explicit_multipart.deliver
+ email = BaseMailer.explicit_multipart
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
- email = BaseMailer.explicit_multipart(:parts_order => order.reverse).deliver
+ email = BaseMailer.explicit_multipart(:parts_order => order.reverse)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
end
end
test "explicit multipart with attachments creates nested parts" do
- email = BaseMailer.explicit_multipart(:attachments => true).deliver
+ email = BaseMailer.explicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@@ -333,7 +342,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with templates" do
- email = BaseMailer.explicit_multipart_templates.deliver
+ email = BaseMailer.explicit_multipart_templates
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@@ -343,7 +352,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with any" do
- email = BaseMailer.explicit_multipart_with_any.deliver
+ email = BaseMailer.explicit_multipart_with_any
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -353,7 +362,8 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with options" do
- email = BaseMailer.custom_block(true).deliver
+ email = BaseMailer.custom_block(true)
+ email.ready_to_send!
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -363,7 +373,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with one part is rendered as body" do
- email = BaseMailer.custom_block.deliver
+ email = BaseMailer.custom_block
assert_equal(0, email.parts.size)
assert_equal("text/plain", email.mime_type)
assert_equal("base64", email.content_transfer_encoding)
@@ -407,6 +417,22 @@ class BaseTest < ActiveSupport::TestCase
mail = BaseMailer.explicit_multipart
assert_not_nil(mail.content_type_parameters[:boundary])
end
+
+ test "can pass random headers in as a hash" do
+ hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
+ 'In-Reply-To' => '1234@mikel.me.com' }
+ mail = BaseMailer.simple_with_headers(hash)
+ assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
+ assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
+ end
+
+ test "can pass random headers in as a hash to mail" do
+ hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
+ 'In-Reply-To' => '1234@mikel.me.com' }
+ mail = BaseMailer.simple(hash)
+ assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
+ assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
+ end
protected