blib/lib/Email/Mailer.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 84.6% 22 / 26 Conditioncond 69.3% 34 / 49 MC/DCmcdc 29.6% 8 / 27 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 79.5% 156 / 196 CC 35 SCAR 32.9
CC
35
Cov
86%
send
38.5
new
4.0
BEGIN
1.0
CRAP 26.8
↑ Summary
1 package Email::Mailer;
2 # ABSTRACT: Multi-purpose emailer for HTML, auto-text, attachments, and templates
3  
4 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use exact -noautoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Email::MessageID;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Email::MIME 1.940;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Email::MIME::CreateHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Email::Sender::Simple 'sendmail';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTML::FormatText;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use IO::All 'io';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use MIME::Words 'encode_mimewords';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Encode qw( encode is_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = '1.24'; # VERSION
18  
19 38 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
20 38 my $self = shift;
21  
22 38 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 18 20 38
23 # $self is not an object, is incoming pair values = make $self object
24 20 $self = bless( {@_}, $self );
25 }
26 else {
27 # $self is an object = make a new $self object incorporating any new values
28 18 $self = bless( { %$self, @_ }, ref $self );
29 }
30  
31 # for a certain set of keys, ensure they are all lower-case
32 $self->{ lc $_ } = delete $self->{$_}
33 38 ▶ for ( grep { /^(?:to|from|subject|html|text)$/i and /[A-Z]/ } keys %$self );
Condition66%2/3
/^(?:to|from|subject|html|text)$/iu && /[A-Z]/u
!ll&&!rl&&r
25810
MC/DC0%0/2
/^(?:to|from|subject|html|text)$/iu && /[A-Z]/u
/^(?:to|from|subject|html|text)$/iu /[A-Z]/u
Truth table66%2/3
A && B
A= /^(?:to|from|subject|html|text)$/iuB= /[A-Z]/u
ABresult
0X0
100
111
34  
35 38 return $self;
36 }
37  
38 20 ▶sub send {
Subroutine send: called CC=32 SCAR=36.5
Pod documented
39 20 my $self = shift;
40  
41 # if @_ is a set of hashrefs, map them into new mail objects; otherwise, just merge in new values;
42 # then iterate through the objects inside the map
43 my @mails = map {
44 # make a clean copy of the data so we can return the mail object unchanged at the end
45 21 my $mail = {%$_};
46  
47 # process any template functionality (look for values that are scalarrefs)
48 21 ▶ if ( ref $mail->{process} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mail->{"process"} eq "CODE") 1 20 21
49 2 ▶ $mail->{$_} = $mail->{process}->( ${ $mail->{$_} }, $mail->{data} || {} )
Condition50%1/2
$mail->{'data'} || {}
l!l
20
MC/DC0%0/1
$mail->{'data'} || {}
$mail->{'data'}
Truth table50%1/2
A
A= $mail->{'data'}
Aresult
11
00
50 1 for ( grep { ref $mail->{$_} eq 'SCALAR' } keys %$mail );
51 }
52  
53 # automatically create the text version from HTML if there is no text version and there is HTML
54 21 ▶ if ( $mail->{html} and not $mail->{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail->{"html"} and not $mail->{"text"}) 10 11 21
Condition100%3/3
$mail->{"html"} and not $mail->{"text"}
!ll&&!rl&&r
5610
MC/DC100%2/2
$mail->{"html"} and not $mail->{"text"}
$mail->{"html"} not $mail->{"text"}
Truth table100%3/3
A and not(B)
A= $mail->{"html"}B= not $mail->{"text"}
ABresult
0X0
100
111
55 10 ▶ my $width = $mail->{width} // 72;
Condition100%2/2
$mail->{"width"} // 72
l!l
55
MC/DC100%1/1
$mail->{"width"} // 72
$mail->{"width"}
Truth table100%2/2
A
A= $mail->{"width"}
Aresult
11
00
56 10 ▶ $width ||= 1_000_000;
Condition100%2/2
$width ||= 1000000
l!l
55
MC/DC100%1/1
$width ||= 1000000
$width
Truth table100%2/2
A
A= $width
Aresult
11
00
57  
58 $mail->{text} = HTML::FormatText
59 ->new( leftmargin => 0, rightmargin => $width )
60 10 ->format( HTML::TreeBuilder->new->parse( $mail->{html} . "\n" ) );
61 }
62  
63 21 ▶ $mail->{text} = encode( 'UTF-8', $mail->{text} ) if is_utf8( $mail->{text} );
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($mail->{"text"}) 6 15 21
64  
65 21 my @keys = keys %$mail;
66 21 for my $name ( qw( to from subject ) ) {
67 63 my ($key) = grep { lc($_) eq $name } @keys;
68 $mail->{$key} = encode_mimewords( $mail->{$key}, Charset => 'UTF-8' )
69 63 ▶ if ( $key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/u 0 63 63
Condition66%4/6
$key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/u
!ll&&!rl&&r
36270
$key and defined $mail->{$key}
!ll&&!rl&&r
36027
MC/DC0%0/3
$key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/u
$key defined $mail->{$key} $mail->{$key} =~ /[^[:ascii:]]/u
Truth table50%2/4
A and B and C
A= $keyB= defined $mail->{$key}C= $mail->{$key} =~ /[^[:ascii:]]/u
ABCresult
0XX0
10X0
1100
1111
70 }
71  
72 21 ▶ $mail->{'Message-Id'} //= Email::MessageID->new->in_brackets;
Condition33%1/3
$mail->{"Message-Id"} //= "Email::MessageID"->new->in_brackets
l!l&&r!l&&!r
0210
MC/DC0%0/2
$mail->{"Message-Id"} //= "Email::MessageID"->new->in_brackets
$mail->{"Message-Id"} "Email::MessageID"->new->in_brackets
Truth table33%1/3
A //= B
A= $mail->{"Message-Id"}B= "Email::MessageID"->new->in_brackets
ABresult
1X1
011
000
73  
74 # create a headers hashref (delete things from a data copy that known to not be headers)
75 my $headers = [
76 map {
77 0 ▶ $mail->{$_} = join( ',', @{ $mail->{$_} } ) if ( ref $mail->{$_} eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $mail->{$_} eq "ARRAY" 0 48 48
78 0 ▶ $mail->{$_} = join( ',', values %{ $mail->{$_} } ) if ( ref $mail->{$_} eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $mail->{$_} eq "HASH" 0 48 48
79 48 ucfirst($_) => $mail->{$_};
80 }
81 21 grep { not /^(?:html|text|embed|attachments|process|data|transport|width)$/i }
82 sort keys %$mail
83 ];
84  
85 # build up an attachments arrayref of attachment MIME objects
86 my $attachments = ( not $mail->{attachments} or ref $mail->{attachments} ne 'ARRAY' ) ? [] : [
87 map {
88 Email::MIME->create(
89 attributes => {
90 disposition => 'attachment',
91 content_type => $_->{ctype} || 'application/octet-stream',
92 encoding => $_->{encoding} // 'base64',
93 filename => $_->{name} || $_->{filename} || $_->{source},
94 name => $_->{name} || $_->{filename} || $_->{source},
95 },
96 8 ▶ body => ( ( $_->{content} ) ? $_->{content} : io( $_->{source} )->binary->all ),
Branch: 1 branch
BranchTrueFalseTotal
$_->{'content'} ? : 7 1 8
Condition62%10/16
$_->{'ctype'} || 'application/octet-stream'
l!l
80
$_->{"encoding"} // "base64"
l!l
80
$_->{'name'} || $_->{'filename'} || $_->{'source'}
l!l&&r!l&&!r
710
$_->{"name"} or $_->{"filename"}
l!l&&r!l&&!r
701
$_->{'name'} || $_->{'filename'} || $_->{'source'}
l!l&&r!l&&!r
710
$_->{"name"} or $_->{"filename"}
l!l&&r!l&&!r
701
MC/DC0%0/1
$_->{'ctype'} || 'application/octet-stream'
$_->{'ctype'}
MC/DC0%0/1
$_->{"encoding"} // "base64"
$_->{"encoding"}
MC/DC0%0/3
$_->{'name'} || $_->{'filename'} || $_->{'source'}
$_->{"name"} $_->{"filename"} $_->{'source'}
MC/DC0%0/3
$_->{'name'} || $_->{'filename'} || $_->{'source'}
$_->{"name"} $_->{"filename"} $_->{'source'}
Truth table50%1/2
A
A= $_->{'ctype'}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"encoding"}
Aresult
11
00
Truth table50%2/4
A or B || C
A= $_->{"name"}B= $_->{"filename"}C= $_->{'source'}
ABCresult
1XX1
01X1
0011
0000
Truth table50%2/4
A or B || C
A= $_->{"name"}B= $_->{"filename"}C= $_->{'source'}
ABCresult
1XX1
01X1
0011
0000
97 ),
98 21 ▶ } @{ $mail->{attachments} }
Branch: 1 branch
BranchTrueFalseTotal
!$mail->{'attachments'} || ref $mail->{'attachments'} ne 'ARRAY' ? : 14 7 21
Condition66%2/3
not $mail->{"attachments"} or ref $mail->{"attachments"} ne "ARRAY"
l!l&&r!l&&!r
1407
MC/DC50%1/2
not $mail->{"attachments"} or ref $mail->{"attachments"} ne "ARRAY"
not $mail->{"attachments"} ref $mail->{"attachments"} ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not $mail->{"attachments"}B= ref $mail->{"attachments"} ne "ARRAY"
ABresult
1X1
011
000
99 ];
100  
101 # build a single MIME email object to send based on what data we have for the email
102 21 my $email_mime;
103 21 ▶ if ( $mail->{text} and not $mail->{html} and @$attachments == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mail->{'text'} and not $mail->{'html'} and @$attachments == 0) { } 3 18 21
elsif ($mail->{'text'} and not $mail->{'html'}) { } 2 16 18
Condition83%5/6
$mail->{"text"} and not $mail->{"html"} and @$attachments == 0
!ll&&!rl&&r
1623
$mail->{"text"} and not $mail->{"html"}
!ll&&!rl&&r
0165
MC/DC66%2/3
$mail->{"text"} and not $mail->{"html"} and @$attachments == 0
$mail->{"text"} not $mail->{"html"} @$attachments == 0
Truth table75%3/4
A and not(B) and C
A= $mail->{"text"}B= not $mail->{"html"}C= @$attachments == 0
ABCresult
0XX0
10X0
1100
1111
104 $email_mime = Email::MIME->create(
105 header_str => $headers,
106 body => $mail->{text},
107 3 attributes => {
108 charset => 'UTF-8',
109 encoding => 'quoted-printable',
110 },
111 );
112 }
113 ▶ elsif ( $mail->{text} and not $mail->{html} ) {
Condition66%2/3
$mail->{"text"} and not $mail->{"html"}
!ll&&!rl&&r
0162
MC/DC50%1/2
$mail->{"text"} and not $mail->{"html"}
$mail->{"text"} not $mail->{"html"}
Truth table66%2/3
A and not(B)
A= $mail->{"text"}B= not $mail->{"html"}
ABresult
0X0
100
111
114 $email_mime = Email::MIME->create(
115 header_str => $headers,
116 attributes => { content_type => 'multipart/mixed' },
117 parts => [
118 Email::MIME->create(
119 header_str => [],
120 body => $mail->{text},
121 2 attributes => {
122 charset => 'UTF-8',
123 encoding => 'quoted-printable',
124 },
125 ),
126 @$attachments,
127 ],
128 );
129 }
130 else {
131 my $html_email = Email::MIME->create_html(
132 header => [],
133 body => $mail->{html},
134 text_body => $mail->{text},
135 embed => $mail->{embed},
136 16 text_body_attributes => {
137 charset => 'UTF-8',
138 encoding => 'quoted-printable',
139 },
140 );
141  
142 16 $email_mime = Email::MIME->create(
143 header_str => $headers,
144 attributes => { content_type => 'multipart/mixed' },
145 parts => [ $html_email, @$attachments ],
146 );
147 }
148  
149 # send the email with Email::Sender::Simple
150 21 sendmail( $email_mime, { transport => $mail->{transport} } );
151  
152 21 ▶ $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 19 20
153 20 } ( ref $_[0] eq 'HASH' ) ? ( map { $self->new(%$_) } @_ ) : $self->new(@_);
154  
155 # return the mail objects as desired by the caller
156 20 ▶ return ( wantarray() ) ? (@mails) : \@mails;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 20 20
157 }
158  
159 3 1;
160  
161 __END__
↑ Summary