Skip to content

Instantly share code, notes, and snippets.

@fabioadrianosoares
Created December 5, 2012 18:49
Show Gist options
  • Save fabioadrianosoares/4218375 to your computer and use it in GitHub Desktop.
Save fabioadrianosoares/4218375 to your computer and use it in GitHub Desktop.
Servidor SMTP de teste
use strict;
use warnings;
use feature 'say';
use Carp;
use Data::Dumper;
use Socket;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
my $server = new Net::SMTP::Server('', 25) ||
croak("Unable to handle client connection: $!\n");
say 'Aguardando conexao...';
while(my $conn = $server->accept()) {
# We can perform all sorts of checks here for spammers, ACLs,
# and other useful stuff to check on a connection.
my $remotehost = gethostbyaddr($conn->peeraddr(), AF_INET) . ' [' . inet_ntoa($conn->peeraddr()) . ']';
# Handle the client's connection and spawn off a new parser.
# This can/should be a fork() or a new thread,
# but for simplicity...
if (fork == 0) {
my $client = new Net::SMTP::Server::Client($conn) ||
croak("Unable to handle client connection: $!\n");
# Process the client. This command will block until
# the connecting client completes the SMTP transaction.
if ($client->process) {
# In this simple server, we're just relaying everything
# to a server. If a real server were implemented, you
# could save email to a file, or perform various other
# actions on it here.
say "Recebido e-mail de $remotehost - " . $client->{FROM} . ' - para: ' . join(',', @{$client->{TO}});
} else {
say "ERRO: Cliente $remotehost nao finalizou envio do email.";
}
exit 0;
}
say 'Aguardando proxima conexao...';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment