0

In our rails app, we would like to load the mail server configuration during the startup. Here is the what we have in config/environment/development.rb:

  #Action Mailer
  config.action_mailer.smtp_settings = eval(Authentify::AuthentifyUtility.find_config_const('development_smtp_setting'))

The Authentify::AuthentifyUtility.find_config_const('development_smtp_setting') shall return:

 {  
   :address              => "mymail.com",  
    :port                 => 587,    
    :user_name            => "smtp_login_name",  
    :password             => "password",  
    :authentication       => :plain,
    :enable_starttls_auto => false
  }

However rails server throws out error:

activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
: ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

It seems that there is no db connection during the load time. What's the right way to load the mail server config? Thanks.

user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

2

Do you need to load it from the DB? Usually you either load from a file or an environment variable. Also take a look at config.after_initialize http://guides.rubyonrails.org/configuring.html#rails-general-configuration

Josh
  • 8,329
  • 4
  • 36
  • 33
  • Yes, the mail config params need to be loaded from database table. – user938363 Sep 22 '13 at 13:11
  • config.after_initialize works. Thanks : config.after_initialize do config.action_mailer.smtp_settings = eval(Authentify::AuthentifyUtility.find_config_const('development_smtp_setting')) end – user938363 Sep 22 '13 at 14:11
  • Great! FWIW `eval` is a MAJOR security risk! http://stackoverflow.com/questions/637421/is-eval-supposed-to-be-nasty You're better off parsing the string, or storing it as JSON or YAML and using the built in parsers. This post is a good place to start: http://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object This will be MUCH safer and probably faster too! You can even have rails do the encode and decode for you: http://stackoverflow.com/questions/6694432/using-rails-serialize-to-save-hash-to-database – Josh Sep 23 '13 at 05:29
  • We don't eval user input and only eval our own code stored in readonly table. – user938363 Sep 26 '13 at 02:24