RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / bin / fakesmtpserver.pl
1 #!/usr/bin/env perl
2
3 =head1 Fake SMTP Server
4
5 While this script is running, creates an SMTP server at localhost port 25.
6
7 Can only accept one client connection at a time.  If necessary,
8 it could be updated to fork on client connections.
9
10 When an e-mail is delivered, the TO and FROM are printed to STDOUT.
11 The TO, FROM and MSG are saved to a file in $message_save_dir
12
13 Open a saved .eml file with Mozilla Thunderbird (or other mail clients)
14 to review e-mail with all html/pdf attachments
15
16 =cut
17
18 use strict;
19 use warnings;
20
21 use Carp;
22 use Net::SMTP::Server;
23 use Net::SMTP::Server::Client;
24 use Net::SMTP::Server::Relay;
25
26 my $message_save_dir = '/home/freeside/fakesmtpserver';
27
28 mkdir $message_save_dir, 0777;
29
30 my $server = new Net::SMTP::Server('localhost', 25) ||
31     croak("Unable to handle client connection: $!\n");
32
33 while(my $conn = $server->accept()) {
34   my $client = new Net::SMTP::Server::Client($conn) ||
35       croak("Unable to handle client connection: $!\n");
36
37   $client->process || next;
38
39   open my $fh, '>', $message_save_dir.'/'.time().'.eml'
40     or die "error: $!";
41
42   for my $f (qw/TO FROM/) {
43
44       if (ref $client->{$f} eq 'ARRAY') {
45         print "$f: $_\n" for @{$client->{$f}};
46         # print $fh "$f: $_\n" for @{$client->{$f}};
47       } else {
48         print "$f: $client->{$f}\n";
49         # print $fh "$f: $client->{$f}\n";
50       }
51
52   }
53   print $fh "$client->{MSG}\n";
54   print "\n";
55   close $fh;
56 }