This program uses Net::SMTP to talk to an SMTP server and uses the EXPN and VRFY commands to figure out whether an address is going to work. It isn't perfect, because it relies on the remote SMTP giving meaningful information with the EXPN and VRFY commands—they are common ways for spammers to harvest email addresses, and so many servers have disabled these options. It uses Net::DNS if available, but can also work without it.
This program inspects $0 (the program name) to see how it was called. If run as expn, it uses the EXPN command; if called as vrfy, it uses the VRFY command. Use links to install it with two names (on a system without links, simply copy the program code in Example 18-5):
% cat > expn #!/usr/bin/perl -w ... ^D % ln expn vrfy
When given an email address as an argument, the program reports what the mail server says when you try to EXPN or VRFY the address. If you have Net::DNS installed, it tries all hosts listed as mail exchangers in the DNS entry for the address.
Here's what it looks like without Net::DNS:
% expn [email protected] Expanding gnat at frii.com ([email protected]): calisto.frii.com Hello coprolith.frii.com [207.46.130.14], pleased to meet you [email protected]
And here's the same address with Net::DNS installed:
% expn [email protected] Expanding gnat at mail.frii.net ([email protected]): deimos.frii.com Hello coprolith.frii.com [207.46.130.14], pleased to meet you Nathan Torkington <[email protected]> Expanding gnat at mx1.frii.net ([email protected]): phobos.frii.com Hello coprolith.frii.com [207.46.130.14], pleased to meet you [email protected] Expanding gnat at mx2.frii.net ([email protected]): europa.frii.com Hello coprolith.frii.com [207.46.130.14], pleased to meet you [email protected] Expanding gnat at mx3.frii.net ([email protected]): ns2.winterlan.com Hello coprolith.frii.com [207.46.130.14], pleased to meet you 550 gnat... User unknown
The program is shown in Example 18-5.
#!/usr/bin/perl -w # expn -- convince smtp to divulge an alias expansion use strict; use Net::SMTP; use Sys::Hostname; my $fetch_mx = 0; # try loading the module, but don't blow up if missing eval { require Net::DNS; Net::DNS->import('mx'); $fetch_mx = 1; }; my $selfname = hostname( ); die "usage: $0 address\@host ...\n" unless @ARGV; # Find out whether called as "vrfy" or "expn". my $VERB = ($0 =~ /ve?ri?fy$/i) ? 'VRFY' : 'EXPN'; my $multi = @ARGV > 1; my $remote; # Iterate over addresses give on command line. foreach my $combo (@ARGV) { my ($name, $host) = split(/\@/, $combo); my @hosts; $host ||= 'localhost'; @hosts = map { $_->exchange } mx($host) if $fetch_mx; @hosts = ($host) unless @hosts; foreach my $host (@hosts) { print $VERB eq 'VRFY' ? "Verify" : "Expand", "ing $name at $host ($combo):"; $remote = Net::SMTP->new($host, Hello => $selfname); unless ($remote) { warn "cannot connect to $host\n"; next; } print "\n"; if ($VERB eq 'VRFY') { $remote->verify($name); } elsif ($VERB eq 'EXPN') { $remote->expand($name); } last if $remote->code = = 221; next if $remote->code = = 220; print $remote->message; $remote->quit; print "\n" if $multi; } }
Copyright © 2003 O'Reilly & Associates. All rights reserved.