简体   繁体   English

在ActionMailer中禁用某些电子邮件

[英]Disable certain emails in ActionMailer

I would like to turn off certain emails in development and on test/staging servers, but keep sending them in production. 我想在开发和测试/登台服务器中关闭某些电子邮件,但在生产中继续发送。 What I currently do is set the default "to" address for these administrative mails to blank: 我目前要做的是将这些管理邮件的默认“收件人”地址设置为空白:

  default :to => Rails.env.production? ? "admin-list@sharethevisit.com" : ""

However, this puts stack traces in my development log when I'm testing the user mails which should be sending. 但是,当我测试应该发送的用户邮件时,这会将堆栈跟踪记录放在开发日志中。

Is there a more effective way of disabling certain emails based on the current environment? 有没有一种更有效的方法可以根据当前环境禁用某些电子邮件? I tried checking in the functions themselves, but is not ideal because I have to change each function, plus it doesn't actually work... it just fails to set the needed @account variable before rendering the email and creating a different stack trace. 我尝试检查函数本身,但并不理想,因为我必须更改每个函数,而且它实际上不起作用...它只是无法在渲染电子邮件和创建其他堆栈跟踪之前设置所需的@account变量。

  def user_registered_notify_email(account)
    if Rails.env.production?
      @account = account
      mail(:subject => "New user registered: #{@account.full_name}")
    end
  end

I can't remember where I found this to credit the author but this is how I redirected email in development mode. 我不记得在哪里可以找到作者的名字,但这就是我在开发模式下重定向电子邮件的方式。 Create a new file in RAILS_ROOT/config/initializers for this. 为此,在RAILS_ROOT / config / initializers中创建一个新文件。 DEFAULT_DEV_EMAIL_OVERRIDE is defined in our main site config with other static values. DEFAULT_DEV_EMAIL_OVERRIDE在我们的主站点配置中定义为其他静态值。

if Rails.env.development?
  if Rails.version =~ /^2\./
    class ActionMailer::Base
      def create_mail_with_overriding_recipients
        mail = create_mail_without_overriding_recipients
        mail.to = DEFAULT_DEV_EMAIL_OVERRIDE
        mail
      end
      alias_method_chain :create_mail, :overriding_recipients
    end
  elsif Rails.version =~ /^3\./
    if Rails.env.development?
      class OverrideMailRecipient
        def self.delivering_email(mail)
          mail.to = DEFAULT_DEV_EMAIL_OVERRIDE
        end
      end
      ActionMailer::Base.register_interceptor(OverrideMailRecipient)
    end
  end
end

我通常按​​照以下说明使用邮件拦截器: http : //asciicasts.com/episodes/206-action-mailer-in-rails-3

Consider using an interceptor to set Mail::Message#perform_deliveries to false : 考虑使用拦截器将Mail::Message#perform_deliveriesfalse

class NeverDeliverInterceptor
  def self.delivering_email(message)
    message.perform_deliveries = false
  end
end

if !Rails.env.production?
  ActionMailer::Base.register_interceptor(NeverDeliverInterceptor)
end

See API doc for source & other usage. 有关来源和其他用法, 请参见API文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM