【问题标题】:How to load the mail serve configuration during start time in rails 3.2?如何在 rails 3.2 的启动时间加载邮件服务配置?
【发布时间】:2013-09-20 23:14:54
【问题描述】:

在我们的 rails 应用程序中,我们希望在启动期间加载邮件服务器配置。这是我们在 config/environment/development.rb 中的内容:

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

Authentify::AuthentifyUtility.find_config_const('development_smtp_setting') 应返回:

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

但是rails server 抛出错误:

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

在加载期间似乎没有数据库连接。加载邮件服务器配置的正确方法是什么?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 smtp actionmailer


    【解决方案1】:

    您需要从数据库中加载它吗?通常,您要么从文件加载,要么从环境变量加载。也看看config.after_initializehttp://guides.rubyonrails.org/configuring.html#rails-general-configuration

    【讨论】:

    • 是的,邮件配置参数需要从数据库表中加载。
    • config.after_initialize 有效。谢谢:config.after_initialize 做 config.action_mailer.smtp_settings = eval(Authentify::AuthentifyUtility.find_config_const('development_smtp_setting')) end
    • 太棒了! FWIW eval 是一个重大的安全风险! stackoverflow.com/questions/637421/is-eval-supposed-to-be-nasty 您最好解析字符串,或者将其存储为 JSON 或 YAML 并使用内置解析器。这篇文章是一个很好的起点:stackoverflow.com/questions/1667630/… 这会更安全,也可能更快!您甚至可以让 rails 为您进行编码和解码:stackoverflow.com/questions/6694432/…
    • 我们不评估用户输入,只评估我们自己存储在只读表中的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2011-02-13
    • 2016-03-31
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多