Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created May 17, 2011 00:21
Show Gist options
  • Save waffle2k/975648 to your computer and use it in GitHub Desktop.
Save waffle2k/975648 to your computer and use it in GitHub Desktop.
Colourized SMTP tester script
#!/usr/bin/perl
use strict;
use IO::Socket;
use Term::ANSIColor;
sub get_file( $ ){
my ($filename) = @_;
open FD, "<$filename" || die( "Cannot open $filename: $!\n" );
my $text = do { local $/; <FD>; };
close FD;
return $text;
}
my $host = shift || 'mail.tucows.com';
my $port = shift || 25;
my $rcpt = shift || die("Please specify a recipient\n");
unless( $rcpt =~ /\S+?\@\S/ ){
die( "Not a valid email address\n");
}
print color 'bold blue';
print "Connecting to $host:$port\n";
print color 'reset';
my $sock=IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
unless (defined $sock) {
exit -1;
}
#print $sock "GET / HTTP/1.0\n\n";
my $banner = <$sock>;
$banner =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$banner]\n";
unless( $banner =~ /^220/ ){
print color 'bold red';
print "Error from server: [" . $banner . "]\n";
print color 'reset';
exit( 1 );
}
print color 'bold yellow';
print ">> [helo test.com]\n";
print color 'reset';
print $sock "helo test.com\r\n";
my $response = <$sock>;
$response =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$response]\n";
print color 'reset';
print color 'bold yellow';
print ">> [mail from:<pete\@killallhumans.ca>]\n";
print color 'reset';
print $sock "mail from:<pete\@killallhumans.ca>\r\n";
$response = <$sock>;
$response =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$response]\n";
print color 'reset';
print color 'bold yellow';
print ">> [rcpt to:<$rcpt>]\n";
print color 'reset';
print $sock "rcpt to:<$rcpt>\r\n";
$response = <$sock>;
$response =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$response]\n";
print color 'reset';
print color 'bold yellow';
print ">> [data]\n";
print color 'reset';
print $sock "data\r\n";
$response = <$sock>;
$response =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$response]\n";
print color 'reset';
#############################################################
# Get the signature file, and send it
#############################################################
my $filename = shift || do {
print color 'bold red';
print("Enter a filename for the body\n");
print color 'reset';
exit( 1 );
};
my $body = get_file( $filename );
for( split /\n/, $body ){
# print color 'bold yellow';
# print ">> [$_]\n";
# print color 'reset';
print $sock "$_\r\n";
}
print color 'bold yellow';
print ">> [.]\n";
print color 'reset';
print $sock "\r\n.\r\n";
$response = <$sock>;
$response =~ s/[\r\n]//g;
print color 'bold green';
print "<< [$response]\n";
print color 'reset';
exit( 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment