【问题标题】:What is the "best practices" approach to setting up custom global email defaults in Rails?在 Rails 中设置自定义全局电子邮件默认值的“最佳实践”方法是什么?
【发布时间】:2011-05-14 19:24:32
【问题描述】:

我正在从头开始一个新的 Rails 3 应用程序。

当我进行基本设置(配置 gems、会话等)时,我遇到了一些困扰我一段时间的事情。

我们当前的系统(Ruby 脚本和 Rails 2 应用程序的混合体)向客户发送各种电子邮件/传真通知。有些事情在 80% 的情况下很常见 - 抄送 - 我们端的某些电子邮件帐户和电子邮件签名。

之前我只是在environment.rb中定义了GLOBALS比如

SYSTEM_EMAIL_SIGNATURE

SYSTEM_EMAIL_NOTIFY

然后在邮件程序中使用它们,或者如果它是一个独立的脚本,我有一个 setup.rb 文件 - 它有一堆常见的设置 - 包括一个像这样的自定义电子邮件设置。

由于我正在从头开始重建这个应用程序并将所有脚本整合到一个 ruby​​ 应用程序中 - 我试图想出一种更好的方法来做到这一点。

现在我正在设置具有 action_mailer 设置的 email.rb 初始化程序,我通过添加更多项目来扩展它:

      ########## 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  

那么这是一个好方法吗?或者有比这两种方法更好的方法吗?

【问题讨论】:

    标签: ruby-on-rails ruby environment actionmailer


    【解决方案1】:

    我认为您在 default_from 和 default_notify 方面处于正确的轨道上。 我不会为此使用 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
    

    这使您可以设置默认值,然后将它们向下级联并根据需要覆盖每个环境。

    但是,对于签名,我只是将其移动到部分中,然后您将其包含在您的邮件模板中。它们是与其他任何视图一样的视图,并且可以具有布局、部分视图以及所有这些。

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2011-10-18
      • 2017-06-20
      • 1970-01-01
      • 2021-10-10
      • 2011-05-14
      相关资源
      最近更新 更多