how can you filter/block outgoing email addresses with rails 2.x actionmailer?
for non-production rails 2.x environments i开发者_StackOverflow社区 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.
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!
based on @Xavier's rails 3 proposal i was able to get it working in 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
精彩评论