Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 paneq authored
2  actionmailer/CHANGELOG.md
View
@@ -4,6 +4,8 @@
* Asynchronously send messages via the Rails Queue *Brian Cardarella*
+* Set default Action Mailer options via config.action_mailer.default_options= *Robert Pankowecki*
+
## Rails 3.2.5 (Jun 1, 2012) ##
* No changes.
5 actionmailer/lib/action_mailer/base.rb
View
@@ -421,6 +421,9 @@ def default(value = nil)
self.default_params = default_params.merge(value).freeze if value
default_params
end
+ #Alias so that we can use it in config/application.rb which requires setters
+ #: config.action_mailer.default_options = {from: "no-replay@example.org"}
+ alias :default_options= :default
# Receives a raw email, parses it into an email object, decodes it,
# instantiates a new mailer, and passes the email object to the mailer
@@ -786,4 +789,4 @@ def insert_part(container, response, charset) #:nodoc:
ActiveSupport.run_load_hooks(:action_mailer, self)
end
-end
+end
13 actionmailer/test/base_test.rb
View
@@ -653,6 +653,19 @@ def welcome
assert_equal "Anonymous mailer body", mailer.welcome.body.encoded.strip
end
+ test "default_from can be set" do
+ class DefaultFromMailer < ActionMailer::Base
+ default :to => 'system@test.lindsaar.net'
+ self.default_options = {from: "robert.pankowecki@gmail.com"}
+
+ def welcome
+ mail(subject: "subject")
+ end
+ end
+
+ assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
+ end
+
protected
# Execute the block setting the given values and restoring old values after
4 guides/source/configuring.textile
View
@@ -424,12 +424,12 @@ There are a number of settings available on +config.action_mailer+:
* +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.
-* +config.action_mailer.default+ configures Action Mailer defaults. These default to:
+* +config.action_mailer.default_options+ configures Action Mailer defaults. Use to set options like `from` or `replay_to` for every mailer. These default to:
<ruby>
:mime_version => "1.0",
:charset => "UTF-8",
:content_type => "text/plain",
-:parts_order => [ "text/plain", "text/enriched", "text/html" ]
+:parts_order => [ "text/plain", "text/enriched", "text/html" ],
</ruby>
* +config.action_mailer.observers+ registers observers which will be notified when mail is delivered.
Please sign in to comment.
Something went wrong with that request. Please try again.