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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
use Data::Dumper;
use Email::Sender::Transport::SMTP::Persistent;
use Email::Stuffer;
use Encode;
use Encode::Locale;
use Encode::MIME::Header;
use Getopt::Long;
use List::Util;
use MCE;
use MCE::Loop;
use MCE::Flow;
use MCE::Shared;
use Path::Tiny;
use Pod::Usage;
use Time::HiRes qw(gettimeofday tv_interval);
use open ':encoding(locale)';
@ARGV = map { decode(locale => $_, 1) } @ARGV;
# ####
# configurable settings
# ####
my $transport = Email::Sender::Transport::SMTP::Persistent->new({
host => 'localhost',
port => 25,
#debug => 1,
});
my $thread_count = 64;
my $chunksize = 1;
# ####
# end config
# ####
=head1 NAME
mailer.pl - send lots of mails
=head1 SYNOPSIS
mailer.pl options ...
Options:
--help, -h short help message
--to <email or file> recipient(s)
--from <email> from
--subject <string> subject
--content-html <file> body of the mail (html part) (optional)
--content-txt <file> body of the mail (text part)
--debug output extra debug information
=cut
my %opts = ();
Getopt::Long::Configure ("bundling");
pod2usage(-verbose => 0) if (@ARGV== 0);
GetOptions(\%opts, "help|h", "to=s", "from=s", "subject=s", "content-html=s", "content-txt=s", "debug") or pod2usage(2);
pod2usage(0) if $opts{help};
sub trim {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
my $subject = $opts{subject};
my $from = $opts{from};
my $content_html;
my $content_txt;
die "Error: subject not set\n" unless ($opts{subject});
die "Error: from not set\n" unless ($opts{from});
die "Error: content-txt not set\n" unless ($opts{"content-txt"});
die "Error: can't read content-txt: $!\n" unless (-r $opts{"content-txt"});
die "Error: can't read content-html: $!\n" if ($opts{"content-html"} and not -r $opts{"content-html"});
if ($opts{"content-html"}) {
$content_html = path($opts{"content-html"})->slurp_utf8;
}
$content_txt = path($opts{"content-txt"})->slurp_utf8;
print "Building email ... " if $opts{debug};
# create mail
# To will be filled when sending
my $msg = Email::Stuffer->new();
$msg->from($from);
$msg->subject(trim($subject));
$msg->text_body($content_txt);
$msg->html_body($content_html);
$msg->header("Precedence", "bulk");
#$msg->header("List-Unsubscribe", "<mailto:$from>");
$msg->transport($transport);
print "done\n" if $opts{debug};
print "creating workers\n" if $opts{debug};
print "Sending\n";
my $send_time = [gettimeofday];
my $queue = MCE::Shared->queue(await => 1, fast => 1);
mce_flow {
task_name => ['producer', 'sender'],
max_workers => [1, $thread_count]},
sub {
if ($opts{to} =~ m/.+\@.+\..+/) {
$queue->enqueue($opts{to});
} else {
if (-r $opts{to}) {
open my $fh, "<", $opts{to};
while (<$fh>) {
$queue->enqueue($_);
$queue->await($thread_count);
}
close $fh;
} else {
print STDERR "Error: can't read recipient file: $!\n";
}
}
$queue->end();
},
sub {
while (defined (my $to = $queue->dequeue())) {
$msg->to($to);
$msg->send();
print "sent mail in worker ".MCE->wid."\n" if $opts{debug};
}
$transport->disconnect;
};
print "done after ".tv_interval($send_time)." seconds\n";
|