SMTP Testing with Perl

Raven

SMTP Testing with Perl

After testing a few too many SMTP servers and email addresses using Telnet, I finally broke down and wrote a perl script to do it for me. The SMTP Telnet test is handy, but the Debug mode for Net::SMTP does the same thing, and with less typing.

This script requires Net::SMTP and Net::DNS. Just set the $from variable to your own email address, and give the recipient address as the first argument. Or hack it however you like.

my $from = "you\@yourdomain";

my $email = $ARGV[0];
my $domain;
if ($email =~ /^[^\@]+\@([^\@]+)$/) {
$domain = $1;
} else {
die "Can't parse $email\n";
}

use Net::DNS;
my @mx = mx($domain);
my $this_mx;
if (@mx < 1) {
die "Can't find MX for $domain\n";
} else {
foreach my $mx (@mx) {
print "MX: " . $mx->exchange . " " . $mx->preference . "\n";
}
$this_mx = $mx[0]->exchange;
}

use Net::SMTP;
my $smtp = Net::SMTP->new($this_mx, Debug=>1);
$smtp->mail($from);
$smtp->to($email);
$smtp->data();
$smtp->datasend("To: $email\n");
$smtp->datasend("\n");
$smtp->datasend("SMTP Test\n");
$smtp->dataend();
$smtp->quit;

Comments are closed on this post