[英]Can I specify a different recipient for an ActionMailer email based on the environment?
I'm wondering if it's possible to configure a Rails email derived from ActionMailer to send to a different recipient based on the environment. 我想知道是否可以配置从ActionMailer派生的Rails电子邮件,以根据环境发送给不同的收件人。 For example, for development I'd like it to send mail to my personal email so I don't clog up our company email account with "Testing" emails; 例如,对于开发我希望它发送邮件到我的个人电子邮件,所以我不会用“测试”电子邮件堵塞我们公司的电子邮件帐户; for production however I want it to use the real address. 但是我希望它能使用真实的地址。
How can I achieve this? 我怎样才能做到这一点?
The mail_safe plugin might be a little over kill. mail_safe插件可能有点过分了。 A simple initializer will do 一个简单的初始化程序就可以
Rails 2.x 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 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
By default the development environment isn't setup to actually send emails (it just logs them). 默认情况下,开发环境未设置为实际发送电子邮件(它只记录它们)。
Setting up alternate accounts can be done in many different ways. 设置备用帐户可以通过多种不同方式完成。 You can either use some logic in your mailer like so... 您可以在邮件程序中使用某些逻辑,如此...
recipients (Rails.env.production? ? "email@company.com" : "test@non-company.org")
Or you can define the recipient as a constant in the environment files like so: 或者您可以将收件人定义为环境文件中的常量,如下所示:
/config/environment/production.rb /config/environment/production.rb
EMAIL_RECIPIENT = "email@company.com"
/config/environment/development.rb /config/environment/development.rb
EMAIL_RECIPIENT = "test@non-company.org"
and then use the constant in your mailer. 然后在邮件程序中使用常量。 example: 例:
recipients EMAIL_RECIPIENT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.