Rails:ActionMailer的运行时配置?

我想通过Gmail从我的应用发送少量电子邮件。 现在,SMTP设置将在运行时确定(即:来自数据库),这可以完成吗?

—编辑—

我可以在其中一个类的方法中设置ActionMailer子类(名为Notifier)smtp设置。 这样我就可以设置用于动态发送电子邮件的用户名和密码。 唯一的事情是你必须设置所有smtp_settings。 是否可以在类方法中设置用户名和密码设置?

这是我现在正在使用的代码,它正在发送:

class Notifier  true, :address => "smtp.gmail.com", :port => "587", :domain => "mydomain.com", :authentication => :plain, :user_name => "fabian@mydomain.com", :password => "password" } recipients user.email subject "Test test" body "Test" end end 

我想在这里设置用户名和pw。

(导轨3)

因为我这样打电话给邮件:

 CustomerMailer.customer_auto_inform(@form).deliver 

在CustomerMailer类中,我有私有方法:

 def init_email_account(shop_mail) ActionMailer::Base.raise_delivery_errors = true ActionMailer::Base.smtp_settings = { :address => shop_mail.address, :port => shop_mail.port, :domain => shop_mail.domain, :user_name => shop_mail.user_name, :password => shop_mail.password, :authentication => shop_mail.authentication.name, :enable_starttls_auto => shop_mail.enable_starttls_auto } end 

在调用发送电子邮件的mail()之前,您需要调用私有方法init_email_account来填充数据库中的smtp_settings。 shop_mail是存储有关邮件帐户设置的数据的模型。

HTH

你可以在控制器中做到这一点:

 class ApplicationController < ActionController::Base ... private def set_mailer_settings ActionMailer::Base.smtp_settings.merge!({ username: 'username', password: 'yoursupersecretpassword' }) end end 

由于配置文件都是Ruby,因此可以在运行时从配置文件等中轻松获取设置。

这是我在一段时间内写的一篇文章, 让ActionMailer使用GMail SMTP 。

注意:如果您使用的是rails 2.3和Ruby 1.87,则不需要该插件,只需使用此评论中的设置即可

假设您已在环境或初始化程序中配置了smtp_settings,您可以像这样设置用户名:

 Notifier.smpt_settings.merge!({:user_name => "x", :password=> "y"})