【发布时间】:2011-03-12 02:12:59
【问题描述】:
对于非生产 Rails 2.x 环境,我想阻止/过滤任何未发送给我组织中人员的外发电子邮件(例如“*@where-i-work.com”)。
请注意,我不想完全阻止电子邮件 - 我知道我可以在测试模式下将它们写入日志 - 我需要将电子邮件发送给内部员工。
谢谢。
【问题讨论】:
标签: ruby-on-rails filter block actionmailer
对于非生产 Rails 2.x 环境,我想阻止/过滤任何未发送给我组织中人员的外发电子邮件(例如“*@where-i-work.com”)。
请注意,我不想完全阻止电子邮件 - 我知道我可以在测试模式下将它们写入日志 - 我需要将电子邮件发送给内部员工。
谢谢。
【问题讨论】:
标签: ruby-on-rails filter block actionmailer
您可以尝试在 environment.rb 文件中扩展 Mail::Message.deliver 函数 - 类似于(未测试 - 只是演示代码!):
class Mail::Message
def deliver_with_recipient_filter
self.to = self.to.to_a.delete_if {|to| !(to =~ /.*@where-i-work.com\Z/)} if RAILS_ENV != production
self.deliver_without_recipient_filter unless self.to.blank?
end
alias_method_chain :deliver, :recipient_filter
end
请注意,Rails 3 的这个 id - 我认为 Rails 2 的所有版本都使用 TMail 而不是 Mail,因此如果您不使用 Rails 3,则需要覆盖其他内容。
希望这会有所帮助!
【讨论】:
基于@Xavier 的 rails 3 提案,我能够让它在 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?('where-i-work.com')
end
end
unless mail.to.blank?
deliver_without_recipient_filter!(mail)
end
end
alias_method_chain 'deliver!'.to_sym, :recipient_filter
end
【讨论】:
AWS::SES::Base 而不是 ActionMailer::Base。