summaryrefslogtreecommitdiff
path: root/bin/fakesmtpserver.pl
blob: 1f2ca3f317ec0a01af8b70ced8e171db50a63bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env perl

=head1 Fake SMTP Server

While this script is running, creates an SMTP server at localhost port 25.

Can only accept one client connection at a time.  If necessary,
it could be updated to fork on client connections.

When an e-mail is delivered, the TO and FROM are printed to STDOUT.
The TO, FROM and MSG are saved to a file in $message_save_dir

=cut

use strict;
use warnings;

use Carp;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
use Net::SMTP::Server::Relay;

my $message_save_dir = '/home/freeside/fakesmtpserver';

mkdir $message_save_dir, 0777;

my $server = new Net::SMTP::Server('localhost', 25) ||
    croak("Unable to handle client connection: $!\n");

while(my $conn = $server->accept()) {
  my $client = new Net::SMTP::Server::Client($conn) ||
      croak("Unable to handle client connection: $!\n");

  $client->process || next;

  open my $fh, '>', $message_save_dir.'/'.time().'.txt'
    or die "error: $!";

  for my $f (qw/TO FROM/) {

      if (ref $client->{$f} eq 'ARRAY') {
        print "$f: $_\n" for @{$client->{$f}};
        print $fh "$f: $_\n" for @{$client->{$f}};
      } else {
        print "$f: $client->{$f}\n";
        print $fh "$f: $client->{$f}\n";
      }

  }
  print $fh "\n\n$client->{MSG}\n";
  print "\n";
  close $fh;
}