Skip to content

Instantly share code, notes, and snippets.

@joe11051105
Last active December 28, 2017 11:16
Show Gist options
  • Save joe11051105/eafebe41f38bc3664599dd341af135e8 to your computer and use it in GitHub Desktop.
Save joe11051105/eafebe41f38bc3664599dd341af135e8 to your computer and use it in GitHub Desktop.
ActionMailer::Base.register_interceptor(BanDisposableInterceptor)
class BanDisposableInterceptor
def self.delivering_email(mail)
mail.perform_deliveries = false unless BanDisposableValidator.valid?(mail.to[0])
end
end
class BanDisposableValidator < ActiveModel::EachValidator
DISPOSABLE_DOMAINS =
JSON.parse(File.read("#{Rails.root}/config/disposable-email-domains.json")) rescue []
delegate :get_mail_domain, :valid?, to: self
def validate_each(record, attribute, value)
error_message = I18n.t 'devise.registrations.messages.invalid_email'
unless valid?(value)
record.errors[:email] << error_message
end
end
def self.valid?(mail)
domain_name = get_mail_domain(mail)
return false if domain_name.nil?
!DISPOSABLE_DOMAINS.include?(domain_name)
end
private
def self.get_mail_domain(mail)
Mail::Address.new(mail).domain rescue nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment