blib/lib/Email/Stuffer.pm

filter: Statementstmt 93.6% 178 / 190 Branchbran 68.0% 68 / 100 Conditioncond 65.0% 13 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 94.7% 36 / 38 Podpod 95.0% 19 / 20 totaltotal 83.6% 318 / 380 SCAR 43.7
CC
62
Cov
84%
_detect_content_type
9.5
new
8.3
attach_file
7.7
CRAP 79.1
↑ Summary
1 9 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Stuffer 0.019;
4 # ABSTRACT: A more casual approach to creating and sending Email:: emails
5  
6 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod # Prepare the message
11 #pod my $body = <<'AMBUSH_READY';
12 #pod Dear Santa
13 #pod
14 #pod I have killed Bun Bun.
15 #pod
16 #pod Yes, I know what you are thinking... but it was actually a total accident.
17 #pod
18 #pod I was in a crowded line at a BayWatch signing, and I tripped, and stood on
19 #pod his head.
20 #pod
21 #pod I know. Oops! :/
22 #pod
23 #pod So anyways, I am willing to sell you the body for $1 million dollars.
24 #pod
25 #pod Be near the pinhole to the Dimension of Pain at midnight.
26 #pod
27 #pod Alias
28 #pod
29 #pod AMBUSH_READY
30 #pod
31 #pod # Create and send the email in one shot
32 #pod Email::Stuffer->from ('cpan@ali.as' )
33 #pod ->to ('santa@northpole.org' )
34 #pod ->bcc ('bunbun@sluggy.com' )
35 #pod ->text_body($body )
36 #pod ->attach_file('dead_bunbun_faked.gif' )
37 #pod ->send;
38 #pod
39 #pod =head1 DESCRIPTION
40 #pod
41 #pod B<The basics should all work, but this module is still subject to
42 #pod name and/or API changes>
43 #pod
44 #pod Email::Stuffer, as its name suggests, is a fairly casual module used
45 #pod to stuff things into an email and send them. It is a high-level module
46 #pod designed for ease of use when doing a very specific common task, but
47 #pod implemented on top of the light and tolerable Email:: modules.
48 #pod
49 #pod Email::Stuffer is typically used to build emails and send them in a single
50 #pod statement, as seen in the synopsis. And it is certain only for use when
51 #pod creating and sending emails. As such, it contains no email parsing
52 #pod capability, and little to no modification support.
53 #pod
54 #pod To re-iterate, this is very much a module for those "slap it together and
55 #pod fire it off" situations, but that still has enough grunt behind the scenes
56 #pod to do things properly.
57 #pod
58 #pod =head2 Default Transport
59 #pod
60 #pod Although it cannot be relied upon to work, the default behaviour is to
61 #pod use C<sendmail> to send mail, if you don't provide the mail send channel
62 #pod with either the C<transport> method, or as an argument to C<send>.
63 #pod
64 #pod (Actually, the choice of default is delegated to
65 #pod L<Email::Sender::Simple>, which makes its own choices. But usually, it
66 #pod uses C<sendmail>.)
67 #pod
68 #pod =head2 Why use this?
69 #pod
70 #pod Why not just use L<Email::Simple> or L<Email::MIME>? After all, this just adds
71 #pod another layer of stuff around those. Wouldn't using them directly be better?
72 #pod
73 #pod Certainly, if you know EXACTLY what you are doing. The docs are clear enough,
74 #pod but you really do need to have an understanding of the structure of MIME
75 #pod emails. This structure is going to be different depending on whether you have
76 #pod text body, HTML, both, with or without an attachment etc.
77 #pod
78 #pod Then there's brevity... compare the following roughly equivalent code.
79 #pod
80 #pod First, the Email::Stuffer way.
81 #pod
82 #pod Email::Stuffer->to('Simon Cozens<simon@somewhere.jp>')
83 #pod ->from('Santa@northpole.org')
84 #pod ->text_body("You've been good this year. No coal for you.")
85 #pod ->attach_file('choochoo.gif')
86 #pod ->send;
87 #pod
88 #pod And now doing it directly with a knowledge of what your attachment is, and
89 #pod what the correct MIME structure is.
90 #pod
91 #pod use Email::MIME;
92 #pod use Email::Sender::Simple;
93 #pod use IO::All;
94 #pod
95 #pod Email::Sender::Simple->try_to_send(
96 #pod Email::MIME->create(
97 #pod header => [
98 #pod To => 'simon@somewhere.jp',
99 #pod From => 'santa@northpole.org',
100 #pod ],
101 #pod parts => [
102 #pod Email::MIME->create(
103 #pod body => "You've been a good boy this year. No coal for you."
104 #pod ),
105 #pod Email::MIME->create(
106 #pod body => io('choochoo.gif'),
107 #pod attributes => {
108 #pod filename => 'choochoo.gif',
109 #pod content_type => 'image/gif',
110 #pod },
111 #pod ),
112 #pod ],
113 #pod );
114 #pod );
115 #pod
116 #pod Again, if you know MIME well, and have the patience to manually code up
117 #pod the L<Email::MIME> structure, go do that, if you really want to.
118 #pod
119 #pod Email::Stuffer as the name suggests, solves one case and one case only:
120 #pod generate some stuff, and email it to somewhere, as conveniently as
121 #pod possible. DWIM, but do it as thinly as possible and use the solid
122 #pod Email:: modules underneath.
123 #pod
124 #pod =head1 COOKBOOK
125 #pod
126 #pod Here is another example (maybe plural later) of how you can use
127 #pod Email::Stuffer's brevity to your advantage.
128 #pod
129 #pod =head2 Custom Alerts
130 #pod
131 #pod package SMS::Alert;
132 #pod use base 'Email::Stuffer';
133 #pod
134 #pod sub new {
135 #pod shift()->SUPER::new(@_)
136 #pod ->from('monitor@my.website')
137 #pod # Of course, we could have pulled these from
138 #pod # $MyConfig->{support_tech} or something similar.
139 #pod ->to('0416181595@sms.gateway')
140 #pod ->transport('SMTP', { host => '123.123.123.123' });
141 #pod }
142 #pod
143 #pod Z<>
144 #pod
145 #pod package My::Code;
146 #pod
147 #pod unless ( $Server->restart ) {
148 #pod # Notify the admin on call that a server went down and failed
149 #pod # to restart.
150 #pod SMS::Alert->subject("Server $Server failed to restart cleanly")
151 #pod ->send;
152 #pod }
153 #pod
154 #pod =head1 METHODS
155 #pod
156 #pod As you can see from the synopsis, all methods that B<modify> the
157 #pod Email::Stuffer object returns the object, and thus most normal calls are
158 #pod chainable.
159 #pod
160 #pod However, please note that C<send>, and the group of methods that do not
161 #pod change the Email::Stuffer object B<do not> return the object, and thus
162 #pod B<are not> chainable.
163 #pod
164 #pod =cut
165  
166 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
167 9 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
168 9 ▶use Params::Util 1.05 qw(_INSTANCE _INSTANCEDOES);
Subroutine BEGIN: called CC=1 SCAR=0.0
169 9 ▶use Email::MIME 1.943 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
170 9 ▶use Email::MIME::Creator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
171 9 ▶use Email::Sender::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
172 9 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
173  
174 #####################################################################
175 # Constructor and Accessors
176  
177 #pod =method new
178 #pod
179 #pod Creates a new, empty, Email::Stuffer object.
180 #pod
181 #pod You can pass a hashref of properties to set, including:
182 #pod
183 #pod =for :list
184 #pod * to
185 #pod * from
186 #pod * cc
187 #pod * bcc
188 #pod * reply_to
189 #pod * subject
190 #pod * text_body
191 #pod * html_body
192 #pod * transport
193 #pod
194 #pod The to, cc, bcc, and reply_to headers properties may be provided as array
195 #pod references. The array's contents will be used as the list of arguments to the
196 #pod setter.
197 #pod
198 #pod =cut
199  
200 9 my %IS_INIT_ARG = map {; $_ => 1 } qw(
201 to from cc bcc reply_to subject text_body html_body transport
202 );
203  
204 9 my %IS_ARRAY_ARG = map {; $_ => 1 } qw(
205 to cc bcc reply_to
206 transport
207 );
208  
209 sub new {
210 12 ▶ Carp::croak("new method called on Email::Stuffer instance") if ref $_[0];
Subroutine new: called CC=8 SCAR=21.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 12 12
211  
212 12 my ($class, $arg) = @_;
213  
214 12 my $self = bless {
215 parts => [],
216 email => Email::MIME->create(
217 header => [],
218 parts => [],
219 ),
220 }, $class;
221  
222 12 ▶ my @init_args = keys %{ $arg || {} };
Branch: 1 branch
BranchTrueFalseTotal
$arg or {} 11 1 12
223 12 ▶ if (my @bogus = grep {; ! $IS_INIT_ARG{$_} } @init_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bogus) = grep({not $IS_INIT_ARG{$_};} @init_args)) 0 12 12
224 0 Carp::croak("illegal arguments to Email::Stuffer->new: @bogus");
225 }
226  
227 12 for my $init_arg (@init_args) {
228 2 my @args = $arg->{$init_arg};
229 2 ▶ if ($IS_ARRAY_ARG{$init_arg} && ref $args[0] && ref $args[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_ARRAY_ARG{$init_arg} and ref $args[0] and ref $args[0] eq "ARRAY") 1 1 2
Condition66%4/6
$IS_ARRAY_ARG{$init_arg} and ref $args[0] and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
101
$IS_ARRAY_ARG{$init_arg} and ref $args[0]
!ll&&!rl&&r
101
MC/DC33%1/3
$IS_ARRAY_ARG{$init_arg} and ref $args[0] and ref $args[0] eq "ARRAY"
$IS_ARRAY_ARG{$init_arg} ref $args[0] ref $args[0] eq "ARRAY"
Truth table50%2/4
A and B and C
A= $IS_ARRAY_ARG{$init_arg}B= ref $args[0]C= ref $args[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
230 1 @args = @{ $args[0] };
231 }
232  
233 2 $self->$init_arg(@args);
234 }
235  
236 12 $self;
237 }
238  
239 sub _self {
240 68 ▶ my $either = shift;
Subroutine _self: called CC=2 SCAR=6.9
241 68 ▶ ref($either) ? $either : $either->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $either ? : 60 8 68
242 }
243  
244 #pod =method header_names
245 #pod
246 #pod Returns, as a list, all of the headers currently set for the Email
247 #pod For backwards compatibility, this method can also be called as B[headers].
248 #pod
249 #pod =cut
250  
251 sub header_names {
252 0 ▶ shift()->{email}->header_names;
Subroutine header_names: not called CC=1 SCAR=6.9
Pod documented
253 }
254  
255 sub headers {
256 1 ▶ shift()->{email}->header_names; ## This is now header_names, headers is depreciated
Subroutine headers: called CC=1 SCAR=0.0
Pod undocumented
257 }
258  
259 #pod =method parts
260 #pod
261 #pod Returns, as a list, the L<Email::MIME> parts for the Email
262 #pod
263 #pod =cut
264  
265 sub parts {
266 61 ▶ grep { defined $_ } @{shift()->{parts}};
Subroutine parts: called CC=1 SCAR=0.0
Pod documented
267 }
268  
269 #####################################################################
270 # Header Methods
271  
272 #pod =method header $header => $value
273 #pod
274 #pod Sets a named header in the email. Multiple calls with the same $header
275 #pod will overwrite previous calls $value.
276 #pod
277 #pod =cut
278  
279 sub header {
280 3 ▶ my $self = shift()->_self;
Subroutine header: called CC=2 SCAR=7.0
Pod documented
281 3 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
282 3 $self->{email}->header_str_set(ucfirst shift, shift);
283 3 return $self;
284 }
285  
286 #pod =method to @addresses
287 #pod
288 #pod Sets the To: header in the email
289 #pod
290 #pod =cut
291  
292 sub _assert_addr_list_ok {
293 27 ▶ my ($self, $header, $allow_empty, $list) = @_;
Subroutine _assert_addr_list_ok: called CC=7 SCAR=20.3
294  
295 27 ▶ Carp::croak("$header is a required field")
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_empty or @$list 0 27 27
Condition66%2/3
$allow_empty or @$list
l!l&&r!l&&!r
4230
MC/DC0%0/2
$allow_empty or @$list
$allow_empty @$list
Truth table66%2/3
A or B
A= $allow_emptyB= @$list
ABresult
1X1
011
000
296 unless $allow_empty or @$list;
297  
298 27 for (@$list) {
299 34 ▶ Carp::croak("list of $header headers contains undefined values")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 34 34
300 unless defined;
301  
302 34 ▶ Carp::croak("list of $header headers contains unblessed references")
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ and not builtin::blessed($_) 3 31 34
Condition66%2/3
ref $_ and not builtin::blessed($_)
!ll&&!rl&&r
3103
MC/DC50%1/2
ref $_ and not builtin::blessed($_)
ref $_ not builtin::blessed($_)
Truth table66%2/3
A and not(B)
A= ref $_B= not builtin::blessed($_)
ABresult
0X0
100
111
303 if ref && ! blessed $_;
304 }
305 }
306  
307 sub to {
308 12 ▶ my $self = shift()->_self;
Subroutine to: called CC=2 SCAR=6.9
Pod documented
309 12 $self->_assert_addr_list_ok(to => 0 => \@_);
310 11 ▶ $self->{email}->header_str_set(To => (@_ > 1 ? \@_ : @_));
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2 9 11
311 11 return $self;
312 }
313  
314 #pod =method from $address
315 #pod
316 #pod Sets the From: header in the email
317 #pod
318 #pod =cut
319  
320 sub from {
321 10 ▶ my $self = shift()->_self;
Subroutine from: called CC=2 SCAR=7.0
Pod documented
322 10 $self->_assert_addr_list_ok(from => 0 => \@_);
323 10 ▶ Carp::croak("only one address is allowed in the from header") if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 10 10
324 10 $self->{email}->header_str_set(From => shift);
325 10 return $self;
326 }
327  
328 #pod =method reply_to $address
329 #pod
330 #pod Sets the Reply-To: header in the email
331 #pod
332 #pod =cut
333  
334 sub reply_to {
335 1 ▶ my $self = shift()->_self;
Subroutine reply_to: called CC=2 SCAR=7.0
Pod documented
336 1 $self->_assert_addr_list_ok('reply-to' => 0 => \@_);
337 1 ▶ Carp::croak("only one address is allowed in the reply-to header") if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1 1
338 1 $self->{email}->header_str_set('Reply-To' => shift);
339 1 return $self;
340 }
341  
342 #pod =method cc @addresses
343 #pod
344 #pod Sets the Cc: header in the email
345 #pod
346 #pod =cut
347  
348 sub cc {
349 2 ▶ my $self = shift()->_self;
Subroutine cc: called CC=2 SCAR=7.0
Pod documented
350 2 $self->_assert_addr_list_ok(cc => 1 => \@_);
351 1 ▶ $self->{email}->header_str_set(Cc => (@_ > 1 ? \@_ : @_));
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
352 1 return $self;
353 }
354  
355 #pod =method bcc @addresses
356 #pod
357 #pod Sets the Bcc: header in the email
358 #pod
359 #pod =cut
360  
361 sub bcc {
362 2 ▶ my $self = shift()->_self;
Subroutine bcc: called CC=2 SCAR=7.0
Pod documented
363 2 $self->_assert_addr_list_ok(bcc => 1 => \@_);
364 1 ▶ $self->{email}->header_str_set(Bcc => (@_ > 1 ? \@_ : @_));
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
365 1 return $self;
366 }
367  
368 #pod =method subject $text
369 #pod
370 #pod Sets the Subject: header in the email
371 #pod
372 #pod =cut
373  
374 sub subject {
375 10 ▶ my $self = shift()->_self;
Subroutine subject: called CC=2 SCAR=7.0
Pod documented
376 10 ▶ Carp::croak("subject is a required field") unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 10 10
377 10 $self->{email}->header_str_set(Subject => shift);
378 10 return $self;
379 }
380  
381 #####################################################################
382 # Body and Attachments
383  
384 #pod =method text_body $body [, $attributes => $value, ... ]
385 #pod
386 #pod Sets the text body of the email. Appropriate headers are set for you.
387 #pod You may override MIME attributes as needed. See the C<attributes>
388 #pod parameter to L<Email::MIME/create> for the headers you can set.
389 #pod
390 #pod If C<$body> is undefined, this method will do nothing.
391 #pod
392 #pod Prior to Email::Stuffer version 0.015 text body was marked as flowed,
393 #pod which broke all pre-formated body text. Empty space at the beggining
394 #pod of the line was dropped and every new line character could be changed
395 #pod to one space (and vice versa). Version 0.015 (and later) does not set
396 #pod flowed format automatically anymore and so text body is really plain
397 #pod text. If you want to use old behavior of "advanced" flowed formatting,
398 #pod set flowed format manually by: C<< text_body($body, format => 'flowed') >>.
399 #pod
400 #pod =cut
401  
402 sub text_body {
403 7 ▶ my $self = shift()->_self;
Subroutine text_body: called CC=2 SCAR=7.0
Pod documented
404 7 ▶ my $body = defined $_[0] ? shift : return $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 7 0 7
405 7 my %attr = (
406 # Defaults
407 content_type => 'text/plain',
408 charset => 'utf-8',
409 encoding => 'quoted-printable',
410 # Params overwrite them
411 @_,
412 );
413  
414 # Create the part in the text slot
415 7 $self->{parts}->[0] = Email::MIME->create(
416 attributes => \%attr,
417 body_str => $body,
418 );
419  
420 7 $self;
421 }
422  
423 #pod =method html_body $body [, $header => $value, ... ]
424 #pod
425 #pod Sets the HTML body of the email. Appropriate headers are set for you.
426 #pod You may override MIME attributes as needed. See the C<attributes>
427 #pod parameter to L<Email::MIME/create> for the headers you can set.
428 #pod
429 #pod If C<$body> is undefined, this method will do nothing.
430 #pod
431 #pod =cut
432  
433 sub html_body {
434 2 ▶ my $self = shift()->_self;
Subroutine html_body: called CC=2 SCAR=7.0
Pod documented
435 2 ▶ my $body = defined $_[0] ? shift : return $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
436 2 my %attr = (
437 # Defaults
438 content_type => 'text/html',
439 charset => 'utf-8',
440 encoding => 'quoted-printable',
441 # Params overwrite them
442 @_,
443 );
444  
445 # Create the part in the HTML slot
446 2 $self->{parts}->[1] = Email::MIME->create(
447 attributes => \%attr,
448 body_str => $body,
449 );
450  
451 2 $self;
452 }
453  
454 #pod =method attach $contents [, $attribute => $value, ... ]
455 #pod
456 #pod Adds an attachment to the email. The first argument is the file contents
457 #pod followed by (as for text_body and html_body) the list of headers to use.
458 #pod Email::Stuffer will I<try> to guess the headers correctly, but you may wish
459 #pod to provide them anyway to be sure. Encoding is Base64 by default. See
460 #pod the C<attributes> parameter to L<Email::MIME/create> for the headers you
461 #pod can set.
462 #pod
463 #pod =cut
464  
465 sub _detect_content_type {
466 11 ▶ my ($filename, $body) = @_;
Subroutine _detect_content_type: called CC=9 SCAR=22.5
467  
468 11 ▶ if (defined($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 6 5 11
469 6 ▶ if ($filename =~ /\.([a-zA-Z]{3,4})\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.([a-zA-Z]{3,4})\z/u) 6 0 6
470 my $content_type = {
471 'gif' => 'image/gif',
472 'png' => 'image/png',
473 'jpg' => 'image/jpeg',
474 'jpeg' => 'image/jpeg',
475 'txt' => 'text/plain',
476 'htm' => 'text/html',
477 'html' => 'text/html',
478 'css' => 'text/css',
479 'csv' => 'text/csv',
480 'pdf' => 'application/pdf',
481 'wav' => 'audio/wav',
482 6 }->{lc($1)};
483 6 ▶ return $content_type if defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type 6 0 6
484 }
485 }
486 5 ▶ if ($body =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ / \A(?: (GIF8) # gif | (\xff\xd8) # jpeg | (\x89PNG) # png | (%PDF-) # pdf ) /ux) 5 0 5
487 \A(?:
488 (GIF8) # gif
489 | (\xff\xd8) # jpeg
490 | (\x89PNG) # png
491 | (%PDF-) # pdf
492 )
493 /x) {
494 5 ▶ return 'image/gif' if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 1 4 5
495 4 ▶ return 'image/jpeg' if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 3 4
496 3 ▶ return 'image/png' if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 2 1 3
497 1 ▶ return 'application/pdf' if $4;
Branch: 1 branch
BranchTrueFalseTotal
if $4 1 0 1
498 }
499 0 return 'application/octet-stream';
500 }
501  
502 sub attach {
503 12 ▶ my $self = shift()->_self;
Subroutine attach: called CC=4 SCAR=13.9
Pod documented
504 12 ▶ my $body = defined $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 12 0 12
505 12 my %attr = (
506 # Cheap defaults
507 encoding => 'base64',
508 # Params overwrite them
509 @_,
510 );
511  
512 # The more expensive defaults if needed
513 12 ▶ unless ( $attr{content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr{"content_type"}) 11 1 12
514 11 $attr{content_type} = _detect_content_type($attr{filename}, $body);
515 }
516  
517 ### MORE?
518  
519 # Determine the slot to put it at
520 12 my $slot = scalar @{$self->{parts}};
521 12 ▶ $slot = 3 if $slot < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $slot < 3 3 9 12
522  
523 # Create the part in the attachment slot
524 12 $self->{parts}->[$slot] = Email::MIME->create(
525 attributes => \%attr,
526 body => $body,
527 );
528  
529 12 $self;
530 }
531  
532 #pod =method attach_file $file [, $attribute => $value, ... ]
533 #pod
534 #pod Attachs a file that already exists on the filesystem to the email.
535 #pod C<attach_file> will attempt to auto-detect the MIME type, and use the
536 #pod file's current name when attaching. See the C<attributes> parameter to
537 #pod L<Email::MIME/create> for the headers you can set.
538 #pod
539 #pod =cut
540  
541 sub attach_file {
542 9 ▶ my $self = shift;
Subroutine attach_file: called CC=7 SCAR=20.4
Pod documented
543 9 my $body_arg = shift;
544 9 my $name = undef;
545 9 my $body = undef;
546  
547 # Support IO::All::File arguments
548 9 ▶ if ( Params::Util::_INSTANCE($body_arg, 'IO::All::File') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&Params::Util::_INSTANCE($body_arg, 'IO::All::File')) { } 0 9 9
elsif (defined $body_arg and &Params::Util::_STRING($body_arg)) { } 8 1 9
549 0 $body_arg->binmode;
550 0 $name = $body_arg->name;
551 0 $body = $body_arg->all;
552  
553 # Support file names
554 ▶ } elsif ( defined $body_arg and Params::Util::_STRING($body_arg) ) {
Condition66%2/3
defined $body_arg and &Params::Util::_STRING($body_arg)
!ll&&!rl&&r
018
MC/DC50%1/2
defined $body_arg and &Params::Util::_STRING($body_arg)
defined $body_arg &Params::Util::_STRING($body_arg)
Truth table66%2/3
A and B
A= defined $body_argB= &Params::Util::_STRING($body_arg)
ABresult
0X0
100
111
555 8 ▶ croak "No such file '$body_arg'" unless -f $body_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $body_arg 1 7 8
556 7 $name = $body_arg;
557 7 $body = _slurp( $body_arg );
558  
559 # That's it
560 } else {
561 1 ▶ my $type = ref($body_arg) || "<$body_arg>";
Condition50%1/2
ref $body_arg || "<$body_arg>"
l!l
10
MC/DC0%0/1
ref $body_arg || "<$body_arg>"
ref $body_arg
Truth table50%1/2
A
A= ref $body_arg
Aresult
11
00
562 1 croak "Expected a file name or an IO::All::File derivative, got $type";
563 }
564  
565 # Clean the file name
566 7 $name = File::Basename::basename($name);
567  
568 7 ▶ croak("basename somehow returned undef") unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
569  
570 # Now attach as normal
571 7 $self->attach( $body, name => $name, filename => $name, @_ );
572 }
573  
574 # Provide a simple _slurp implementation
575 sub _slurp {
576 8 ▶ my $file = shift;
Subroutine _slurp: called CC=3 SCAR=11.0
577 8 local $/ = undef;
578  
579 8 ▶ open my $slurp, '<:raw', $file or croak("error opening $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $slurp, "<:raw", $file or croak("error opening ${file}: $!") 1 7 8
580 7 my $source = <$slurp>;
581 7 ▶ close( $slurp ) or croak "error after slurping $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $slurp or croak("error after slurping ${file}: $!") 0 7 7
582 7 \$source;
583 }
584  
585 #pod =method transport
586 #pod
587 #pod $stuffer->transport( $moniker, @options )
588 #pod
589 #pod or
590 #pod
591 #pod $stuffer->transport( $transport_obj )
592 #pod
593 #pod The C<transport> method specifies the L<Email::Sender> transport that
594 #pod you want to use to send the email, and any options that need to be
595 #pod used to instantiate the transport. C<$moniker> is used as the transport
596 #pod name; if it starts with an equals sign (C<=>) then the text after the
597 #pod sign is used as the class. Otherwise, the text is prepended by
598 #pod C<Email::Sender::Transport::>.
599 #pod
600 #pod Alternatively, you can pass a complete transport object (which must be
601 #pod an L<Email::Sender::Transport> object) and it will be used as is.
602 #pod
603 #pod =cut
604  
605 sub transport {
606 7 ▶ my $self = shift()->_self;
Subroutine transport: called CC=4 SCAR=19.2
Pod documented
607  
608 7 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 0 7
609 # Change the transport
610 7 ▶ if ( _INSTANCEDOES($_[0], 'Email::Sender::Transport') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCEDOES($_[0], 'Email::Sender::Transport')) { } 7 0 7
611 7 $self->{transport} = shift;
612 } else {
613 0 my ($moniker, @arg) = @_;
614 0 ▶ my $class = $moniker =~ s/\A=//
Branch: 1 branch
BranchTrueFalseTotal
$moniker =~ s/\A=//u ? : 0 0 0
615 ? $moniker
616 : "Email::Sender::Transport::$moniker";
617 0 require_module($class);
618 0 my $transport = $class->new(@arg);
619 0 $self->{transport} = $transport;
620 }
621 }
622  
623 7 $self;
624 }
625  
626 #####################################################################
627 # Output Methods
628  
629 #pod =method email
630 #pod
631 #pod Creates and returns the full L<Email::MIME> object for the email.
632 #pod
633 #pod =cut
634  
635 sub email {
636 61 ▶ my $self = shift;
Subroutine email: called CC=6 SCAR=18.0
Pod documented
637 61 my @parts = $self->parts;
638  
639 ### Lyle Hopkins, code added to Fix single part, and multipart/alternative
640 ### problems
641 61 ▶ if (scalar(@{ $self->{parts} }) >= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$self->{'parts'};} >= 3) { } 3 58 61
elsif (scalar @{$self->{'parts'};}) { } 5 53 58
642 ## multipart/mixed
643 3 $self->{email}->parts_set(\@parts);
644 58 } elsif (scalar(@{ $self->{parts} })) {
645 ## Check we actually have any parts
646 5 ▶ if ( _INSTANCE($parts[0], 'Email::MIME')
Branch: 2 branches
BranchTrueFalseTotal
if (&_INSTANCE($parts[0], 'Email::MIME') and &_INSTANCE($parts[1], 'Email::MIME')) { } 1 4 5
elsif (&_INSTANCE($parts[0], 'Email::MIME')) { } 4 0 4
Condition66%2/3
&_INSTANCE($parts[0], "Email::MIME") and &_INSTANCE($parts[1], "Email::MIME")
!ll&&!rl&&r
041
MC/DC50%1/2
&_INSTANCE($parts[0], "Email::MIME") and &_INSTANCE($parts[1], "Email::MIME")
&_INSTANCE($parts[0], "Email::MIME") &_INSTANCE($parts[1], "Email::MIME")
Truth table66%2/3
A and B
A= &_INSTANCE($parts[0], "Email::MIME")B= &_INSTANCE($parts[1], "Email::MIME")
ABresult
0X0
100
111
647 && _INSTANCE($parts[1], 'Email::MIME')
648 ) {
649 ## multipart/alternate
650 1 $self->{email}->header_set('Content-Type' => 'multipart/alternative');
651 1 $self->{email}->parts_set(\@parts);
652 } elsif (_INSTANCE($parts[0], 'Email::MIME')) {
653 ## As @parts is $self->parts without the blanks, we only need check
654 ## $parts[0]
655 ## single part text/plain
656 4 _transfer_headers($self->{email}, $parts[0]);
657 4 $self->{email} = $parts[0];
658 }
659 }
660  
661 61 $self->{email};
662 }
663  
664 # Support coercion to an Email::MIME
665 0 ▶sub __as_Email_MIME { shift()->email }
Subroutine __as_Email_MIME: not called CC=1 SCAR=6.9
666  
667 # Quick any routine
668 sub _any (&@) {
669 20 ▶ my $f = shift;
Subroutine _any: called CC=4 SCAR=13.9
670 20 ▶ return if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 20 20
671 20 for (@_) {
672 60 ▶ return 1 if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 8 52 60
673 }
674 12 return 0;
675 }
676  
677 # header transfer from one object to another
678 sub _transfer_headers {
679 # $_[0] = from, $_[1] = to
680 4 ▶ my @headers_move = $_[0]->header_names;
Subroutine _transfer_headers: called CC=3 SCAR=11.0
681 4 my @headers_skip = $_[1]->header_names;
682 4 foreach my $header_name (@headers_move) {
683 20 ▶ next if _any { $_ eq $header_name } @headers_skip;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &_any(sub { $_ eq $header_name; } , @headers_skip) 8 12 20
684 12 my @values = $_[0]->header($header_name);
685 12 $_[1]->header_str_set( $header_name, @values );
686 }
687 }
688  
689 #pod =method as_string
690 #pod
691 #pod Returns the string form of the email. Identical to (and uses behind the
692 #pod scenes) Email::MIME-E<gt>as_string.
693 #pod
694 #pod =cut
695  
696 sub as_string {
697 31 ▶ shift()->email->as_string;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
698 }
699  
700 #pod =method send
701 #pod
702 #pod $stuffer->send;
703 #pod
704 #pod or
705 #pod
706 #pod $stuffer->send({ to => [ $to_1, $to_2 ], from => $sender });
707 #pod
708 #pod Sends the email via L<Email::Sender::Simple>.
709 #pod L<Envelope information|Email::Sender::Manual::QuickStart/"envelope information">
710 #pod can be specified in a hash reference.
711 #pod
712 #pod On failure, returns false.
713 #pod
714 #pod =cut
715  
716 sub send {
717 5 ▶ my $self = shift;
Subroutine send: called CC=4 SCAR=14.6
Pod documented
718 5 my $arg = shift;
719 5 ▶ my $email = $self->email or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $email = $self->email or return undef 0 5 5
720  
721 5 my $transport = $self->{transport};
722  
723 5 ▶ Email::Sender::Simple->try_to_send(
Branch: 2 branches
BranchTrueFalseTotal
$transport ? : 5 0 5
$arg ? : 0 5 5
724 $email,
725 {
726 ($transport ? (transport => $transport) : ()),
727 $arg ? %$arg : (),
728 },
729 );
730 }
731  
732 #pod =method send_or_die
733 #pod
734 #pod $stuffer->send_or_die;
735 #pod
736 #pod or
737 #pod
738 #pod $stuffer->send_or_die({ to => [ $to_1, $to_2 ], from => $sender });
739 #pod
740 #pod Sends the email via L<Email::Sender::Simple>.
741 #pod L<Envelope information|Email::Sender::Manual::QuickStart/"envelope information">
742 #pod can be specified in a hash reference.
743 #pod
744 #pod On failure, throws an exception.
745 #pod
746 #pod =cut
747  
748 sub send_or_die {
749 2 ▶ my $self = shift;
Subroutine send_or_die: called CC=4 SCAR=14.6
Pod documented
750 2 my $arg = shift;
751 2 ▶ my $email = $self->email or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $email = $self->email or return undef 0 2 2
752  
753 2 my $transport = $self->{transport};
754  
755 2 ▶ Email::Sender::Simple->send(
Branch: 2 branches
BranchTrueFalseTotal
$transport ? : 2 0 2
$arg ? : 0 2 2
756 $email,
757 {
758 ($transport ? (transport => $transport) : ()),
759 $arg ? %$arg : (),
760 },
761 );
762 }
763  
764 9 1;
765  
766 #pod =head1 TO DO
767 #pod
768 #pod =for :list
769 #pod * Fix a number of bugs still likely to exist
770 #pod * Write more tests.
771 #pod * Add any additional small bit of automation that isn't too expensive
772 #pod
773 #pod =head1 SEE ALSO
774 #pod
775 #pod L<Email::MIME>, L<Email::Sender>, L<http://ali.as/>
776 #pod
777 #pod =cut
778  
779 __END__
↑ Summary