I’m sure you’ve seen the simple email address format validation function; they’re usually a simple regular expressing that just check the address portion (the firstname.lastname@example.org bit). That’s really only a bit of the validation that should be done. The RFC822 specs detail that the format of email addresses can be much larger, for example, it could be something like “Andrew Collington & Co.” <email@example.com>, and, of course, the simple regex on that would fail. But even a check on the address format isn’t often enough… The user could enter a correctly formatted email address but simply have mis-spelled the address… they may accidentally type in firstname.lastname@example.org, or email@example.com rather than hotmail.co.uk, and things like that. In which case you may want to check the MX and/or A record to see if its a valid domain. And whilst you’re doing that, why not check to see if it’s a commonly used email host that maybe they’ve typed in wrong?
So here is a class that will allow you to do all that in one easy method call: