我正在从头开始一个新的 Rails 3 应用程序。
当我进行基本设置(配置 gem、 session 等)时,我遇到了困扰我一段时间的事情。
我们当前的系统(Ruby 脚本和 Rails 2 应用程序的混合体)向客户发送各种电子邮件/传真通知。在 80% 的情况下,某些事情很常见 - 抄送 - 我们端的某些电子邮件帐户和电子邮件签名。
之前我只是在environment.rb中定义了GLOBALS比如
SYSTEM_EMAIL_SIGNATURE
或
SYSTEM_EMAIL_NOTIFY
稍后在邮寄程序中使用它们,或者如果它是一个独立的脚本,我有一个 setup.rb 文件 - 它有一堆通用设置 - 包括像这样的自定义电子邮件设置。
因为我要从头开始重建这个应用程序并将所有脚本整合到一个 ruby 应用程序中 - 我试图想出一种更好的方法来做到这一点。
现在我正在设置一个具有 action_mailer 设置的 email.rb Initializer,我通过添加更多项目对其进行了扩展:
########## Setup Global Email Defaults ##############
Site::Application.configure do
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'mail.example.com',
:port => 25,
:domain => 'example.com',
# These are custom to OUR setup - used later in the code
:default_from => 'it.systems@example.com',
:default_notify => ["it.manager@example.com"],
:default_signature => "
---------------------------
This is an automatic email.
If you have any questions please contact customer service
at 1 (800) 888-0000 or go to http://www.example.com.
Thank you for your business!"
}
end
那么这是一个好的方法吗?或者有比这两种方法更好的方法吗?
最佳答案
我认为您在 default_from 和 default_notify 方面走在正确的 rails 上。 我不会为此使用 SMTP 设置;这些不是 SMTP 设置,它们只是一般的邮件程序设置。
我会在初始化程序中使用类似这样的东西:
MAILER_SETTINGS = YAML::load(open(File.join(Rails.root, "config", "mailer.yml")).read)[Rails.env]
使用如下所示的 yaml 文件:
development: &development
default_from: foo@bar.com
default_notify: ["foo@bar.com"]
production:
<<: *development
default_from: production@bar.com
这让您可以设置默认值,然后将它们级联并根据需要覆盖每个环境。
但是,对于签名,我只是将其移动到一个部分,然后您将其包含在您的邮件模板中。它们是与其他任何 View 一样的 View ,并且可以具有布局、部分等。
关于ruby-on-rails - 在 Rails 中设置自定义全局电子邮件默认值的 "best practices"方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4262091/