Reputation: 445
I have an input field for email_id in my form.
When the user enters the email id and submits the form, i need to validate the domain of the email.
(Note: I need not validate the email address. I need to Validate only the Domain)
What is the best way to check whether the Email-Domain is Valid or Not in Ruby on Rails?
Upvotes: 5
Views: 4864
Reputation: 94
There is a gem valid_email2 which has custom validations. It checks both MX records and A records. It also allows you to simply add blacklisted or whitelisted addresses as config in ymls. You can customize validation and validate according to the requirements. Check it's README for more.
Upvotes: 2
Reputation: 1145
There is kamilc/email_verifier to verify the realness of an email address by pretending to send an actual email to the mail server. But this does verify the address and not only its domain.
If you want to check the mail server only, you need to open a tcp connection to the domain on port 25 as described here by @diciu.
EDIT: This is not a good idea, as stated by @Doon in the comments. You should better send an actual verification email to the user.
Upvotes: 1
Reputation: 63
You can check DNS
servers if MX
records for domain exists.
mx = Resolv::DNS.open { |dns| dns.getresources('domain.com', Resolv::DNS::Resource::IN::MX) }
ok = mx.size > 0
Upvotes: 2
Reputation: 51
require ‘resolv’ #”gem install resolv-ipv6favor“
email = “[email protected]” #test with valid email
split_email = email.split(“@”) #split string with “@”
domain = split_email[1].to_s #get domain name
if email =~ (/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
#checking domain name
Resolv::DNS.open do |dns|
@staus = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
else
errors_add(:email, ‘domain name can not be found.’)
end
Upvotes: 1