Search code examples
ruby-on-railsfilterblockactionmailer

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


for non-production rails 2.x environments i want to block/filter any outgoing emails that aren't addressed to people in my organization (e.g. "*@where-i-work.com").

please note, i don't want to block email entirely - i know i can just write them to the logs in test mode - i need emails to internal employees to be delivered.

thanks.


Solution

  • You could try extending the Mail::Message.deliver function in your environment.rb file - something like (not tested - just 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
    

    Note that this id for Rails 3 - I think all versions of Rails 2 use TMail instead of Mail, so you'll need to override something else if you're not using Rails 3.

    Hope this helps!