Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActionMailer ignores settings in environment.rb

I put my ActionMailer config in my config/environment.rb file like so:

MyApp::Application.initialize!
MyApp::Application.configure do

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      address: "smtp.elasticemail.com",
      port: 2525,
      domain: "myapp.com",
      authentication: "plain",
      user_name: "my_username",
      password: "my_password",
      enable_starttls_auto: true
  }

end

My understanding is that this is the correct way to configure settings that will apply to all your environments.

This worked fine in development, but when I deployed to my staging server (which uses a custom config/environments/staging.rb config file) I got a "connection refused" error when it tried to deliver mail. staging.rb has no mailer-related settings in it at all.

So I fired up the console on the staging server with RAILS_ENV=staging rails c, and "puts Rails.application.config.action_mailer" shows that the settings I put in environment.rb are indeed in effect, but for some reason ActionMailer is not using them.

Through experimentation I found that copying the config directly into staging.rb solves the problem. Why is this necessary? If the rails console shows the settings are in effect, why isn't ActionMailer using them?

Digging deeper, I see that my mailer class's delivery_method is not set as expected:

MyMailer.foo(Person.find(1)).delivery_method

 => #<Mail::SMTP:0x0000000370d4d0 @settings={:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}> 
like image 332
George Armhold Avatar asked Jun 28 '12 20:06

George Armhold


1 Answers

put

MyApp::Application.configure do

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      address: "smtp.elasticemail.com",
      port: 2525,
      domain: "myapp.com",
      authentication: "plain",
      user_name: "my_username",
      password: "my_password",
      enable_starttls_auto: true
  }

end

before MyApp::Application.initialize!

like image 90
Luiz E. Avatar answered Sep 19 '22 23:09

Luiz E.