A quick discussion of how to detect invalid email addresses, in several ways, and how to combine them into a system for reducing invalid addresses from entering your system and negatively impacting our sender reputation.
Detecting invalid addresses
Lexical structure (Looks like a duck)
The first thing we want to verify is that this is something that could possibly be a valid email address. At least from a lexical point of view. The proper tool for this is regular expressions, which thanks to modern technology are actually readable now.
This validates the lexical structure of the address, which is the first step in validing it.
Domain validation (Quacks like a duck)
Now that we’ve validated the lexical structure, we can move on some more indepth testing of the address. First, we’ll want to validate that the domain in the address actually points to a real domain address, and that email is enabled for that domain.
On a technical basis, we do this by checking for the existence of an MX record for the given domain name.
Note that you can extend this to perform further checks, such as validating the existence of the MX domain, checking SPF records (if you only want to communicate with two-way mailboxes), etc.
Checking if the mailbox exists (Swims like a duck)
I don’t recommend this. In order to get it to work, you’ll have to set up your server as a sending IP for your domain. Beyond the initial setup, there’s a good chance that if you abuse it, your servers will be banned for spamming smtp connections.
All together now (Is it a duck?)
By combining the methods above, you can build a validation function that fits your needs and acceptable level of risk. On a personal note, the majority of “bad” addresses I see come from end users testing out our site. They will either enter gibberish or a fake email (often not caring if it’s even a valid domain) just to see how the site behaves. In this case, you will need to communicate why the email failed validation.
Here’s a gist with the above code put together into a valid_email?
method