diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8fe5868d529ef..2eef57f7adcd0 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -446,6 +446,33 @@ def process(*args) #:nodoc: super end + class DeprecatedHeaderProxy < ActiveSupport::BasicObject + def initialize(message) + @message = message + end + + def []=(key, value) + unless value.is_a?(String) + ActiveSupport::Deprecation.warn("Using a non-String object for a header's value is deprecated. " \ + "You specified #{value.inspect} (a #{value.class}) for #{key}", caller) + + value = value.to_s + end + + @message[key] = value + end + + def headers(hash = {}) + hash.each_pair do |k,v| + self[k] = v + end + end + + def method_missing(meth, *args, &block) + @message.send(meth, *args, &block) + end + end + # Allows you to pass random and unusual headers to the new +Mail::Message+ object # which will add them to itself. # @@ -462,9 +489,9 @@ def process(*args) #:nodoc: # X-Special-Domain-Specific-Header: SecretValue def headers(args=nil) if args - @_message.headers(args) + DeprecatedHeaderProxy.new(@_message).headers(args) else - @_message + DeprecatedHeaderProxy.new(@_message) end end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index ef3808ee65fd4..7ae0b98eecd13 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -76,6 +76,11 @@ def teardown assert_equal("Not SPAM", email['X-SPAM'].decoded) end + test "deprecated non-String custom headers" do + email = assert_deprecated { BaseMailer.welcome_with_fixnum_header } + assert_equal("2", email['X-SPAM-COUNT'].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' } diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index 4f61e6978adfd..f11171875b77f 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -10,6 +10,11 @@ def welcome(hash = {}) mail({:subject => "The first email on new API!"}.merge!(hash)) end + def welcome_with_fixnum_header(hash = {}) + headers['X-SPAM-COUNT'] = 2 + mail({:template_name => "welcome", :subject => "The first email on new API!"}.merge!(hash)) + end + def welcome_with_headers(hash = {}) headers hash mail