2011-03-10 30 views
3

适用于非生产导轨2.x环境我想阻止/过滤任何传出的电子邮件,这些电子邮件并非寄给我组织中的人员(例如“*@where-i-work.com”)。如何过滤/阻止传出电子邮件地址与轨道2.x actionmailer?

请注意,我不想完全阻止电子邮件 - 我知道我可以在测试模式下将它们写入日志 - 我需要通过电子邮件发送给内部员工。

谢谢。

+0

有一个宝石本! http://stackoverflow.com/questions/4393213/rails-redirect-all-outgoing-emails-to-single-address-for-testing https://github.com/pboling/sanitize_email – 2012-10-21 01:58:15

回答

3

你可以尝试在你的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 

请注意,此ID为Rails 3 - 我觉得所有版本Rails 2使用TMail代替Mail,所以如果你不使用Rails 3,你需要重写其他的东西。

希望这有助于!

+0

我使用Rails 2,但感谢领先。 – 2011-03-11 01:00:53

2

基于@ Xavier的轨道3建议我能得到它的工作在轨道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 
+0

如果你正在使用aws-ses,你需要重写'AWS :: SES :: Base'而不是'ActionMailer :: Base'。 – blindgaenger 2011-03-30 16:36:09

相关问题