line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Bulkmail; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25286
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
App::Bulkmail - Simple but flexible bulkmailer |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=cut |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use App::Bulkmail; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
App::Bulkmail->run(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
... or |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
App::Bulkmail->run( |
23
|
|
|
|
|
|
|
dryrun => 1, |
24
|
|
|
|
|
|
|
dump => 1, |
25
|
|
|
|
|
|
|
template => 'mail.tt', |
26
|
|
|
|
|
|
|
recipients => [ |
27
|
|
|
|
|
|
|
{ email => 'joe@example.net', name => 'Joe Doe' }, |
28
|
|
|
|
|
|
|
{ email => 'jane@example.net', name => 'Jane Roe' }, |
29
|
|
|
|
|
|
|
], |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 ARGUMENTS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over 4 |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item B (filename or scalarref) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
A Template Toolkit template. If the argument is a scalar ref it should contain |
39
|
|
|
|
|
|
|
the template text otherwise is is used as a filename. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=item B (filename or array of hashes) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
A list of recipients. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item B (boolean) |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Wether to send mail. Default is to send mail! |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B (boolean) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Dump mail in mbox format on STDOUT (default: no) |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item B (boolean) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Prevent some messages on STDOUT (default: no) |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item B (boolean) |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Print some extra information on STDOUT (default: no) |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item B |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Use this as progress indicator. Default is to try to instantiate |
64
|
|
|
|
|
|
|
Term::ProgressBAr if neiter dump, quiet, verbose is set. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=back |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
1
|
|
1083
|
use Any::Moose; |
|
1
|
|
|
|
|
34529
|
|
|
1
|
|
|
|
|
7
|
|
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
1
|
|
1492
|
use File::Slurp; |
|
1
|
|
|
|
|
14860
|
|
|
1
|
|
|
|
|
91
|
|
73
|
1
|
|
|
1
|
|
4418
|
use Template; |
|
1
|
|
|
|
|
30783
|
|
|
1
|
|
|
|
|
56
|
|
74
|
1
|
|
|
1
|
|
10
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1530
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
has template => ( |
77
|
|
|
|
|
|
|
is => 'rw', |
78
|
|
|
|
|
|
|
required => 1, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
has recipients => ( |
82
|
|
|
|
|
|
|
is => 'rw', |
83
|
|
|
|
|
|
|
required => 1, |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
for ( qw( dryrun dump quiet verbose progress ) ) { |
87
|
|
|
|
|
|
|
has $_ => ( |
88
|
|
|
|
|
|
|
is => 'rw', |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
around BUILDARGS => sub { |
93
|
|
|
|
|
|
|
my $orig = shift; |
94
|
|
|
|
|
|
|
my $class = shift; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
if ( @_ ) { |
97
|
|
|
|
|
|
|
return $class->$orig( @_ ); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
require Getopt::Long |
101
|
|
|
|
|
|
|
or croak "Couldn't not load Getopt::Long in " . __PACKAGE__ . "\n"; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
my %args; |
104
|
|
|
|
|
|
|
Getopt::Long::GetOptions( \%args, |
105
|
|
|
|
|
|
|
"template=s", |
106
|
|
|
|
|
|
|
"recipients=s", |
107
|
|
|
|
|
|
|
"dump!", |
108
|
|
|
|
|
|
|
"dryrun!", |
109
|
|
|
|
|
|
|
"quiet!", |
110
|
|
|
|
|
|
|
"verbose!", |
111
|
|
|
|
|
|
|
) or croak "Couldn't parse command arguments"; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return \%args; |
114
|
|
|
|
|
|
|
}; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub BUILD { |
117
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Ok, this should really be a coercion |
120
|
0
|
0
|
|
|
|
|
unless ( ref $self->template eq 'SCALAR' ) { |
121
|
0
|
|
|
|
|
|
$self->template( |
122
|
|
|
|
|
|
|
scalar File::Slurp::read_file( $self->template, scalar_ref => 1 ) |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Ok, this should really be a coercion |
127
|
0
|
0
|
|
|
|
|
unless ( ref $self->recipients ) { |
128
|
0
|
|
|
|
|
|
my $file = $self->recipients; |
129
|
0
|
|
|
|
|
|
my $data; |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
if ( $file =~ /\.yaml$/i ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
require YAML; |
133
|
0
|
|
|
|
|
|
$data = YAML::LoadFile( $file ); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} elsif ( $file =~ /\.json$/i ) { |
136
|
0
|
|
|
|
|
|
require JSON; |
137
|
0
|
|
|
|
|
|
$data = JSON::from_json( File::Slurp::read_file( $file ) ); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} elsif ( $file =~ /\.csv$/i ) { |
140
|
0
|
|
|
|
|
|
require Text::CSV_XS; |
141
|
0
|
|
|
|
|
|
$data = [ ]; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my $csv = Text::CSV_XS->new ({ binary => 1 }); |
144
|
0
|
0
|
|
|
|
|
open my $fh, "<", $file or croak("Couldn't open file $file: $!\n"); |
145
|
0
|
|
|
|
|
|
my @fields = @{ $csv->getline ($fh) }; |
|
0
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
while ( my $row = $csv->getline ($fh) ) { |
147
|
0
|
|
|
|
|
|
push @{ $data }, { }; |
|
0
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
@{ $data->[-1] }{ @fields } = @{ $row }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
} |
150
|
0
|
0
|
|
|
|
|
$csv->eof or croak("".$csv->error_diag); |
151
|
|
|
|
|
|
|
} else { |
152
|
0
|
|
|
|
|
|
$self->recipients( |
153
|
|
|
|
|
|
|
File::Slurp::read_file( $file, array_ref => 1 ) |
154
|
|
|
|
|
|
|
) |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
0
|
0
|
|
|
|
|
if ( ref $data eq 'HASH' ) { |
158
|
0
|
|
0
|
|
|
|
$data = [ |
159
|
0
|
|
|
|
|
|
map { $data->{ $_ }->{email} ||= $_; $data->{ $_ } } |
|
0
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
sort keys %{ $data } |
161
|
|
|
|
|
|
|
]; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
$self->recipients( $data ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# progress should probably just be made lazy |
168
|
0
|
0
|
0
|
|
|
|
unless ( defined($self->progress) || $self->quiet || $self->verbose || $self->dump ) { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
169
|
0
|
|
|
|
|
|
$self->progress( |
170
|
|
|
|
|
|
|
Term::ProgressBar->new({ |
171
|
0
|
0
|
|
|
|
|
count => scalar @{ $self->recipients }, |
172
|
|
|
|
|
|
|
ETA => 'linear', |
173
|
|
|
|
|
|
|
}) |
174
|
|
|
|
|
|
|
) if require Term::ProgressBar; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub run { |
180
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
unless( blessed $self ) { |
183
|
0
|
|
|
|
|
|
$self = $self->new( @_ ); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my $template = $self->template; |
187
|
0
|
|
|
|
|
|
my $verbose = $self->verbose; |
188
|
0
|
|
|
|
|
|
my $dump = $self->dump; |
189
|
0
|
|
|
|
|
|
my $dryrun = $self->dryrun; |
190
|
0
|
|
|
|
|
|
my $progress = $self->progress; |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
my $count = scalar @{ $self->recipients }; |
|
0
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
my $fmt; |
194
|
0
|
0
|
0
|
|
|
|
if ( $verbose || $dump ) { |
195
|
0
|
|
|
|
|
|
my $len = length "$count"; |
196
|
0
|
|
|
|
|
|
$fmt = "[%${len}d/%${len}d] %s"; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
my $tt = Template->new(); |
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
my $done; |
202
|
0
|
|
|
|
|
|
for my $recipient (@{ $self->recipients }) { |
|
0
|
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
$done += 1; |
204
|
0
|
0
|
|
|
|
|
printf $fmt, $done, $count, "Processing $recipient\n" if $verbose; |
205
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
my $mail; |
207
|
|
|
|
|
|
|
$tt->process($template, $recipient, \$mail) |
208
|
0
|
0
|
|
|
|
|
or do { |
209
|
0
|
|
|
|
|
|
warn "Couldn't process template for $recipient"; |
210
|
0
|
|
|
|
|
|
next; |
211
|
|
|
|
|
|
|
}; |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
printf "From bulkmailer.pl $fmt", $done, $count, "\n$mail" if $dump; |
214
|
|
|
|
|
|
|
|
215
|
0
|
0
|
|
|
|
|
next if $dryrun; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
open my $sendmail, "| /usr/lib/sendmail -t" |
218
|
0
|
0
|
|
|
|
|
or do { |
219
|
0
|
|
|
|
|
|
warn "Couldn't open sendmail while processing $recipient: $!"; |
220
|
0
|
|
|
|
|
|
next; |
221
|
|
|
|
|
|
|
}; |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
|
|
|
print $sendmail $mail; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
close $sendmail |
226
|
0
|
0
|
|
|
|
|
or do { |
227
|
0
|
|
|
|
|
|
warn "Couldn't close sendmail while processing $recipient: $!"; |
228
|
0
|
|
|
|
|
|
next; |
229
|
|
|
|
|
|
|
}; |
230
|
|
|
|
|
|
|
} continue { |
231
|
0
|
0
|
|
|
|
|
$progress->update($done) if $progress; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
0
|
0
|
|
|
|
|
$progress->update( $count ) if $progress; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 AUTHOR |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Peter Makholm, C<< >> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head1 BUGS |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
244
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
245
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 SUPPORT |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
perldoc App::Bulkmail |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
You can also look for information at: |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=over 4 |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
L |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
L |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=item * CPAN Ratings |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
L |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * Search CPAN |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
L |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=back |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Copyright 2009 Peter Makholm. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
288
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
289
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=cut |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
1; # End of App::Bulkmail |