【问题标题】:how can you filter/block outgoing email addresses with rails 2.x actionmailer?如何使用 rails 2.x actionmailer 过滤/阻止外发电子邮件地址?
【发布时间】:2011-03-12 02:12:59
【问题描述】:

对于非生产 Rails 2.x 环境,我想阻止/过滤任何未发送给我组织中人员的外发电子邮件(例如“*@where-i-work.com”)。

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

谢谢。

【问题讨论】:

标签: ruby-on-rails filter block actionmailer


【解决方案1】:

您可以尝试在 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,则需要覆盖其他内容。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    基于@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,则需要覆盖 AWS::SES::Base 而不是 ActionMailer::Base
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2012-08-17
    • 2015-04-12
    相关资源
    最近更新 更多