5

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?

Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
Chetan Datta
  • 447
  • 9
  • 19
  • 2
    create a custom validator method which will check for correct domain. create a array of acceptable domains and with the entered email_id split it like .split("@") then check it with array by .include? method. – Navin Sep 27 '16 at 10:13
  • Is there any widely used gem or something? – Chetan Datta Sep 27 '16 at 10:14
  • What exactly do you mean by _Validate_? Check if the mail server exists? Or check if the given text has a valid format for a domain (e.g. `/([a-z0-9]+\.)+[a-z]{2,}/i`)? – Max N. Sep 27 '16 at 10:41
  • 1
    its a simplest functionality to add by yourself. don't bulk your application by adding any gem to it. – Navin Sep 27 '16 at 10:43
  • @razr , My requirement is - Check if the mail server exists? – Chetan Datta Sep 27 '16 at 10:50

4 Answers4

2

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
TheR
  • 63
  • 1
  • 6
  • I would add something like [this list of trash mail adresses](https://gist.githubusercontent.com/adamloving/4401361/raw/db901ef28d20af8aa91bf5082f5197d27926dea4/temporary-email-address-domains) to be blocked. – Max N. Sep 27 '16 at 10:38
  • this isn't 100% correct, If and only If no MX record exists, you are to perform an A or AAAA lookup of the RHS. (per rfc 5321 -- https://tools.ietf.org/html/rfc5321 ) – Doon Sep 27 '16 at 11:20
2

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.

vinay mittal
  • 94
  • 1
  • 2
1

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.

Max N.
  • 993
  • 1
  • 12
  • 31
  • 1
    be careful about about opening up connections to random email servers, and pretending to send an email, Especially from a web host. This is a great way to get the webhost blocked. Also it isn't a great way to verify addresses, since with spammers now a days lots of web hosts are BOS (Block on Sight) so the mail servers will 5XX the address due to blocklist and your verification will fail. – Doon Sep 27 '16 at 11:29
  • Ok, I didn't know that. So what would be a sound implementation for such a check? Because checking if the domain exists is neither a good way to verify an email address. – Max N. Sep 28 '16 at 11:45
  • 1
    alas there really is no good Way (wearing my postmaster@ hat). If you require a valid email address, send an email to it with information about why it was generated,who requested it, etc... and ask the user to confirm their address. You should do this regardless before adding the email address to any sort of mail list, system that will email it, and confirm they want to be added. Since even if you take malice out of the equation there are lots of people that either don't know their email address or cannot type. – Doon Sep 28 '16 at 11:56
  • Oh yeah, sure. I always do that in my apps, but I didn't come up with it here... Thanks :) – Max N. Sep 28 '16 at 12:04
  • to further add. Might look at something like VERP or other bounce automated bounce processing so if/when the verification email bounces you can use the information in the return path to do something with the account (Temp hold/ mark as invalid, require immediate verification, etc..) – Doon Sep 28 '16 at 12:07
1
require ‘resolv’ #”gem install resolv-ipv6favor“

email = “foo@example.com” #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
Karthik
  • 51
  • 7
  • 1
    as i stated above, this isn't 100%. per RFC 5321, if no mx records exist you are to use the A/AAAA of the host name as implicit MX with pref 0. – Doon Sep 27 '16 at 11:23