#!/usr/bin/perl -W # # verifyemail.pl - A script which verifies if the given e-mail address exists. # # Copyright (C) 2010 Bogdan 'bogdro' Drozdowski, http://rudy.mif.pg.gda.pl/~bogdro/soft/ # (bogdandr AT op.pl, bogdro AT rudy.mif.pg.gda.pl) # # Requires: # Net::DNS, Getopt::Long, Net::SMTP # # Licence: # GNU General Public Licence v3+ # # Last modified : 2010-07-23 # # Syntax: # ./verifyemail.pl login@domain [login2@domain2 ...] # # Exit code 0 if all the given addresses seem to be valid. # Exit code 1 if some of the given addresses seem to be valid. # Exit code 2 if none of the given addresses seems to be valid. # Exit code 3 if help or license message printed and nothing checked. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foudation: # Free Software Foundation # 51 Franklin Street, Fifth Floor # Boston, MA 02110-1301 # USA use strict; use warnings; use Net::DNS; use Getopt::Long; use Net::SMTP; my $my_domain = 'mail.sillydomain.com'; my $my_addr = 'blah@sillydomain.com'; my $help_msg = "$0: E-mail address veifier.\nAuthor: Bogdan Drozdowski, ". "http://rudy.mif.pg.gda.pl/~bogdro/soft/\n". "Syntax: $0 [options] login\@domain [login2\@domain2 ...]\n". "Options:\n". "-h, --help, -?\t\t\tPrint help\n". "--license, --licence, -L\tDisplay license information\n". "--domain \t\t\tUse the given domain to present to the mail server\n". "\t\t\t\t(default: $my_domain)\n". "--sender
\t\tUse the given sender address to present to the mail server\n". "\t\t\t\t(default: $my_addr)\n". "-q, --quiet\t\t\tDon't print VALID/INVALID/UNKNOWN messages\n" ; my $lic_msg = "$0: E-mail address veifier.\nAuthor: Bogdan Drozdowski, ". "http://rudy.mif.pg.gda.pl/~bogdro/soft/\n\n". "This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foudation: Free Software Foundation 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA\n"; if ( @ARGV == 0 ) { print $help_msg; exit 3; } my $lic = 0; my $help = 0; my $quiet = 0; Getopt::Long::Configure ("ignore_case", "ignore_case_always"); if ( ! GetOptions ( 'h|help|?' => \$help, 'license|licence|L' => \$lic, 'domain=s' => \$my_domain, 'sender=s' => \$my_addr, 'q|quiet' => \$quiet, ) ) { print $help_msg; exit 3; } if ( $lic ) { print $lic_msg; exit 3; } if ( @ARGV == 0 || $help ) { print $help_msg; exit 3; } sub print_valid ($) { my $addr = shift; print "$addr: VALID\n" if ! $quiet; } sub print_fail ($) { my $addr = shift; print "$addr: UNKNOWN\n" if ! $quiet; } sub print_invalid ($) { my $addr = shift; print "$addr: INVALID\n" if ! $quiet; } my %invalid; ADDR: foreach my $addr (@ARGV) { $invalid{$addr} = 1; if ( $addr !~ /\w+@\w+/ ) { print_invalid ($addr); $invalid{$addr} = 1; next ADDR; } my @addr_parts = split /@/, $addr; my @mail_servers = mx ($addr_parts[@addr_parts-1]); if (@mail_servers) { foreach my $server (@mail_servers) { #print $server->preference, " ", $server->exchange, "\n"; my $smtps = Net::SMTP->new ($server->exchange, #Debug => 1, Hello => $my_domain); if ( $smtps ) { $smtps->mail ($my_addr); if ( $smtps->recipient ($addr) ) { print_valid ($addr); $invalid{$addr} = 0; $smtps->quit (); next ADDR; } else { print_invalid ($addr); $invalid{$addr} = 1; } $smtps->quit (); } else { print_fail ($addr); $invalid{$addr} = 1; } } } else { # DNS failed to provide Mail eXchangers. Try the domain itself my $smtps = Net::SMTP->new ($addr_parts[@addr_parts-1], #Debug => 1, Hello => $my_domain); if ( $smtps ) { $smtps->mail ($my_addr); if ( $smtps->recipient ($addr) ) { print_valid ($addr); $invalid{$addr} = 0; $smtps->quit (); next ADDR; } else { print_invalid ($addr); $invalid{$addr} = 1; } $smtps->quit (); } else { print_fail ($addr); $invalid{$addr} = 1; } } } my $n_inv = 0; foreach my $addr (@ARGV) { $n_inv++ if defined ($invalid{$addr}) && $invalid{$addr} == 1; } exit 0 if $n_inv == 0; exit 1 if $n_inv != 0 && $n_inv != @ARGV; exit 2;