Simple SMTP server in PERL

From CT3

Jump to: navigation, search

This simple SMTP server listens on port 25, accepts e-mails using SMTP protocol and prints them on stdout. It can be used in lab environments to test EEM applets without setting up actual e-mail server infrastructure.

Contents

Installation

Store the source in smtpDump.pl and run it with PERL interpreter. The script requires the Net::SMTP::Server and Data::Dumper modules from CPAN. Windows users can install ActivePerl or download the EXE file compiled with perlapp.

Usage guidelines

Usage: smtpDump.pl [''ip-address'']

Command line parameters:

  • ip-address: The IP address to which the SMTP socket is bound. If missing, the server listens on all IP addresses configured on the host.

Author

Ivan Pepelnjak, © 2008 NIL Data Communications

Source code

#!/usr/bin/perl

use strict;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
use Data::Dumper;

our $host = $ARGV[0] || "0.0.0.0" ;

our $server = new Net::SMTP::Server($host) || 
  croak("Unable to open SMTP server socket");

print "Waiting for incoming SMTP connections on ".($host eq "0.0.0.0" ? "all IP addresses":$host)."\n";
$| = 1;
while(my $conn = $server->accept()) {
  print "Incoming mail ... from " . $conn->peerhost() ;
  my $client = new Net::SMTP::Server::Client($conn) || 
    croak("Unable to process incoming request");
  if (!$client->process) { 
    print "\nfailed to receive complete e-mail message\n"; next; }
  print " received\n";
  print "From: $client->{FROM}\n";
  my $to = $client->{TO};
  my $toList = @{$to} ? join(",",@{$to}) : $to;
  print "To:   $toList\n";
  print "\n" ;
  print $client->{MSG};
}
Personal tools

CT3

Main menu