Skip to content

Instantly share code, notes, and snippets.

@dtaniwaki
Last active August 29, 2015 13:57
Show Gist options
  • Save dtaniwaki/9827058 to your computer and use it in GitHub Desktop.
Save dtaniwaki/9827058 to your computer and use it in GitHub Desktop.
if Settings.mailer && Settings.mailer.interceptors
class SandboxEmailInterceptor
def self.delivering_email(message)
whitelist = [Settings.mailer.whitelist].flatten.compact
blacklist = [Settings.mailer.blacklist].flatten.compact
allowed = [message.to].flatten.all?{|t| whitelist.any?{|r| r.is_a?(Regexp) ? r =~ t : r == t } }
prohibited = [message.to].flatten.all?{|t| blacklist.any?{|r| r.is_a?(Regexp) ? r =~ t : r == t } }
if prohibited || !allowed
Rails.logger.debug "Intercept email for #{message.to.inspect}"
message.to = [Settings.mailer.interceptors].flatten
end
end
end
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor)
end
unless Rails.env.production?
class EnvSubjectInterceptor
def self.delivering_email(message)
message.subject = "(#{Rails.env}) #{message.subject}"
end
end
ActionMailer::Base.register_interceptor(EnvSubjectInterceptor)
end
@dtaniwaki
Copy link
Author

"Settings" looks like this.

mailer:
  whitelist:
    - !ruby/regexp '/.*@hotmail.com$/'
    - '[email protected]'
  interceptors:
    - '[email protected]'

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