summaryrefslogtreecommitdiff
path: root/regress/smtp/emul.pl
blob: 1fae82979e8a9c61913cd788ed5537f94deba8c0 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env perl
#
# $FML$
#


use strict;
use Carp;
use lib qw(../../fml/lib ../../cpan/lib ../../img/lib);
use vars qw(%option);
use Getopt::Long;

# parse options
GetOptions(\%option, qw(recipient=s sender=s mta=s));
my $me     = 'fukachan@home.fml.org';
my $rcpt   = $option{ recipient } || $me;
my $sender = $option{ sender }    || $me;
my $mta    = $option{ mta }       || '127.0.0.1:1025';

# help
usage() unless @ARGV;
for my $file (@ARGV) { usage() if $file eq '-h';}

# main
for my $file (@ARGV) {
    send_file($file) if -f $file;
}

exit 0;


sub usage
{
    print STDERR <<"EOF";
USAGE:
    $0 [options] file(s)

	--recipient	 ADDRESS
        --sender         ADDRESS
	--mta            HOST:PORT e.g. 127.0.0.1:10025
EOF

    exit(0);
}


sub send_file
{
    my ($file) = @_;
    my $sfp = sub { print STDERR @_;};

    use Mail::Delivery;
    my $service = new Mail::Delivery {
	protocol           => 'SMTP',
	default_io_timeout => 10,
	smtp_log_function  => $sfp,
    };
    if ($service->error) { croak($service->error); return;}

    use Mail::Message;
    my $msg = Mail::Message->parse( { file => $file } ); 
      

    $service->deliver(
		      {
			  smtp_servers    => $mta,

			  smtp_sender     => $sender,
			  recipient_array => [ $rcpt ],
			  recipient_limit => 1000,

			  message         => $msg,
		      });
    if ($service->error) { croak($service->error); return;}
}

1;