Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between config.action_mailer.smtp_settings and ActionMailer::Base.smtp_settings in Rails?

I set up an Exchange compatible mail server in a RoR application. I used the following setup in development.rb:

config.action_mailer.smtp_settings = {
  :address              => 'mail.server.com',
  :port                 => 5870,
  :user_name            => 'username',
  :password             => 'password',
  :authentication       => :login
}

This setup does not work, I get Net::SMTPAuthenticationError: 504 Unrecognized authentication type.

However if I apply the exact same configuration in environment.rb, it works perfectly:

ActionMailer::Base.smtp_settings = {
  :address              => 'mail.server.com',
  :port                 => 5870,
  :user_name            => 'username',
  :password             => 'password',
  :authentication       => :login  
}

Why is this? Shouldn't config.action_mailer.smtp_settings set the same settings? Is this a bug? Does it have a reason?

I tried it with Gmail as told here, and it works, so smtp_settings does have effect on the mailer, but it seems to me like not all the options count/work.

like image 535
andrasf Avatar asked Jul 04 '12 20:07

andrasf


People also ask

What is ActionMailer?

1 Introduction. Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.


2 Answers

config.action_mailer.smtp_settings forwards the settings to ActionMailer::Base.smtp_settings.

However, the main difference is that the former is an environment-specific setting while the second one is a global setting.

In other word, you should be sure to set config.action_mailer.smtp_settings in the proper environment file to apply the settings. If you want to use the settings in production, for example, add the assignment in the config/environments/production.rb file. Likewise, if you want the settings to be applied to the entire project, set them in config/application.rb.

like image 189
Simone Carletti Avatar answered Sep 20 '22 19:09

Simone Carletti


I had similar problem, the config.action_mailer.smtp_settings did not work, but every configuration was proper.

Finally i figured out, that a Rails extension overrides the ActionMailer::Base settings in the initializer phase... So investigate all your code and all the 3rd party codes (!), extensions, modules, etc. there should be the problem!

like image 42
tompata Avatar answered Sep 17 '22 19:09

tompata