Rails 3:尝试使用模块扩展 Action Mailer
尝试重写旧的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在开发环境中使用 mail_safe 重写电子邮件,强烈推荐。如果它不符合您的要求,您可以研究一下以获取灵感,代码非常简单。
以下代码摘自 /lib/mail_safe/rails3_hook.rb< /a> 并且应该做你想做的事:
替代版本,使用
ActionMailer::Base
注册,而不是Mail
(感谢 Kevin Whitaker 让我知道这是可能的):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:
Alternate version, registering with
ActionMailer::Base
instead ofMail
(thanks to Kevin Whitaker for letting me know it's possible):