8

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}> 

1 Answer 1

13

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!

Sign up to request clarification or add additional context in comments.

2 Comments

I'd really like to see the Stackoverflow SQL logs for when my question was posted vs when you answered (and solved) it... had to be < 30 seconds.
Great question! Great answer! Solved a knotty problem for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.