Last active
December 28, 2017 11:16
-
-
Save joe11051105/eafebe41f38bc3664599dd341af135e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActionMailer::Base.register_interceptor(BanDisposableInterceptor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BanDisposableInterceptor | |
def self.delivering_email(mail) | |
mail.perform_deliveries = false unless BanDisposableValidator.valid?(mail.to[0]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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