Skip to content

Instantly share code, notes, and snippets.

@jmccartie
Created September 23, 2012 22:53
Show Gist options
  • Save jmccartie/3773340 to your computer and use it in GitHub Desktop.
Save jmccartie/3773340 to your computer and use it in GitHub Desktop.
Don't deliver email if the address is on a blacklist
class EmailAddressFilter
def self.delivering_email(message)
message.perform_deliveries = false
# Remove email from message.to if email is on the unsubscribe list
tos = message.to.to_a
message.to.each do |email|
tos.delete(email) if Unsubscribe.find_by_email(email)
end
message.to = tos # can't delete directly from message.to. probably a better way to do this...
return unless message.to.any?
# Still have emails? Let's send them!
message.perform_deliveries = true
end
end
ActionMailer::Base.register_interceptor(EmailAddressFilter)
@cgorshing
Copy link

How about

message.to = message.to.select { |email| !Unsubscribe.find_by_email(email) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment