Created: 2008-02-29 04:37
Updated: 2019-02-22 12:08
License: mit


Email Veracity was my very first open source project. Six years ago, it seemed like a cool idea, but it's now pretty common knowledge that trying to validate email addresses beyond a loose format check will likely yield false-positives. The whole idea of including a library for that seems silly.

Email Veracity 101

Let's explore the different functionality that Email Veracity provides and some simple ways you can integrate this functionality into your code without needing to rely on a flaky guy like me!

Validating an address for format

Trying to explicitly validate an email address for total RFC compliance is extravagantly complex, so why bother? Send a verification email to ensure that the user has provided a functioning address. If you still think that having a more robust check is necessary, the regex used in Email Veracity is from here and is a pretty good compromise between obscene complexity and useful robustness.

email = ''

if email =~ /\A[^@]+@[^@]+\Z/
  puts 'probably valid'
  puts 'definately not valid'

Extracting the domain portion of an email address

There are libraries out there for formally parsing email addresses and you can use those to get the domain portion of the email address, but we can also just use a little regular expression.

email = ''
match = /@([\w\.\-]+)\Z/i =~ email

if match
  puts 'domain is: ' + match[1]
  puts 'no domain found'

Looking up records on the domain

I highly discourage using this method to actually validate email addresses in-line with a request; it will absolutely yield false-positives.

require 'resolv'

A  = Resolv::DNS::Resource::IN::A  # Address records
MX = Resolv::DNS::Resource::IN::MX # Mail-exchange records
NS = Resolv::DNS::Resource::IN::NS # Name server records

def lookup(host, type), type)

a_records  = lookup('', A)
mx_records = lookup('', MX)
ns_records = lookup('', NS)

puts { |r| r.address.to_s }
puts { |r| }
puts { |r| }

Check out the Ruby Standard Library Documentation for more ways to use Resolv::DNS in your project.
