Validate email addresses

You probably never tried to validate email addresses, I did, and it doesn’t seem to be hard, but it is (sorry if you also did 😉 )!
I was searching for a regexp, and if you search one you will find tone of them, but you don’t know which one to choose. Some are very long, some are too restictive (doesn’t handle new TLDs or handle only TLDs with three letters), some are RFC-822 compliant but they seems complicated, and I wonder if all addresses are RFC 822 compliant (It’s pretty often that RFC are not respected) … Also, it’s not because the format of the email address is valid, that that mail exists. The best and easy solution I came with, which is not the full solution, is to use the Email::Valid perl module like this:

#!/bin/perl

use Email::Valid ;

open(MAILS, "my-file-with-one-address-per-line");

while ($iemail = ) {
$oemail = Email::Valid->address(-address => $iemail, -mxcheck => 1) ;
if ($oemail) {
print "$iemail \n" ;
}
}

This code will print on the terminal all addresses from the input file that are valid. It will silently pass on invalid addresses. If you only want to get invalid emails, change the if ($oemail) { ligne with: if (!$oemail) {. As you maybe have seen, there is a -mxcheck in the code: This does searches for a MX (Mail Exchanger) for the domain part of the given mail address, if there's no MX, the mail is considred invalid. So what I find with this perl module is that it have a good regexp for matching email addresses, and that it check for a MX. That's it !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s