Bulk.pm

filter: Statementstmt 73.8% 79 / 107 Branchbran 16.6% 5 / 30 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 91.6% 22 / 24 Podpod 20.0% 1 / 5 totaltotal 59.1% 110 / 186 SCAR 38.9
CC
20
Cov
58%
assemble_mime_kit
52.2
__ANON__
20.0
send
6.1
CRAP 48.8
↑ Summary Bulk/Command.pm »
1 package Email::MIME::Kit::Bulk;
2 BEGIN {
3 12 ▶ $Email::MIME::Kit::Bulk::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 # ABSTRACT: Email::MIME::Kit-based bulk mailer
6 12 $Email::MIME::Kit::Bulk::VERSION = '0.0.1';
7  
8 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Email::MIME::Kit;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Email::Sender::Simple 'sendmail';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use MooseX::Types::Email;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use MooseX::Types::Path::Tiny qw/ Path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use List::AllUtils qw/ sum0 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use MCE::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 ▶use Email::MIME::Kit::Bulk::Kit;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use Email::MIME::Kit::Bulk::Target;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 12 has targets => (
26 traits => ['Array'],
27 isa => 'ArrayRef[Email::MIME::Kit::Bulk::Target]',
28 required => 1,
29 handles => {
30 targets => 'elements',
31 num_targets => 'count',
32 },
33 );
34  
35  
36 12 has kit => (
37 is => 'ro',
38 isa => Path,
39 required => 1,
40 coerce => 1,
41 );
42  
43  
44 12 has from => (
45 is => 'ro',
46 isa => 'MooseX::Types::Email::EmailAddress',
47 required => 1,
48 );
49  
50  
51 12 has processes => (
52 is => 'ro',
53 isa => 'Maybe[Int]',
54 predicate => 'has_processes',
55 );
56  
57 sub single_process {
58 12 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 9 ▶ return $_[0]->processes == 1;
Subroutine single_process: called CC=1 SCAR=0.0
Pod undocumented
60 }
61  
62 12 has verbose => (
63 isa => 'Bool',
64 is => 'ro',
65 default => 0,
66 );
67  
68 12 has transport => (
69 is => 'ro',
70 );
71  
72 sub mime_kit {
73 3 ▶ my $self = shift;
Subroutine mime_kit: called CC=1 SCAR=0.0
Pod undocumented
74 3 Email::MIME::Kit::Bulk::Kit->new({
75 source => $self->kit->stringify,
76 @_,
77 });
78 }
79  
80 around BUILDARGS => sub {
81 10 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=13.0
82 10 my $class = shift;
83  
84 10 my $params = $class->$orig(@_);
85  
86 10 ▶ if (!exists $params->{targets} && exists $params->{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $params->{"targets"} and exists $params->{"to"}) 0 10 10
Condition33%1/3
not exists $params->{"targets"} and exists $params->{"to"}
!ll&&!rl&&r
1000
MC/DC0%0/2
not exists $params->{"targets"} and exists $params->{"to"}
not exists $params->{"targets"} exists $params->{"to"}
Truth table33%1/3
not(A) and B
A= not exists $params->{"targets"}B= exists $params->{"to"}
ABresult
0X0
100
111
87 $params->{targets} = [
88 Email::MIME::Kit::Bulk::Target->new(
89 to => delete $params->{to},
90 0 map { maybe $_ => delete $params->{$_} } qw/ cc bcc /
91 )
92 ];
93 }
94  
95 10 return $params;
96 12 };
97  
98  
99 sub send {
100 9 ▶ my $self = shift;
Subroutine send: called CC=5 SCAR=18.1
Pod documented
101  
102 9 my $af = STDOUT->autoflush;
103  
104 9 ▶ MCE::Map::init { max_workers => $self->processes }
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_processes 0 9 9
105 if $self->has_processes;
106  
107 my $errors = sum0
108 $self->single_process
109 0 ? map { $self->send_target($_) } $self->targets
110 9 ▶ : mce_map { $self->send_target($_) } $self->targets;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$self->single_process ? : 0 9 9
111  
112 1 ▶ warn "\n" . ($self->num_targets - $errors) . ' email(s) sent successfully'
Branch: 2 branches
BranchTrueFalseTotal
if $self->verbose 0 1 1
$errors ? : 0 0 0
113 . ($errors ? " ($errors failure(s))" : '') . "\n" if $self->verbose;
114  
115 1 STDOUT->autoflush($af);
116  
117 1 return $self->num_targets - $errors;
118 }
119  
120 sub send_target {
121 2 ▶ my( $self, $target ) = @_;
Subroutine send_target: called CC=1 SCAR=5.5
Pod undocumented
122  
123 2 my $email = $self->assemble_mime_kit($target);
124  
125 # work around bugs in q-p encoding (it forces \r\n, but the sendmail
126 # executable expects \n, or something like that)
127 0 (my $text = $email->as_string) =~ s/\x0d\x0a/\n/g;
128  
129 return try {
130 0 ▶ sendmail(
Subroutine __ANON__: not called CC=2 SCAR=17.9
131 $text,
132 {
133 from => $target->from,
134 to => [ $target->recipients ],
135 maybe transport => $self->transport,
136 }
137 );
138 0 ▶ print '.' if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
139 0 0;
140 }
141 catch {
142 0 ▶ my @recipients = (blessed($_) && $_->isa('Email::Sender::Failure'))
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('Email::Sender::Failure') ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("Email::Sender::Failure")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Email::Sender::Failure")
builtin::blessed($_) $_->isa("Email::Sender::Failure")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Email::Sender::Failure")
ABresult
0X0
100
111
143 ? ($_->recipients)
144 : ($target->recipients);
145  
146 # XXX better error handling here - logging?
147 0 warn 'Failed to send to ' . join(', ', @recipients) . ': '
148 . "$_";
149  
150 0 ▶ print 'x' if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
151 0 1;
152 0 };
153 }
154  
155 sub assemble_mime_kit {
156 3 ▶ my $self = shift;
Subroutine assemble_mime_kit: called CC=10 SCAR=39.5
Pod undocumented
157 3 my ($target) = @_;
158  
159 3 ▶ my $from = $target->from || $self->from;
Condition66%2/3
$target->from || $self->from
l!l&&r!l&&!r
120
MC/DC0%0/2
$target->from || $self->from
$target->from $self->from
Truth table66%2/3
A || B
A= $target->fromB= $self->from
ABresult
1X1
011
000
160 3 my $to = $target->to;
161 3 my @cc = $target->cc;
162  
163 3 my %opts;
164 3 ▶ $opts{language} = $target->language
Branch: 1 branch
BranchTrueFalseTotal
if $target->has_language 3 0 3
165 if $target->has_language;
166  
167 3 my $kit = $self->mime_kit(%opts);
168 0 my $email = $kit->assemble($target->template_params);
169  
170 0 ▶ if (my @attachments = $target->extra_attachments) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@attachments) = $target->extra_attachments) 0 0 0
171 0 my $old_email = $email;
172  
173 my @parts = map {
174 0 ▶ my $attach = ref($_) ? $_ : [$_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
175 Email::MIME->create(
176 attributes => {
177 filename => $attach->[0],
178 name => $attach->[0],
179 encoding => 'base64',
180 disposition => 'attachment',
181 ($attach->[1]
182 ? (content_type => $attach->[1])
183 : ()),
184 },
185 0 ▶ body => ${ $kit->get_kit_entry($attach->[0]) },
Branch: 1 branch
BranchTrueFalseTotal
$attach->[1] ? : 0 0 0
186 );
187 } @attachments;
188  
189 0 $email = Email::MIME->create(
190 header => [
191 Subject => $old_email->header_obj->header_raw('Subject'),
192 ],
193 parts => [
194 $old_email,
195 @parts,
196 ],
197 );
198 }
199  
200 # XXX Email::MIME::Kit reads the manifest.json file as latin1
201 # fix this in a better way once that is fixed?
202 0 my $subject = $email->header('Subject');
203 0 utf8::decode($subject);
204 0 $email->header_str_set('Subject' => $subject);
205  
206 0 ▶ $email->header_str_set('From' => $from)
Branch: 1 branch
BranchTrueFalseTotal
unless $email->header("From") 0 0 0
207 unless $email->header('From');
208 0 ▶ $email->header_str_set('To' => $to)
Branch: 1 branch
BranchTrueFalseTotal
unless $email->header("To") 0 0 0
209 unless $email->header('To');
210 0 ▶ $email->header_str_set('Cc' => join(', ', @cc))
Branch: 1 branch
BranchTrueFalseTotal
unless $email->header("Cc") or not @cc 0 0 0
Condition0%0/3
$email->header("Cc") or not @cc
l!l&&r!l&&!r
000
MC/DC0%0/2
$email->header("Cc") or not @cc
$email->header("Cc") not @cc
Truth table0%0/3
A or not(B)
A= $email->header("Cc")B= not @cc
ABresult
1X1
011
000
211 unless $email->header('Cc') || !@cc;
212  
213 0 $email->header_str_set(
214 'X-UserAgent'
215 => "Email::MIME::Kit::Bulk v$Email::MIME::Kit::Bulk::VERSION"
216 );
217  
218 0 return $email;
219 }
220  
221 12 __PACKAGE__->meta->make_immutable;
222  
223 12 1;
224  
225 __END__
226  
227 =pod
228  
229 =encoding UTF-8
230  
231 =head1 NAME
232  
233 Email::MIME::Kit::Bulk - Email::MIME::Kit-based bulk mailer
234  
235 =head1 VERSION
236  
237 version 0.0.1
238  
239 =head1 SYNOPSIS
240  
241 use Email::MIME::Kit::Bulk;
242 use Email::MIME::Kit::Bulk::Target;
243  
244 my @targets = (
245 Email::MIME::Kit::Bulk::Target->new(
246 to => 'someone@somewhere.com',
247 ),
248 Email::MIME::Kit::Bulk::Target->new(
249 to => 'someone.else@somewhere.com',
250 cc => 'copied@somewhere.com',
251 language => 'en',
252 ),
253 );
254  
255 my $bulk = Email::MIME::Kit::Bulk->new(
256 kit => '/path/to/mime/kit',
257 processes => 5,
258 targets => \@targets,
259 );
260  
261 $bulk->send;
262  
263 =head1 DESCRIPTION
264  
265 C<Email::MIME::Kit::Bulk> is an extension of L<Email::MIME::Kit> for sending
266 bulk emails. The module can be used directly, or via the
267 companion script C<emk_bulk>.
268  
269 If a language is specified for a target, C<Email::MIME::Kit> will use
270 C<manifest.I<language>.json> to generate its associated email. If no language
271 is given, the regular C<manifest.json> will be used instead.
272  
273 If C<emk_bulk> is used, it'll look in the I<kit> directory for a
274 C<targets.json> file, which it'll use to create the email targets.
275 The format of the C<targets.json> file is a simple serialization of
276 the L<Email::MIME::Kit::Bulk::Target> constructor arguments:
277  
278 [
279 {
280 "to" : "someone@somewhere.com"
281 "cc" : [
282 "someone+cc@somewhere.com"
283 ],
284 "language" : "en",
285 "template_params" : {
286 "superlative" : "Fantastic"
287 },
288 },
289 {
290 "to" : "someone+french@somewhere.com"
291 "cc" : [
292 "someone+frenchcc@somewhere.com"
293 ],
294 "language" : "fr",
295 "template_params" : {
296 "superlative" : "Extraordinaire"
297 },
298 }
299 ]
300  
301 C<Email::MIME::Kit::Bulk> uses L<MCE> to parallize the sending of the emails.
302 The number of processes used can be set via the C<processes> constructor
303 argument. By default L<MCE> will select the number of processes based on
304 the number of available
305 processors. If the number of processes is set to be C<1>, L<MCE> is bypassed
306 altogether.
307  
308 =head1 METHODS
309  
310 =head2 new( %args )
311  
312 Constructor.
313  
314 =head3 Arguments
315  
316 =over
317  
318 =item targets => \@targets
319  
320 Takes in an array of L<Email::MIME::Kit::Bulk::Target> objects,
321 which are the email would-be recipients.
322  
323 Either the argument C<targets> or C<to> must be passed to the constructor.
324  
325 =item to => $email_address
326  
327 Email address of the 'C<To:>' recipient. Ignored if C<targets> is given as well.
328  
329 =item cc => $email_address
330  
331 Email address of the 'C<Cc:>' recipient. Ignored if C<targets> is given as well.
332  
333 =item bcc => $email_address
334  
335 Email address of the 'C<Bcc:>' recipient. Ignored if C<targets> is given as well.
336  
337 =item kit => $path
338  
339 Path of the directory holding the files used by L<Email::MIME::Kit>.
340 Can be a string or a L<Path::Tiny> object.
341  
342 =item from => $email_address
343  
344 'C<From>' address for the email .
345  
346 =item processes => $nbr
347  
348 Maximal number of parallel processes used to send the emails.
349  
350 If not specified, will be chosen by L<MCE>.
351 If set to 1, the parallel processing will be skipped
352 altogether.
353  
354 Not specified by default.
355  
356 =back
357  
358 =head2 send()
359  
360 Send the emails.
361  
362 =head1 AUTHORS
363  
364 =over 4
365  
366 =item *
367  
368 Jesse Luehrs <doy@cpan.org>
369  
370 =item *
371  
372 Yanick Champoux <yanick.champoux@iinteractive.com>
373  
374 =back
375  
376 =head1 COPYRIGHT AND LICENSE
377  
378 This software is copyright (c) 2015 by Infinity Interactive <contact@iinteractive.com>.
379  
380 This is free software; you can redistribute it and/or modify it under
381 the same terms as the Perl 5 programming language system itself.
382  
383 =cut
↑ Summary Bulk/Command.pm »