views:

163

answers:

2

I'm trying to work on a Rails site here in it's development environment, where I want to test email delivery, and I can't work out why properties declared in the main environment.rb aren't being overwritten by the more specific development.rb file that I presume would be loaded when booting a rails app.

My understanding here is that values in more specific environment config files like this should override the shared 'environment.rb' config file, so if I have declared some email settings like so in config/environment.rb...

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.hostingcompany.com',
  :port           => 25,
  :domain         => 'productiondomain.net',
  :authentication => :login,
  :user_name      => "productiondomainmailer",
  :password       => "TOP_SEKRIT"
}

... then code here in config/environments/development.rb below should override the ActionMailer:Base.smtp_settings hash:

ActionMailer::Base.smtp_settings = {
  :domain         => 'developmentdomain.net'
}

However, when load the app in the development environment, or from script/console to check the value of ActionMailer::Base.smtp_settings[:domain], it's still listed as 'productiondomain.net'.

Why might this happening?

 

+1  A: 

This should work:

config.action_mailer.smtp_settings = {
  :address        => 'smtp.some_host.com',
  :port           => 25,
  :domain         => 'developmentdomain.net',
  :authentication => :login,
  :user_name      => 'blabla',
  :password       => "some_pass"  
}
khelll
+2  A: 

To set the configuration for your development environment put:

config.action_mailer.smtp_settings =

rather than:

ActionMailer::Base.smtp_settings =

in config/environments/development.rb

Note that in config/environments/development.rb you will need to specify the full set of settings and not just :domain as the hash overwrites the existing value rather than being merged with it.

And similarly, set the default in config/environments.rb by putting config.action_mailer.smtp_settings = inside the Rails::Initializer.run do |config| block.

mikej
This seems to have resolved it, thanks Mikej!I'm assuming changing settings inside the initializer loop, using `config.action_mailer.relevant_setting` is the preferred way to make these changes now over `ActionMailer::Base.relevant_setting` now. Why does changing it through the config block work, but not through the ActionMailer::Base class not work?
Chris Adams
The reason it doesn't work is due to the execution order. If you put ActionMailer::Base.smtp_settings = .. at the end of config/environments.rb, outside the Initializer.run block then this will run *after* the contents of config/environments/development.rbso will overwrite your development specific settings. If you try to reference ActionMailer::Base inside the block passed to Initializer.run this this will fail as the block is yielded to *before* the ActionMailer framework has been loaded by Initializer.process. Hence the use of config.action_mailer.smtp_settings = ..
mikej