Perl Script to Test for Open Mail Relays

July 30, 2004, updated August 18, 2004

Open relays are bad. You shouldn't ever run one and your server will be swamped by spammers if you do.

As a warning, while this will detect most normal relays, it does not test for vulnerabilities and unknown reactions that some mail servers may have that allow them to exploited as a mail relay. is pretty good at detecting these things. I can personally speak for software like a fully patched Microsoft Exchange 5.5 server which is absolutely horrible at handling mail trying to be relayed.

#!/usr/bin/perl -w
use IO::Socket::INET;
my $SOCKET = IO::Socket::INET->new(
        PeerAddr => "mailserverhost",
        PeerPort => 25,
        Proto   => 'tcp') || die "Error in create socket!";
$SOCKET->autoflush= 0;
# Suck down the greeting and discard it
print $RESPONSE;
print "HELO server";
print $SOCKET "HELO server";
print $RESPONSE;
print "RSET";
print $SOCKET "RSET";
print $RESPONSE;
print "MAIL FROM:<>";
print $SOCKET "MAIL FROM:<>";
print $RESPONSE;
print "RCPT TO:<>";
print $SOCKET "RCPT TO:<>";
print $RESPONSE;
# goodbye
print $SOCKET "QUIT";
# eat any response
print $RESPONSE;
# kill socket

This script can basically be used to automate doing something like telneting to a server to test it's ability to deny an unauthorized relay.

If you are looking for a better solution, the Grinch is a more developed perl script.

Related Posts


Comment February 18, 2009 by
Great Script, I am adapting to get a list of hosts from .txt I can send u once i finish it.
Comment June 23, 2009 by
cool script, simple and usefull