How can you filter / block outgoing email addresses with rails 2.x actionmailer?

for non-working rails 2.x. I want to block / filter any outgoing messages that are not addressed to people of my organization (for example, "*@where-i-work.com").

Please note: I do not want to completely block emails - I know that I can just write them to magazines in test mode - I need emails for internal employees to be delivered.

thank.

+3
source share
2 answers

You can try expanding the function Mail::Message.deliverin the environment.rb file - something like (not tested - just a demo code!):

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 - , Rails 2 TMail Mail, - , Rails 3.

, !

+3

@Xavier rails 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
+2

All Articles