I've created a perl script to validate email ids for my marketing team to send campaigns.
The script is behaving erratically.
For Example, I had validated 135 email ids on various dates,
******************************************
Date | Valid | Invalid | Total
******************************************
23-Dec-13 45 | 90 | 135
******************************************
24-Dec-13 90 | 45 | 135
******************************************
25-Dec-13 133 | 02 | 135
******************************************
I'm unable to figure out where it went wrong,
Code:
#!/usr/bin/perl
use Data::Dumper;
%lookup_cache = ();
sub valid_address {
my($addr) = @_;
my($domain, $valid);
# Lower-case address
$addr = lc($addr);
# Validate format of address
return(0) unless ($addr =~ /^[^@]+@([-\w]+\.)+[a-z]{2,4}$/);
# Grab domain
$domain = (split(/@/, $addr))[1];
# Lookup and return cached result if it exists
$cached_result = $lookup_cache{$domain};
if ($cached_result ne '')
{
#print "[cached_result] ";
return $cached_result;
}
# Do domain lookup
$valid = 0;
if (open(DNS, "nslookup -q=any $domain |"))
{
while (<DNS>) {
$valid = 1 if (/^$domain.*\s(mail exchanger|internet address)\s=/i);
}
}
# Store cached result for later
$lookup_cache{$domain} = $valid;
return $valid;
}
while (<>) {
$addy = $_;
$addy =~ s/\s+$//;
if ($addy)
{
print "$addy " . (valid_address($addy) ? 'valid' : 'invalid') . "\n";
}
}