Rails:使用动态传递选项发送电子邮件不起作用

时间:2017-03-28 14:21:13

标签: ruby-on-rails ruby email

根据文档发送带有动态传递选项的电子邮件,通过将新的传递选项哈希作为参数传递给mail()

class UserMailer < ApplicationMailer
  def welcome_email(user, company)
    @user = user
    @url  = user_url(@user)
    delivery_options = { user_name: company.smtp_user,
                         password: company.smtp_password,
                         address: company.smtp_host }
    mail(to: @user.email,
         subject: "Please see the Terms and Conditions attached",
         delivery_method_options: delivery_options)
  end
end

但是,邮件仍然使用环境配置文件中定义的默认传递选项发送,例如production.rb或development.rb:

config.action_mailer.smtp_settings = {
      :address => 'smtp.gmail.com',
      :port => 587,
      :domain => 'ourdomain.com',
      :user_name => 'default@ourdomain.com',
      :password => ENV['DEFAULT_PASSWORD'],
      :authentication => 'login',
      :enable_starttls_auto => true
  }

Rails 4。

有谁知道问题可能在哪里?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

生成邮件后覆盖传递方法设置:

class UserMailer < ApplicationMailer
  def welcome_email(user, company)
    @user = user
    @url  = user_url(@user)
    delivery_options = { user_name: company.smtp_user,
                         password: company.smtp_password,
                         address: company.smtp_host }
    mail = mail(to: @user.email,
         subject: "Please see the Terms and Conditions attached")

    mail.delivery_method.settings.merge!(delivery_options)
  end
end