Rails 3:尝试使用模块扩展 Action Mailer

发布于 2024-12-19 02:41:55 字数 1044 浏览 0 评论 0原文

尝试重写旧的 alias_method_chain 以在外发电子邮件上添加过滤器,但它不起作用。我很确定我遗漏了一些东西/遗漏了一些东西,但我不知道是什么。

该文件位于 /lib/outgoing_mail_filter.rb 中,其中加载了 config/initializers/required.rb

这是在 Rails 2 下工作的旧代码:

class ActionMailer::Base
  def deliver_with_recipient_filter!(mail = @mail) 
    unless 'production' == Rails.env
      mail.to = mail.to.to_a.delete_if do |to| 
        !(to.ends_with?('some_domain.com'))
      end
    end
    unless mail.to.blank?
      deliver_without_recipient_filter!(mail)
    end
  end
  alias_method_chain 'deliver!'.to_sym, :recipient_filter
end

这是我当前重写它的尝试:

class ActionMailer::Base
  module RecipientFilter
    def deliver(mail = @mail) 
      super
      unless 'production' == Rails.env
        mail.to = mail.to.to_a.delete_if do |to| 
          !(to.ends_with?('some_domain.com'))
        end
      end
      unless mail.to.blank?
        deliver(mail)
      end    
    end
  end

  include RecipientFilter
end

当我运行测试时,它甚至看起来都没有被调用或任何东西。任何帮助表示赞赏

Trying to rewrite an old alias_method_chain to add a filter on outgoing emails, and it isn't working. I'm pretty sure I've leaving something out/missing something, but I don't know what.

This file is in /lib/outgoing_mail_filter.rb, which is loaded with config/initializers/required.rb

Here's the old code that worked under Rails 2:

class ActionMailer::Base
  def deliver_with_recipient_filter!(mail = @mail) 
    unless 'production' == Rails.env
      mail.to = mail.to.to_a.delete_if do |to| 
        !(to.ends_with?('some_domain.com'))
      end
    end
    unless mail.to.blank?
      deliver_without_recipient_filter!(mail)
    end
  end
  alias_method_chain 'deliver!'.to_sym, :recipient_filter
end

And here's my current attempt at re-writing it:

class ActionMailer::Base
  module RecipientFilter
    def deliver(mail = @mail) 
      super
      unless 'production' == Rails.env
        mail.to = mail.to.to_a.delete_if do |to| 
          !(to.ends_with?('some_domain.com'))
        end
      end
      unless mail.to.blank?
        deliver(mail)
      end    
    end
  end

  include RecipientFilter
end

When I run my tests, it doesn't even look like this is being called or anything. Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-12-26 02:41:55

我正在开发环境中使用 mail_safe 重写电子邮件,强烈推荐。如果它不符合您的要求,您可以研究一下以获取灵感,代码非常简单。

以下代码摘自 /lib/mail_safe/rails3_hook.rb< /a> 并且应该做你想做的事:

require 'mail'

module MailSafe
  class MailInterceptor
    def self.delivering_email(mail)
      # replace the following line with your code
      # and don't forget to return the mail object at the end
      MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
    end

    ::Mail.register_interceptor(self)
  end
end

替代版本,使用 ActionMailer::Base 注册,而不是 Mail (感谢 Kevin Whitaker 让我知道这是可能的):

module MailSafe
  class MailInterceptor
    def self.delivering_email(mail)
      # replace the following line with your code
      # and don't forget to return the mail object at the end
      MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
    end

    ::ActionMailer::Base.register_interceptor(self)
  end
end

I'm using mail_safe to rewrite emails in the development environment, highly recommended. You could look into it for inspiration if it doesn't fit your bill, the code is very simple.

The following code is extracted from /lib/mail_safe/rails3_hook.rb and should do what you want:

require 'mail'

module MailSafe
  class MailInterceptor
    def self.delivering_email(mail)
      # replace the following line with your code
      # and don't forget to return the mail object at the end
      MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
    end

    ::Mail.register_interceptor(self)
  end
end

Alternate version, registering with ActionMailer::Base instead of Mail (thanks to Kevin Whitaker for letting me know it's possible):

module MailSafe
  class MailInterceptor
    def self.delivering_email(mail)
      # replace the following line with your code
      # and don't forget to return the mail object at the end
      MailSafe::AddressReplacer.replace_external_addresses(mail) if mail
    end

    ::ActionMailer::Base.register_interceptor(self)
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文