Monday, November 14, 2011

Email Address verification using Perl script

Checking correctness of one email address is easy and can be done manually, however, if you want to validate a bunch of email addresses then automated script plays a very handy role. 
I would like to thank my colleague and friend "Sai Sathyanarayam" for giving me this script. I think this might be useful for others therefore, I am posting it here. 

# email.pl file
#open "email.txt" file from current directory, 
# email.txt file contains email addresses separated by , (comma) and each address is on new line
open(FILE,"email.txt");
while($line = <FILE> ) {
 
   chomp($line);
    if($line =~ /,/) { $line = $`; }else { print $line." is invalid\n";}
    if ($line =~ /^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/)
   {
       print "$line is valid\n"; 
   }
   else {
     print "$line is invalid\n";
   }
}

Sample email.txt file is as follows:
xyz@abc.com,
pqr@mnr.ac.in,

To perform validation test run following command:
$ perl email.pl