#!/usr/local/bin/perl -w use strict; $| = 1; use Getopt::Long; use Net::SMTP; my $server = 'localhost'; my $port = 25; my $smtpauth = 0; my $user = "$ENV{USER}\@localhost"; my $passwd = ''; my $debug = 0; my $sendtest = 0; my $testmsg = undef; my $msgto = 'postmaster'; GetOptions ('host|h=s' => \$server, 'port=s' => \$port, 'smtpauth|a' => \$smtpauth, 'user|u=s' => \$user, 'password|p=s' => \$passwd, 'debug|d' => \$debug, 'sendtest|t' => \$sendtest, 'testmsg|m=s' => \$testmsg, 'to=s' => \$msgto ); print "\nAttempt to Connect to Server $server on Port $port:"; my $smtp = Net::SMTP->new($server, Port => $port, Debug => $debug? 1 : 0); # See if Open occured or Not my $chk = 1; $chk = 0 unless defined $smtp; if ( !$chk ) { print "Failed...\n"; exit; } else { print "Connected!\n"; } if ($smtpauth) { print "Attempting to login: "; if ($smtp->auth($user, $passwd)) { print "succeeded\n"; } else { print "failed\n"; } } if ($sendtest) { print "Attempting to send message: "; my $msg =<<"MSG"; To: $msgto From: $user Subject: This is a test This is a test from $user on $server. MSG if ($testmsg and -r $testmsg) { if (open (MSG, $testmsg)) { local $/; $msg = ; close MSG; } else { warn "Can't open test message at '$testmsg' using default.\nError: $!\n"; } } if ($user =~ /@/) { $smtp->mail($user); } else { $smtp->mail("$user\@$server"); } if ($smtp->to($msgto)) { if ($smtp->data($msg)) { print "success\n"; } else { print "failure on send\n"; } } else { print "failure on rcpt\n"; } } $smtp->quit; __END__ =head1 NAME test-smtp-auth - Script to test SMTP authentication. =head1 SYNOPSIS test-smtp-auth.pl [-d] [--host] [--port] [--smtpauth [--user] [--password]] [--sendtest [--to] [--testmsg]] =head1 DESCRIPTION This is a quick script to test SMTP auth and mail relay through an SMTP server. NB: This is not a complete open relay test but can be used for that. =head1 Parameters - -d turns on Debugging mode. --host smtphost -> allows a destination host to be provided. --port smtpport -> allows a destination port to be provided. --smtpauth -> turns on SMTP authentication. --user user -> required for --smtpauth --password pass -> required for --smtpauth --sendtest -> actually forces a message to send. --to user -> destination user for --sendtest --testmsg file -> alternate test message file. =head1 SEE ALSO Net::SMTP =head1 LICENSE Copyright (c) 2004 Randy Smith All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =head1 AUTHOR Randy Smith =cut