email Validation script

#!/usr/bin/perl -w
use Net::SMTP;
#Create a new object with 'new'.
BEGIN {
use lib '/net/lib/perl';
require VVV::DNS;
require Net::SMTP;
}


@gl::myips = qw(
202.XX.XX.XX
202.XX.XX.XX
202.XX.XX.XX
202.XX.XX.XX
202.XX.XX.XX
);




my $res = DNS::dnshandle('202.XX.XX.XX');

# list of email iDs
my $file = $ARGV[0];
open (IN, "$file") || die "Require valid filename of emailids $!\n";
my @arrids = <IN>;
close IN;

chomp @arrids;

#function to check valid email

sub valid_email {
my($id1) = @_;
if ($id1 =~ /^[\w\-\_\.]+\@[\w\-\_\.]+$/) {
print "$id1 is valid";
return 1;
} else {
print "$id1 is invalid";
return 0;
}
}

#
# 0 ==>; email is valid
# 1 ==>; email invalid
# 2 ==>; email could not be verified try later
#


sub emailid_invalid {
my($id,$mxref,$smtpbind)=@_;
print "MX HOST for domain = ". $mxref->[0] ."\n";
$smtp = Net::SMTP->new(
Host => $mxref->[0],
Timeout => 30,
LocalAddr => $smtpbind,
Debug => 1,
Hello=>'company.com',
);
unless($smtp){
print "############Could not connect to ...#########\n";
return 2;
}

#Send the MAIL command to the server.
$smtp->mail("user1\@
company.com") || return(2);
print $smtp->to("$id");
my $errcode = $smtp->code();
if ( $errcode =~ /^25/ ) {
return 0;
print " this email ID is correct" ;
} elsif ($errcode =~ /^5/ ) {
return 1;
print " $errcode this email ID is incorrect" ;
} elsif ($errcode =~ /^4/ ) {
print " deffered connection ";
return 3;
}

$smtp->quit();
}



open(VALID,">", " /opt/valid");
open(INVALID,"/opt/invalid");
open(TRYAGAIN,"/opt/RETRY");

my @bindips;

foreach my $id (@arrids ) {
print "Checking ID: $id\n";
next unless (valid_email($id));
print "\nVALID Continue........\n";

my ($uname,$fulldomain) = split(/\@/, $id);
my @mxrec1 = DNS::mxrec($res,$fulldomain);

unless(scalar(@mxrec1)){
print INVALID "$id\n";
next;
}
unless(scalar @bindips){
push @bindips,@gl::myips;
}
my $smtpbind = shift(@bindips);

print STDERR "BIND as $smtpbind\n";

sleep 1;
my $ret = emailid_invalid($id,\@mxrec1,$smtpbind);
if($ret == 0){
print VALID "$id\n";
} elsif ($ret == 1) {
print INVALID "$id\n";
} elsif($ret == 2) {
print TRYAGAIN "$id\n";
} elsif($ret == 3) {
print TRYAGAIN "$id\n";
}

}


1 comment:

chorny said...

Email::Valid CPAN module gives more correct results.

Other Articles

Enter your email address: