即时更改 http_basic_authenticate_with 凭据,rails

Change http_basic_authenticate_with credentials on fly, rails

我需要用户可以即时更改 ActionMailer 设置,而无需重新启动服务器。我正在尝试在我的邮件程序上这样做 class

class CustomerMailer < ApplicationMailer
  self.smtp_settings = {
    address:             "smtp.gmail.com",
    port:                587,
    user_name:           settings[:delivery_email_login],
    password:            settings[:delivery_email_password],
    authentication:      "plain",
    enable_sarttls_auto: true
  }

  def customers_info_email(*some_args)
    # code
end

结束

但是修改需要重启服务器才能生效。

更新

感谢 Anthony L 动态更改 smtp 设置已经解决,但我还有另一个问题。

http_basic_authenticate_with name: login, password: password

这种情况下如何动态更改身份验证凭据?

不要直接更改 smtp_settings,而是在邮件方法上使用 delivery_method_options。例如:

delivery_options = {
        address:    "smtp.gmail.com",
        port:       587,
        domain:     "your_domain.com",
        user_name:  delivery_email_login,
        password:   delivery_email_password,
        authentication: :login,
        enable_starttls_auto: true
      }

mail(from: from_email, 
     to: to_email, 
     subject: subject,
     delivery_method_options: delivery_options)

有关详细信息,请参阅 http://edgeguides.rubyonrails.org/action_mailer_basics.html