Skip to content
Browse files

Added delivers_from.

  • Loading branch information...
1 parent 48faf53 commit e1c131863897390d04bd5515765236590747f2c1 José Valim and Mikel Lindsaar committed Jan 25, 2010
Showing with 18 additions and 5 deletions.
  1. +12 −2 actionmailer/lib/action_mailer/base.rb
  2. +6 −3 actionmailer/test/base_test.rb
View
14 actionmailer/lib/action_mailer/base.rb
@@ -273,6 +273,9 @@ class Base < AbstractController::Base
private_class_method :new #:nodoc:
+ extlib_inheritable_accessor :default_from
+ self.default_from = nil
+
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -298,6 +301,12 @@ def mailer_name
attr_writer :mailer_name
alias :controller_path :mailer_name
+ # Sets who is the default sender for the e-mail
+ def delivers_from(value = nil)
+ self.default_from = value if value
+ self.default_from
+ end
+
# Receives a raw email, parses it into an email object, decodes it,
# instantiates a new mailer, and passes the email object to the mailer
# object's +receive+ method. If you want your mailer to be able to
@@ -318,7 +327,7 @@ def receive(raw_mail)
end
# TODO The delivery should happen inside the instrument block
- def delivered_email(mail)
+ def delivered_email(mail) #:nodoc:
ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload|
self.set_payload_for_mail(payload, mail)
end
@@ -387,8 +396,9 @@ def mail(headers={}, &block)
charset = headers[:charset] || m.charset || self.class.default_charset.dup
mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup
- # Set subjects and fields quotings
+ # Set fields quotings
headers[:subject] ||= default_subject
+ headers[:from] ||= self.class.default_from.dup
quote_fields!(headers, charset)
# Render the templates and blocks
View
9 actionmailer/test/base_test.rb
@@ -1,16 +1,14 @@
# encoding: utf-8
require 'abstract_unit'
-# class Notifier < ActionMailer::Base
-# delivers_from 'notifications@example.com'
class BaseTest < ActiveSupport::TestCase
DEFAULT_HEADERS = {
:to => 'mikel@test.lindsaar.net',
- :from => 'jose@test.plataformatec.com',
:subject => 'The first email on new API!'
}
class BaseMailer < ActionMailer::Base
+ delivers_from 'jose@test.plataformatec.com'
self.mailer_name = "base_mailer"
def welcome(hash = {})
@@ -72,6 +70,11 @@ def explicit_multipart_with_any(hash = {})
assert_equal(email.subject, 'The first email on new API!')
end
+ test "mail() with from overwrites the class level default" do
+ email = BaseMailer.welcome(:from => 'someone@else.com').deliver
+ assert_equal(email.from, ['someone@else.com'])
+ end
+
test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do
@time = Time.now
email = BaseMailer.welcome(:bcc => 'bcc@test.lindsaar.net',

0 comments on commit e1c1318

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