簡體   English   中英

我可以根據環境為ActionMailer電子郵件指定不同的收件人嗎?

[英]Can I specify a different recipient for an ActionMailer email based on the environment?

我想知道是否可以配置從ActionMailer派生的Rails電子郵件,以根據環境發送給不同的收件人。 例如,對於開發我希望它發送郵件到我的個人電子郵件,所以我不會用“測試”電子郵件堵塞我們公司的電子郵件帳戶; 但是我希望它能使用真實的地址。

我怎樣才能做到這一點?

mail_safe插件可能有點過分了。 一個簡單的初始化程序就可以

Rails 2.x

if Rails.env == 'development'
  class ActionMailer::Base
    def create_mail_with_overriding_recipients
      mail = create_mail_without_overriding_recipients
      mail.to = "mail@example.com"
      mail
    end
    alias_method_chain :create_mail, :overriding_recipients
  end
end

Rails 3.x

if Rails.env == 'development'

  class OverrideMailReciptient
    def self.delivering_email(mail)
      mail.to = "mail@example.com"
    end
  end

  ActionMailer::Base.register_interceptor(OverrideMailReciptient)
end

默認情況下,開發環境未設置為實際發送電子郵件(它只記錄它們)。

設置備用帳戶可以通過多種不同方式完成。 您可以在郵件程序中使用某些邏輯,如此...

recipients (Rails.env.production? ? "email@company.com" : "test@non-company.org")

或者您可以將收件人定義為環境文件中的常量,如下所示:

/config/environment/production.rb

EMAIL_RECIPIENT = "email@company.com"

/config/environment/development.rb

EMAIL_RECIPIENT = "test@non-company.org"

然后在郵件程序中使用常量。 例:

recipients EMAIL_RECIPIENT

此外,有幾個插件可以做到這一點。 我發現的三個中最好的一個是mail_safe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM