Skip to content
This repository
Browse code

Introduce config.action_mailer.default_from=

Allows to easily set :from, :replay_to, etc. options in
config/application.rb using simple syntax:

  config.action_mailer.default_options = {from:"no-replay@example.org"}

This was not possible using #default method because

  config.action_mailer.default(from: "no-replay@example.org")

is interpreated as reader method and just returns nil.
It would not call ActionMailer::Base.default method. The only
way of calling this method from config/application.rb was to use
the direct syntax which looks ugly in my opinion:

  config.assets.enabled = false
  config.assets.version = '1.0'
  config.encoding = "utf-8"
  config.action_mailer.default_url_options= {
    host:"example.org",
    protocol:"https"
  }
  ActionMailer::Base.default(from: "no-replay@example.org")
  • Loading branch information...
commit edaa2c48179681efd96d0cad4b5dea696a2a31fb 1 parent b7a4fe1
Robert Pankowecki authored June 15, 2012
2  actionmailer/CHANGELOG.md
Source Rendered
@@ -4,6 +4,8 @@
4 4
 
5 5
 * Asynchronously send messages via the Rails Queue *Brian Cardarella*
6 6
 
  7
+* Set default Action Mailer options via config.action_mailer.default_options= *Robert Pankowecki*
  8
+
7 9
 ## Rails 3.2.5 (Jun 1, 2012) ##
8 10
 
9 11
 *   No changes.
5  actionmailer/lib/action_mailer/base.rb
@@ -421,6 +421,9 @@ def default(value = nil)
421 421
         self.default_params = default_params.merge(value).freeze if value
422 422
         default_params
423 423
       end
  424
+      #Alias so that we can use it in config/application.rb which requires setters
  425
+      #: config.action_mailer.default_options = {from: "no-replay@example.org"}
  426
+      alias :default_options= :default
424 427
 
425 428
       # Receives a raw email, parses it into an email object, decodes it,
426 429
       # instantiates a new mailer, and passes the email object to the mailer
@@ -786,4 +789,4 @@ def insert_part(container, response, charset) #:nodoc:
786 789
 
787 790
     ActiveSupport.run_load_hooks(:action_mailer, self)
788 791
   end
789  
-end
  792
+end
13  actionmailer/test/base_test.rb
@@ -653,6 +653,19 @@ def welcome
653 653
     assert_equal "Anonymous mailer body", mailer.welcome.body.encoded.strip
654 654
   end
655 655
 
  656
+  test "default_from can be set" do
  657
+    class DefaultFromMailer < ActionMailer::Base
  658
+      default :to => 'system@test.lindsaar.net'
  659
+      self.default_options = {from: "robert.pankowecki@gmail.com"}
  660
+
  661
+      def welcome
  662
+        mail(subject: "subject")
  663
+      end
  664
+    end
  665
+
  666
+    assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
  667
+  end
  668
+
656 669
   protected
657 670
 
658 671
     # Execute the block setting the given values and restoring old values after
4  guides/source/configuring.textile
Source Rendered
@@ -424,12 +424,12 @@ There are a number of settings available on +config.action_mailer+:
424 424
 
425 425
 * +config.action_mailer.perform_deliveries+ specifies whether mail will actually be delivered and is true by default. It can be convenient to set it to false for testing.
426 426
 
427  
-* +config.action_mailer.default+ configures Action Mailer defaults. These default to:
  427
+* +config.action_mailer.default_options+ configures Action Mailer defaults. Use to set options like `from` or `replay_to` for every mailer. These default to:
428 428
 <ruby>
429 429
 :mime_version => "1.0",
430 430
 :charset      => "UTF-8",
431 431
 :content_type => "text/plain",
432  
-:parts_order  => [ "text/plain", "text/enriched", "text/html" ]
  432
+:parts_order  => [ "text/plain", "text/enriched", "text/html" ],
433 433
 </ruby>
434 434
 
435 435
 * +config.action_mailer.observers+ registers observers which will be notified when mail is delivered.

0 notes on commit edaa2c4

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