简体   繁体   中英

Rails action_mailer config not working in production

I am trying to setup action_mailer with Rails 4.1.0 to send emails.

In my config/environments/{env}.rb, I have:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.mandrillapp.com',
  port:                 587,
  domain:               'domain.com',
  authentication:       'login',
  user_name:            'email@domain.com',
  password:             'pass',
  enable_starttls_auto: true  }

It's working fine in development, but in prod it's trying to connect to localhost.

m = Mail.new
m.delivery_method
=> #<Mail::SMTP:0xbd79fdac @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}>

But when I do "puts Rails.application.config.action_mailer" Im getting the correct config

{:raise_delivery_errors=>false, :default_url_options=>{:host=>"domain.com"}, :delivery_method=>:smtp, :smtp_settings=>{:address=>"smtp.mandrillapp.com", :port=>587, :domain=>"domain.com", :authentication=>"login", :user_name=>"email@domain.com", :password=>"password", :enable_starttls_auto=>true}, :assets_dir=>"xxx", :javascripts_dir=>"xxx", :stylesheets_dir=>"xxx", :asset_host=>nil, :relative_url_root=>nil}

Any idea?

The Mail gem won't use your ActionMailer config by default. ActionMailer uses Mail but those configuration settings will only apply if you send mail via ActionMailer.

If it's working in your dev mode, then you certainly have configured the Mail gem separately in your development.rb environment file, or in an environment specific initializer.

I forgot I had this config in my development.rb

Mail.defaults do
  delivery_method Rails.configuration.action_mailer.delivery_method, Rails.configuration.action_mailer.smtp_settings
end

You need to add the host option for the default_url_options hash on the production.rb file, like so:

  config.action_mailer.default_url_options = { :host => ENV['HOST_DEFAULT_URL'], 
                                           only_path: false }

That should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM