Rick,

        Thank's for the script!  I appreciate your help.

Marc



On Jun 27, 2011, at 5:24 PM, Rick Sanders wrote:

> Hi,
> 
> I posted a reply to your question but it hasn't shown up yet so maybe there's 
> a problem with my News server.  Anyway, here is a script that should let you 
> do verify e-mail addresses.
> 
> If my reply doesn't make it to the the newsgroup you can post this if you 
> like.
> 
> -Rick
> 
> -------- Original Message --------
> 
> Keep in mind all of the warnings mentioned in this thread.  This is a
> simple script that tries to validate a mail address.
> 
> It queries DNS for the MX record for the user's domain followed by an
> SMTP connection to one of the domain's MX servers.  After sending "HELO"
> and "MAIL FROM" commands it issues a "RCPT TO" command, and then sends
> "QUIT" (eg, it doesn't actually send a message).
> 
> Typical responses:
> 
> 250 2.1.5 OK z9si15926030ibd.51
> 250 <rfs9...@earthlink.net>... Recipient Ok
> 550 g...@earthlink.net...user unknown
> 550-5.1.1 The email account that you tried to reach does not exist
> 
> Warning:  This is just a demo script.  It has no error checking, retry
> capability, or the ability to try multiple MX servers. It doesn't even
> try to check whether the address is constructed correctly.
> 
> -Rick
> 
> #!/usr/bin/perl
> 
> use strict;
> use Net::DNS;
> use Socket;
> my @mx_hosts;
> my $recipient;
> 
>   #  Determine if an e-mail address is valid.
>   # Look up in DNS the MX server that handles mail for the
>   # recipient's domain and ask it if the address is valid.
> 
>   if ( @ARGV[0] ) {
>      $recipient = $ARGV[0];
>   } else {
>      print "Usage:  ./verify_mail_address.pl <mail address>\n\n";
>      exit;
>   }
> 
>   my ($userid,$domain) = split( /\@/, $recipient );
> 
>   print "\nChecking $recipient\n\n";
> 
>   my $res = Net::DNS::Resolver->new;
>   my @mx = mx( $res, $domain );
>   foreach my $rr ( @mx ) {
>      my $pref = $rr->preference;
>      my $mx_host = $rr->exchange;
>      push( @mx_hosts, $mx_host );
>   }
> 
>   #  If any of the mx hosts don't answer you can try another one.
>   #  You may want to sort by the rr->preference value but for this
>   #  example we'll just grab the first one from the list
> 
>   my $mx_host = pop( @mx_hosts );
> 
>   my $status = check_recipient( $recipient, $mx_host );
> 
>   print "$status\n";
>   exit;
> 
> 
> 
> sub check_recipient {
> 
> my $recipient   = shift;
> my $smtp_server = shift;
> my $my_domain   = 'mydomain.com';
> my $my_address  = 'm...@mydomain.com';
> my $recip_status;
> 
>   #  Open a connection to an MX host on the SMTP port and
>   #  ask if a recipient is a valid user in its domain.
> 
>   my $proto = getprotobyname('tcp');
>   socket(SERVER, AF_INET, SOCK_STREAM, $proto);
>   my $iaddr = gethostbyname($smtp_server);
>   my $port = getservbyname('smtp', 'tcp');
> 
>   my $sin = sockaddr_in($port, $iaddr);
> 
>   my $sreply;
> 
>   connect(SERVER, $sin);
>   recv SERVER, $sreply, 512, 0;
> 
>   send SERVER, "HELO $my_domain\r\n", 0;
>   recv SERVER, $sreply, 512, 0;
> 
>   send SERVER, "MAIL From:<$my_address>\r\n", 0;
>   recv SERVER, $sreply, 512, 0;
> 
>   send SERVER, "RCPT To:<$recipient>\r\n", 0;
>   recv SERVER, $recip_status, 512, 0;
> 
>   send SERVER, "QUIT\r\n", 0;
>   recv SERVER, $sreply, 512, 0;
> 
>   close SERVER;
> 
>   return $recip_status;
> 
> }


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to