简体   繁体   English

我可以根据环境为ActionMailer电子邮件指定不同的收件人吗?

[英]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

Also, there are several plugins that do this. 此外,有几个插件可以做到这一点。 The best one that I've found of the three I looked at was mail_safe . 我发现的三个中最好的一个是mail_safe

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用相同的ActionMailer从不同的地址发送电子邮件吗? - Can I send email from different addresses using same ActionMailer 根据环境覆盖到ActionMailer中的字段 - Override to field in ActionMailer based on environment Rails ActionMailer - 格式化发件人和收件人姓名/电子邮件地址 - Rails ActionMailer - format sender and recipient name/email address 如何检测到收件人已将电子邮件拒绝为垃圾邮件? - How can I detect that an email has been rejected as spam by the recipient? 我可以选择使用Ruby On Rails中的ActionMailer在电子邮件中显示附件的位置吗? - Can I choose where an attachment appears in an email using ActionMailer in Ruby On Rails? 如何将actionmailer方法的收件人列表发送到其他电子邮件? - How to send the list of receivers of a actionmailer method to a different email? 使用Devise配置ActionMailer以允许电子邮件来自其他用户。 - Configure ActionMailer with Devise to allow email to come from different user. 为什么我必须使用ActionMailer指定文本格式来呈现模板? - Why do I have to specify text format to render template with ActionMailer? 使用与ActionMailer不同的delivery_method只用于一封电子邮件? - Use a different delivery_method with ActionMailer just for a single email? 如何更改gem如何使用ActionMailer并替换其他邮件程序类? - How can I change out how a gem uses ActionMailer and substitute a different mailer class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM