or similar was not an option, which is a pity. Regardless, on with the pondering.$smtp->recipients(@addresses, { Notify => ['SUCCESS','FAILURE','DELAY' +] });
Within a few short lines of code, you already have something useful. You could at this point, for example, chuck in some code to simply print out the MX records that Net::DNS found, like:#!/usr/bin/perl # Use strictures use strict; use warnings; # Additional modules use Socket; use Net::DNS; use Net::SMTP; # At launch, ask for an email address print "Please enter an email address: "; my $address = <STDIN>; # Break apart the address and do MX lookup against the # domain our ($userpart, $domainpart) = split /@/, $address; our $res = Net::DNS::Resolver->new(); our @mx = mx($res, $domainpart) or die "Cannot resolve $domainpart: $!\n";
But this is not about resolving MX records. This is about talking to SMTP hosts!for my $rr (@mx) { print $rr->preference, " ", $rr->exchange,"\n"; }
This piece of code can work like an SMTP ping, where you could check to see if a host is up, or do as I have and use it for primitive sender verification, or to test the existence of mailboxes at a destination host. Or whatever. You know? ;-)# We assume that @mx contains something, since the die() # during MX resolution would have killed us by now for (@mx) { # It *would* be more elegant to test the primary server # in the event of round-robin MX or pick one in the # event of load-balanced MX, but I want to test for # any disparities in replies. my @mailservers = $rr->exchange; for my $mailserver (@mailservers) { # Connection plus EHLO my $smtp = Net::SMTP->new( Host => $mailserver, Hello => some.server.tld, Timeout => 30, Debug => 0 ) or die "Cannot connect to $mailserver!\n"; # MAIL FROM: $smtp->mail('me@somewhere.com'); # RCPT TO: my $response = $smtp->recipient($address); # Now go looking for response codes. On my system, # 1 = user valid, 0 = user invalid if ($response == 1) { # Do something, or return() something } else { # Do something, or return() something } # Be polite, RSET and QUIT properly $smtp->reset(); $smtp->quit(); # Slow down the rate between probes sleep(2); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pondering the elegant simplicity of Net::SMTP
by dwm042 (Priest) on Nov 30, 2007 at 16:40 UTC | |
by NoSignal (Acolyte) on Dec 03, 2007 at 07:19 UTC |