From e1c131863897390d04bd5515765236590747f2c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?=
 <pair@programming.com>
Date: Mon, 25 Jan 2010 00:37:12 +0100
Subject: Added delivers_from.

---
 actionmailer/lib/action_mailer/base.rb | 14 ++++++++++++--
 actionmailer/test/base_test.rb         |  9 ++++++---
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 84d50fa1f4..552fd7ccb8 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -273,6 +273,9 @@ module ActionMailer #:nodoc:
 
     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 @@ module ActionMailer #:nodoc:
       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 @@ module ActionMailer #:nodoc:
       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 @@ module ActionMailer #:nodoc:
       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
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index b8d21132de..d8616ee3be 100644
--- a/actionmailer/test/base_test.rb
+++ b/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 @@ class BaseTest < ActiveSupport::TestCase
     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',
-- 
cgit v1.2.3