Skip to content

Instantly share code, notes, and snippets.

@dawidlenkiewicz
Created August 19, 2014 15:43
Show Gist options
  • Save dawidlenkiewicz/c893c7812ef5aa5e9fef to your computer and use it in GitHub Desktop.
Save dawidlenkiewicz/c893c7812ef5aa5e9fef to your computer and use it in GitHub Desktop.
email_validator
class EmailValidator < ActiveModel::EachValidator
require 'resolv'
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || I18n.t('email.wrong'))
end
split_email = value.split("@")
domain = split_email[1].to_s
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
if @mx.blank?
record.errors[attribute] << (options[:message] || I18n.t('email.wrong_domain'))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment