blib/lib/Mail/Sendmail/Enhanced.pm

filter: Statementstmt 73.7% 115 / 156 Branchbran 52.7% 39 / 74 Conditioncond 43.4% 56 / 129 MC/DCmcdc 5.9% 4 / 67 Subroutinesub 100% 9 / 9 Podpod 66.6% 2 / 3 totaltotal 51.3% 225 / 438 CC 94 SCAR 56.3
CC
94
Cov
58%
send
497.2
encode_qp
36.4
new
32.8
CRAP 277.6
↑ Summary
1 package Mail::Sendmail::Enhanced;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Mail::Sendmail '0.79_16';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.03';
14  
15 ################################################################################
16  
17 sub new
18 1 ▶{
Subroutine new: called CC=20 SCAR=34.9
Pod documented
19 1 my ( $this ) = ( shift );
20  
21 1 my $mail = {};
22 1 bless ( $mail, $this );
23  
24 1 ▶ while ( my $key = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift()) 8 1 9
25 8 ▶ if ( ref ( $key ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'HASH') { } 0 8 8
26 0 foreach my $k (sort keys %{$key} ) {
27 0 $mail->{$k} = $$key{$k};
28 }
29 } else {
30 8 my $value = shift;
31 8 $mail->{$key} = $value;
32 }
33 }
34  
35 1 ▶ $mail->{smtp} ||= '';
Condition50%1/2
$mail->{"smtp"} ||= ""
l!l
10
MC/DC0%0/1
$mail->{"smtp"} ||= ""
$mail->{"smtp"}
Truth table50%1/2
A
A= $mail->{"smtp"}
Aresult
11
00
36 1 ▶ $mail->{from} ||= '';
Condition50%1/2
$mail->{"from"} ||= ""
l!l
10
MC/DC0%0/1
$mail->{"from"} ||= ""
$mail->{"from"}
Truth table50%1/2
A
A= $mail->{"from"}
Aresult
11
00
37 1 ▶ $mail->{charset} ||= 'utf-8';
Condition50%1/2
$mail->{"charset"} ||= "utf-8"
l!l
10
MC/DC0%0/1
$mail->{"charset"} ||= "utf-8"
$mail->{"charset"}
Truth table50%1/2
A
A= $mail->{"charset"}
Aresult
11
00
38 1 ▶ $mail->{type} ||= 'text/plain';
Condition50%1/2
$mail->{"type"} ||= "text/plain"
l!l
01
MC/DC0%0/1
$mail->{"type"} ||= "text/plain"
$mail->{"type"}
Truth table50%1/2
A
A= $mail->{"type"}
Aresult
11
00
39  
40 1 ▶ $mail->{user} ||= '';
Condition50%1/2
$mail->{"user"} ||= ""
l!l
10
MC/DC0%0/1
$mail->{"user"} ||= ""
$mail->{"user"}
Truth table50%1/2
A
A= $mail->{"user"}
Aresult
11
00
41 1 ▶ $mail->{pass} ||= '';
Condition50%1/2
$mail->{"pass"} ||= ""
l!l
10
MC/DC0%0/1
$mail->{"pass"} ||= ""
$mail->{"pass"}
Truth table50%1/2
A
A= $mail->{"pass"}
Aresult
11
00
42 1 ▶ $mail->{method} ||= 'LOGIN';
Condition50%1/2
$mail->{"method"} ||= "LOGIN"
l!l
10
MC/DC0%0/1
$mail->{"method"} ||= "LOGIN"
$mail->{"method"}
Truth table50%1/2
A
A= $mail->{"method"}
Aresult
11
00
43 1 ▶ $mail->{required} ||= 1;
Condition50%1/2
$mail->{"required"} ||= 1
l!l
10
MC/DC0%0/1
$mail->{"required"} ||= 1
$mail->{"required"}
Truth table50%1/2
A
A= $mail->{"required"}
Aresult
11
00
44  
45 1 ▶ $mail->{to} ||= '';
Condition50%1/2
$mail->{"to"} ||= ""
l!l
01
MC/DC0%0/1
$mail->{"to"} ||= ""
$mail->{"to"}
Truth table50%1/2
A
A= $mail->{"to"}
Aresult
11
00
46 1 ▶ $mail->{cc} ||= '';
Condition50%1/2
$mail->{"cc"} ||= ""
l!l
01
MC/DC0%0/1
$mail->{"cc"} ||= ""
$mail->{"cc"}
Truth table50%1/2
A
A= $mail->{"cc"}
Aresult
11
00
47 1 ▶ $mail->{subject} ||= 'No subject defined';
Condition50%1/2
$mail->{"subject"} ||= "No subject defined"
l!l
01
MC/DC0%0/1
$mail->{"subject"} ||= "No subject defined"
$mail->{"subject"}
Truth table50%1/2
A
A= $mail->{"subject"}
Aresult
11
00
48 1 ▶ $mail->{message} ||= 'No message defined!';
Condition50%1/2
$mail->{"message"} ||= "No message defined!"
l!l
01
MC/DC0%0/1
$mail->{"message"} ||= "No message defined!"
$mail->{"message"}
Truth table50%1/2
A
A= $mail->{"message"}
Aresult
11
00
49  
50 1 ▶ $mail->{attachments} ||= {};
Condition50%1/2
$mail->{"attachments"} ||= {}
l!l
01
MC/DC0%0/1
$mail->{"attachments"} ||= {}
$mail->{"attachments"}
Truth table50%1/2
A
A= $mail->{"attachments"}
Aresult
11
00
51 1 ▶ $mail->{attachments_size_max} ||= 0; #no limit "-1" means no attachment allowed
Condition50%1/2
$mail->{"attachments_size_max"} ||= 0
l!l
01
MC/DC0%0/1
$mail->{"attachments_size_max"} ||= 0
$mail->{"attachments_size_max"}
Truth table50%1/2
A
A= $mail->{"attachments_size_max"}
Aresult
11
00
52  
53 1 ▶ $mail->{commit} ||= 0;
Condition50%1/2
$mail->{"commit"} ||= 0
l!l
01
MC/DC0%0/1
$mail->{"commit"} ||= 0
$mail->{"commit"}
Truth table50%1/2
A
A= $mail->{"commit"}
Aresult
11
00
54  
55 1 ▶ $mail->send() if $mail->{commit};
Branch: 1 branch
BranchTrueFalseTotal
if $mail->{"commit"} 0 1 1
56  
57 1 return $mail;
58 }
59  
60 ################################################################################
61  
62 sub send
63 3 ▶{
Subroutine send: called CC=54 SCAR=62.1
Pod documented
64 3 my ( $self, $ARG ) = ( shift, shift );
65  
66 3 ▶ return 'No address! Please set [to] or/and [cc] fileds.' unless $ARG->{to} || $ARG->{cc} || $self->{to} || $self->{cc};
Branch: 1 branch
BranchTrueFalseTotal
unless $ARG->{"to"} or $ARG->{"cc"} or $self->{"to"} or $self->{"cc"} 0 3 3
Condition33%3/9
$ARG->{"to"} or $ARG->{"cc"} or $self->{"to"} or $self->{"cc"}
l!l&&r!l&&!r
300
$ARG->{"to"} or $ARG->{"cc"} or $self->{"to"}
l!l&&r!l&&!r
300
$ARG->{"to"} or $ARG->{"cc"}
l!l&&r!l&&!r
300
MC/DC0%0/4
$ARG->{"to"} or $ARG->{"cc"} or $self->{"to"} or $self->{"cc"}
$ARG->{"to"} $ARG->{"cc"} $self->{"to"} $self->{"cc"}
Truth table20%1/5
A or B or C or D
A= $ARG->{"to"}B= $ARG->{"cc"}C= $self->{"to"}D= $self->{"cc"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
67  
68 3 ▶ my $charset = $ARG->{charset} || $self->{charset} || 'utf-8';
Condition40%2/5
$ARG->{'charset'} || $self->{'charset'} || 'utf-8'
l!l
30
$ARG->{"charset"} or $self->{"charset"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$ARG->{'charset'} || $self->{'charset'} || 'utf-8'
$ARG->{"charset"} $self->{"charset"}
Truth table33%1/3
A or B
A= $ARG->{"charset"}B= $self->{"charset"}
ABresult
1X1
011
000
69 3 ▶ my $type = $ARG->{type} || $self->{type} || 'text/plain';
Condition40%2/5
$ARG->{'type'} || $self->{'type'} || 'text/plain'
l!l
30
$ARG->{"type"} or $self->{"type"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$ARG->{'type'} || $self->{'type'} || 'text/plain'
$ARG->{"type"} $self->{"type"}
Truth table33%1/3
A or B
A= $ARG->{"type"}B= $self->{"type"}
ABresult
1X1
011
000
70 3 ▶ my $subject = $ARG->{subject} || $self->{subject} || '';
Condition40%2/5
$ARG->{'subject'} || $self->{'subject'} || ''
l!l
30
$ARG->{"subject"} or $self->{"subject"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$ARG->{'subject'} || $self->{'subject'} || ''
$ARG->{"subject"} $self->{"subject"}
Truth table33%1/3
A or B
A= $ARG->{"subject"}B= $self->{"subject"}
ABresult
1X1
011
000
71 3 ▶ my $message = $ARG->{message} || $self->{message} || '';
Condition40%2/5
$ARG->{'message'} || $self->{'message'} || ''
l!l
30
$ARG->{"message"} or $self->{"message"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$ARG->{'message'} || $self->{'message'} || ''
$ARG->{"message"} $self->{"message"}
Truth table33%1/3
A or B
A= $ARG->{"message"}B= $self->{"message"}
ABresult
1X1
011
000
72  
73 3 my $boundary = "====" . time() . "====";
74  
75 # Email subject is encoded using proper character encoding.
76 # original "encode_qp" function contains up to 2 arguments,
77 # but in a case of character set it is needed to start every
78 # new line with a statemant of the encoding, so - as a the
79 # third parameter - the charset is sent to the function.
80  
81 3 my $flnoc = 67;
82 3 my $nlnoc = 78;
83 3 my $bol = " =?$charset?Q?";
84 3 my $eol = "?=\n";
85  
86 {
87 # this part consider multibyte characters and keep that folding
88 # does not divide the multibyte characters into two lines.
89 # The reason is that some email clients are not able to put
90 # together these separated bytes into one character.
91 3 require bytes;
92  
93  
94 3 ▶ my $t_subject = decode( $charset, $subject ) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 3 0 3
95  
96 3 ▶ if ( bytes::length($t_subject) > length($t_subject) || $t_subject =~ /[^\0-\xFF]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($t_subject) > length $t_subject or $t_subject =~ /[^\0-\xFF]/) { } 0 3 3
Condition33%1/3
&bytes::length($t_subject) > length $t_subject or $t_subject =~ /[^\0-\xFF]/
l!l&&r!l&&!r
003
MC/DC0%0/2
&bytes::length($t_subject) > length $t_subject or $t_subject =~ /[^\0-\xFF]/
&bytes::length($t_subject) > length $t_subject $t_subject =~ /[^\0-\xFF]/
Truth table33%1/3
A or B
A= &bytes::length($t_subject) > length $t_subjectB= $t_subject =~ /[^\0-\xFF]/
ABresult
1X1
011
000
97  
98 0 $subject = '';
99 0 my $t_string = ''; # substring of $t_subject which is testing if can be added to $subject
100 0 my $t_length = 0; # the length of $t_subject
101  
102 0 my $t_return = ''; # $t_string which match to the condition
103 0 my $t_result = ''; # encoded string of $t_string
104 0 my $t_number = 0; # number of row of the folded "Subject" field
105 0 ▶ while ( $t_subject ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t_subject) 0 0 0
106 0 foreach(0..$flnoc) {
107 0 $t_string = substr($t_subject,0,$_);
108 0 $t_result = encode_qp(encode($charset,$t_string),{bol=>$bol,eol=>$eol,flnoc=>0,nlnoc=>0,charset=>$charset,});
109  
110 #checking if encoded string $t_result of the tested substring $t_string satisfies length condition:
111 # and if yes we go out with the last good value $t_return and $t_subject get shorter by $t_length
112 0 ▶ last if length( $t_result ) > ($t_number ? $nlnoc : $flnoc);
Branch: 2 branches
BranchTrueFalseTotal
if length $t_result > ($t_number ? $nlnoc : $flnoc) 0 0 0
$t_number ? : 0 0 0
113 0 $t_return = $t_result;
114 0 $t_length = length($t_string);
115 }
116 0 $subject = $subject.$t_return;
117 0 $t_subject = substr( $t_subject, $t_length );
118 0 $t_number++;
119 }
120  
121 } else {
122 3 $subject = encode_qp( $subject , { bol=>$bol, eol=>$eol, flnoc=>$flnoc, nlnoc=>$nlnoc, charset=>$charset, } );
123 }
124 }
125  
126 3 $subject = substr($subject,1);
127  
128 my %mail = (
129 'X-Mailer' => "This is Perl Mail::Sendmail::Enhanced version $Mail::Sendmail::Enhanced::VERSION",
130 'Content-Type' => "multipart/mixed; charset=$charset; boundary=\"$boundary\"",
131 'Smtp' => ($ARG->{smpt}|| $self->{smtp} ),
132 'From' => ($ARG->{from}|| $self->{from} ),
133 'To' => ($ARG->{to} || $self->{to} || '' ),
134 'Cc' => ($ARG->{cc} || $self->{cc} || '' ),
135 'Subject' => $subject,
136 auth => {
137 user => ($ARG->{user} || $self->{user} ),
138 pass => ($ARG->{pass} || $self->{pass} ),
139 method => ($ARG->{method} || $self->{method} ),
140 3 ▶ required => ($ARG->{required}|| $self->{required} ),
Condition35%10/28
$ARG->{'smpt'} || $self->{'smtp'}
l!l&&r!l&&!r
030
$ARG->{'from'} || $self->{'from'}
l!l&&r!l&&!r
030
$ARG->{'to'} || $self->{'to'} || ''
l!l
30
$ARG->{"to"} or $self->{"to"}
l!l&&r!l&&!r
300
$ARG->{'cc'} || $self->{'cc'} || ''
l!l
03
$ARG->{"cc"} or $self->{"cc"}
l!l&&r!l&&!r
003
$ARG->{'user'} || $self->{'user'}
l!l&&r!l&&!r
030
$ARG->{'pass'} || $self->{'pass'}
l!l&&r!l&&!r
030
$ARG->{'method'} || $self->{'method'}
l!l&&r!l&&!r
030
$ARG->{'required'} || $self->{'required'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$ARG->{'smpt'} || $self->{'smtp'}
$ARG->{'smpt'} $self->{'smtp'}
MC/DC0%0/2
$ARG->{'from'} || $self->{'from'}
$ARG->{'from'} $self->{'from'}
MC/DC0%0/2
$ARG->{'to'} || $self->{'to'} || ''
$ARG->{"to"} $self->{"to"}
MC/DC0%0/2
$ARG->{'cc'} || $self->{'cc'} || ''
$ARG->{"cc"} $self->{"cc"}
MC/DC0%0/2
$ARG->{'user'} || $self->{'user'}
$ARG->{'user'} $self->{'user'}
MC/DC0%0/2
$ARG->{'pass'} || $self->{'pass'}
$ARG->{'pass'} $self->{'pass'}
MC/DC0%0/2
$ARG->{'method'} || $self->{'method'}
$ARG->{'method'} $self->{'method'}
MC/DC0%0/2
$ARG->{'required'} || $self->{'required'}
$ARG->{'required'} $self->{'required'}
Truth table33%1/3
A || B
A= $ARG->{'smpt'}B= $self->{'smtp'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $ARG->{'from'}B= $self->{'from'}
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $ARG->{"to"}B= $self->{"to"}
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $ARG->{"cc"}B= $self->{"cc"}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $ARG->{'user'}B= $self->{'user'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $ARG->{'pass'}B= $self->{'pass'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $ARG->{'method'}B= $self->{'method'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $ARG->{'required'}B= $self->{'required'}
ABresult
1X1
011
000
141 },
142 );
143  
144 3 $boundary = '--'.$boundary;
145 3 $mail{'Message'} = "$boundary\n"
146 ."Content-Type: $type; charset=$charset; format=flowed\n"
147 ."Content-Transfer-Encoding: 8bit\n\n"
148 ."$message\n";
149  
150 # ."Content-Transfer-Encoding: quoted-printable\n\n"
151 # .encode_qp( $ARG->{'message'}, {} )."\n";
152  
153 3 ▶ $ARG->{attachments} ||= $self->{attachments} || {};
Condition20%1/5
$ARG->{"attachments"} ||= $self->{"attachments"} || {}
l!l&&r!l&&!r
300
$self->{'attachments'} || {}
l!l
00
MC/DC0%0/2
$ARG->{"attachments"} ||= $self->{"attachments"} || {}
$ARG->{"attachments"} $self->{'attachments'}
Truth table33%1/3
A ||= B
A= $ARG->{"attachments"}B= $self->{'attachments'}
ABresult
1X1
011
000
154 3 ▶ $ARG->{attachments_size_max} ||= $self->{attachments_size_max} || 0; #no limit "-1" means no attachment allowed
Condition60%3/5
$ARG->{"attachments_size_max"} ||= $self->{"attachments_size_max"} || 0
l!l&&r!l&&!r
201
$self->{'attachments_size_max'} || 0
l!l
01
MC/DC50%1/2
$ARG->{"attachments_size_max"} ||= $self->{"attachments_size_max"} || 0
$ARG->{"attachments_size_max"} $self->{'attachments_size_max'}
Truth table66%2/3
A ||= B
A= $ARG->{"attachments_size_max"}B= $self->{'attachments_size_max'}
ABresult
1X1
011
000
155  
156 3 $ARG->{attachments_size_max}=~s/[B ]//g;
157 3 ▶ if ( $ARG->{attachments_size_max} =~ /^(-\d+)$|^(\d+)(|k|K|m|M|t|T)?\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($ARG->{'attachments_size_max'} =~ /^(-\d+)$|^(\d+)(|k|K|m|M|t|T)?\s*$/) { } 3 0 3
158 {
159 3 ▶ if ( $1 ) { $ARG->{attachments_size_max} = $1
Branch: 1 branch
BranchTrueFalseTotal
if ($1) { } 1 2 3
160  
161 } else {
162  
163 2 ▶ $ARG->{attachments_size_max} = $2 if $3 eq 'B' || !$3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "B" or not $3 2 0 2
Condition33%1/3
$3 eq "B" or not $3
l!l&&r!l&&!r
020
MC/DC0%0/2
$3 eq "B" or not $3
$3 eq "B" not $3
Truth table33%1/3
A or not(B)
A= $3 eq "B"B= not $3
ABresult
1X1
011
000
164 2 ▶ $ARG->{attachments_size_max} = 1000 * $2 if $3 eq 'k';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "k" 0 2 2
165 2 ▶ $ARG->{attachments_size_max} = 1024 * $2 if $3 eq 'K';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "K" 0 2 2
166 2 ▶ $ARG->{attachments_size_max} = 1000 * 1000 * $2 if $3 eq 'm';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "m" 0 2 2
167 2 ▶ $ARG->{attachments_size_max} = 1024 * 1024 * $2 if $3 eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "M" 0 2 2
168 2 ▶ $ARG->{attachments_size_max} = 1000 * 1000 * 1000 * $2 if $3 eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "t" 0 2 2
169 2 ▶ $ARG->{attachments_size_max} = 1024 * 1024 * 1024 * $2 if $3 eq 'T';
Branch: 1 branch
BranchTrueFalseTotal
if $3 eq "T" 0 2 2
170 }
171 }
172 else {
173 0 return 'Malform in attachments_size_max='.$ARG->{attachments_size_max}.'! Accepted form is: positive or negative integer plus optional one of the letters: (k,K,m,M,t,T).'
174 }
175  
176 3 ▶ return "Attachments are not allowed whereas some are preperad to send!" if %{$ARG->{attachments}} && $ARG->{attachments_size_max} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if %{$ARG->{"attachments"};} and $ARG->{"attachments_size_max"} < 0 1 2 3
Condition66%2/3
%{$ARG->{"attachments"};} and $ARG->{"attachments_size_max"} < 0
!ll&&!rl&&r
021
MC/DC50%1/2
%{$ARG->{"attachments"};} and $ARG->{"attachments_size_max"} < 0
%{$ARG->{"attachments"};} $ARG->{"attachments_size_max"} < 0
Truth table66%2/3
A and B
A= %{$ARG->{"attachments"};}B= $ARG->{"attachments_size_max"} < 0
ABresult
0X0
100
111
177 # attachment files are packed one by one into the message part each divided by boundary
178  
179 # checking attachments:
180 2 foreach my $fileName ( sort keys %{$ARG->{attachments}} ) {
181  
182 2 my $fileLocation = $ARG->{attachments}->{$fileName};
183  
184 # if does not exists:
185 2 ▶ return "Attachment does not exists! [$fileLocation]" unless -f $fileLocation;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fileLocation 1 1 2
186  
187 # if it is too big:
188 1 ▶ my $size = -s $fileLocation || 0;
Condition50%1/2
-s $fileLocation || 0
l!l
10
MC/DC0%0/1
-s $fileLocation || 0
-s $fileLocation
Truth table50%1/2
A
A= -s $fileLocation
Aresult
11
00
189 return "Attachment too big! [$fileLocation: $size > ".$ARG->{attachments_size_max}."B max.]"
190 1 ▶ if $ARG->{attachments_size_max} > 0 && $size > $ARG->{attachments_size_max};
Branch: 1 branch
BranchTrueFalseTotal
if $ARG->{"attachments_size_max"} > 0 and $size > $ARG->{"attachments_size_max"} 1 0 1
Condition33%1/3
$ARG->{"attachments_size_max"} > 0 and $size > $ARG->{"attachments_size_max"}
!ll&&!rl&&r
001
MC/DC0%0/2
$ARG->{"attachments_size_max"} > 0 and $size > $ARG->{"attachments_size_max"}
$ARG->{"attachments_size_max"} > 0 $size > $ARG->{"attachments_size_max"}
Truth table33%1/3
A and B
A= $ARG->{"attachments_size_max"} > 0B= $size > $ARG->{"attachments_size_max"}
ABresult
0X0
100
111
191 }
192  
193  
194 0 foreach my $fileName ( sort keys %{$ARG->{attachments}} ) {
195 0 my $fileLocation = $ARG->{attachments}->{$fileName};
196 0 ▶ if (open (my $F, $fileLocation )) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $F, $fileLocation) 0 0 0
197 0 my $input_record_separator = $/;
198 0 binmode $F; undef $/;
199 0 my $attachment = encode_base64(<$F>);
200 0 close $F;
201 0 $/ = $input_record_separator;
202  
203 0 $mail{'Message'} .= "$boundary\n"
204 ."Content-Type: application/octet-stream; name=\"$fileName\"\n"
205 ."Content-ID: <$fileName>\n"
206 ."Content-Transfer-Encoding: base64\n"
207 ."Content-Disposition: attachment; filename=\"$fileName\"\n\n"
208 ."$attachment\n";
209 }
210 }
211  
212 0 $mail{'Message'} .= "$boundary--\n";
213  
214  
215 0 ▶ return $Mail::Sendmail::error unless sendmail( %mail );
Branch: 1 branch
BranchTrueFalseTotal
unless sendmail(%mail) 0 0 0
216  
217 0 return;
218 }
219  
220 ################################################################################
221  
222 sub encode_qp
223 4 ▶{
Subroutine encode_qp: called CC=22 SCAR=35.9
Pod undocumented
224 ############################################################################
225 # This function is an exact copy of the that of the same name from the
226 # module: "MIME::QuotedPrint::Perl" '1.00' with the following changes:
227 # 1. The second argument can be scalar - as previously -
228 # or hash which would contain more information
229 # 2. There are changes in counting character in each line in accordance
230 # with hash sent to the function: it can be different in first line
231 # and the next ones. It is so, because usually in the firs line there
232 # is some word (Subject for instance).
233 # The behaviour of the function is identical with the original one in case
234 # we send two scalar arguments only.
235 ############################################################################
236  
237 # $res = text to be encoded
238 4 my ( $res ) = ( shift );
239 4 ▶ return '' unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 4 4
240  
241 # The arguments can be sent in old way, when the second argument was the
242 # end of character rows, or in a new way - as a hash:
243 4 my %par = (
244 bol => " ", # characters at the begining of each lines
245 eol => "\n", # characters at the end of each line
246 flnoc => 68, # first line number of characters, 0 = unlimit
247 nlnoc => 78, # next lines number of characters, 0 = unlimit
248 );
249  
250 4 ▶ while ( my $key = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift()) 3 4 7
251 3 ▶ if ( ref ( $key ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'HASH') { } 3 0 3
252 3 foreach my $k (sort keys %{$key} ) {
253 15 ▶ next unless $k =~ /^(bol|charset|eol|flnoc|nlnoc)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(bol|charset|eol|flnoc|nlnoc)$/ 0 15 15
254 15 ▶ next if $k eq 'flnoc' && $par{$k} !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "flnoc" and not $par{$k} =~ /^\d+$/ 0 15 15
Condition66%2/3
$k eq "flnoc" and not $par{$k} =~ /^\d+$/
!ll&&!rl&&r
1230
MC/DC0%0/2
$k eq "flnoc" and not $par{$k} =~ /^\d+$/
$k eq "flnoc" not $par{$k} =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= $k eq "flnoc"B= not $par{$k} =~ /^\d+$/
ABresult
0X0
100
111
255 15 ▶ next if $k eq 'nlnoc' && $par{$k} !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "nlnoc" and not $par{$k} =~ /^\d+$/ 0 15 15
Condition66%2/3
$k eq "nlnoc" and not $par{$k} =~ /^\d+$/
!ll&&!rl&&r
1230
MC/DC0%0/2
$k eq "nlnoc" and not $par{$k} =~ /^\d+$/
$k eq "nlnoc" not $par{$k} =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= $k eq "nlnoc"B= not $par{$k} =~ /^\d+$/
ABresult
0X0
100
111
256  
257 15 $par{$k} = $$key{$k};
258 }
259 } else { # you can only send - as a second scalar argument the "EOL"
260 # characters in accordance with the original function
261 0 $par{eol} = $key;
262 }
263 }
264  
265 4 ▶ if ($] >= 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.006") 4 0 4
266 4 require bytes;
267 4 ▶ if (bytes::length($res) > length($res) || ($] >= 5.008 && $res =~ /[^\0-\xFF]/))
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($res) > length $res or $] >= "5.008" and $res =~ /[^\0-\xFF]/) 0 4 4
Condition33%2/6
&bytes::length($res) > length $res or $] >= "5.008" and $res =~ /[^\0-\xFF]/
l!l&&r!l&&!r
004
$] >= "5.008" and $res =~ /[^\0-\xFF]/
!ll&&!rl&&r
040
MC/DC0%0/3
&bytes::length($res) > length $res or $] >= "5.008" and $res =~ /[^\0-\xFF]/
&bytes::length($res) > length $res $] >= "5.008" $res =~ /[^\0-\xFF]/
Truth table25%1/4
A or B and C
A= &bytes::length($res) > length $resB= $] >= "5.008"C= $res =~ /[^\0-\xFF]/
ABCresult
1XX1
0111
00X0
0100
268 {
269 0 require Carp;
270 0 Carp::croak("The Quoted-Printable encoding is only defined for bytes");
271 }
272 }
273  
274 # usefull shorthands
275 4 my $bol = $par{bol};
276 4 my $eol = $par{eol};
277 4 my $flnoc = $par{flnoc} - 0 - length($eol) - length($bol);
278 4 my $nlnoc = $par{nlnoc} - 1 - length($eol) - length($bol);
279 4 my $mid = '';
280 0 ▶ unless ( defined $bol ) { $mid = '='; $bol = '' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bol) 0 4 4
281  
282 # Do not mention ranges such as $res =~ s/([^ \t\n!-<>-~])/sprintf("=%02X", ord($1))/eg;
283 # since that will not even compile on an EBCDIC machine (where ord('!') > ord('<')).
284 4 if (ord('A') == 193) { # EBCDIC style machine
285 if (ord('[') == 173) {
286 $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp1047',$1))))/eg; # rule #2,#3
287 $res =~ s/([ \t]+)$/
288 join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp1047',$_)))) }
289 split('', $1)
290 )/egm; # rule #3 (encode whitespace at eol)
291 }
292 elsif (ord('[') == 187) {
293 $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('posix-bc',$1))))/eg; # rule #2,#3
294 $res =~ s/([ \t]+)$/
295 join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('posix-bc',$_)))) }
296 split('', $1)
297 )/egm; # rule #3 (encode whitespace at eol)
298 }
299 elsif (ord('[') == 186) {
300 $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp37',$1))))/eg; # rule #2,#3
301 $res =~ s/([ \t]+)$/
302 join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp37',$_)))) }
303 split('', $1)
304 )/egm; # rule #3 (encode whitespace at eol)
305 }
306 }
307 else { # ASCII style machine
308 4 $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord($1))/eg; # rule #2,#3
309 4 ▶ $res =~ s/\n/=0A/g unless length($eol);
Branch: 1 branch
BranchTrueFalseTotal
unless length $eol 0 4 4
310 4 $res =~ s/([ \t]+)$/
311 0 join('', map { sprintf("=%02X", ord($_)) }
312 split('', $1)
313 )/egm; # rule #3 (encode whitespace at eol)
314 }
315  
316 4 ▶ return $res unless length($eol);
Branch: 1 branch
BranchTrueFalseTotal
unless length $eol 0 4 4
317  
318 # rule #5 (lines must be shorter than 76 chars, but we are not allowed
319 # to break =XX escapes. This makes things complicated :-( )
320 4 my $brokenlines = "";
321  
322 4 ▶ $brokenlines .= "$bol$1$mid$eol" if $flnoc && $res =~ s/(.*?^[^\n]{$flnoc} (?:
Branch: 1 branch
BranchTrueFalseTotal
if $flnoc and $res =~ s/(.*?^[^\n]{$flnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx 1 3 4
Condition66%2/3
$flnoc and $res =~ s/(.*?^[^\n]{$flnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
!ll&&!rl&&r
031
MC/DC50%1/2
$flnoc and $res =~ s/(.*?^[^\n]{$flnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
$flnoc $res =~ s/(.*?^[^\n]{$flnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
Truth table66%2/3
A and B
A= $flnocB= $res =~ s/(.*?^[^\n]{$flnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
ABresult
0X0
100
111
323 [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n
324 |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n
325 | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n
326 ))//xsm;
327  
328 4 ▶ $brokenlines .= "$bol$1$mid$eol" while $nlnoc && $res =~ s/(.*?^[^\n]{$nlnoc} (?:
Branch: 1 branch
BranchTrueFalseTotal
$nlnoc and $res =~ s/(.*?^[^\n]{$nlnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx and do { $brokenlines .= "$bol$1$mid$eol" } 4 4 8
Condition66%2/3
$nlnoc and $res =~ s/(.*?^[^\n]{$nlnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
!ll&&!rl&&r
044
MC/DC50%1/2
$nlnoc and $res =~ s/(.*?^[^\n]{$nlnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
$nlnoc $res =~ s/(.*?^[^\n]{$nlnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
Truth table66%2/3
A and B
A= $nlnocB= $res =~ s/(.*?^[^\n]{$nlnoc} (?: [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n ))//msx
ABresult
0X0
100
111
329 [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n
330 |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n
331 | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n
332 ))//xsm;
333  
334 4 ▶ $brokenlines .= "$bol$res$eol" if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 4 0 4
335  
336 #print "$brokenlines\n";
337 4 $brokenlines;
338 }
339  
340 ################################################################################
341  
342 2 1;
343  
344 ################################################################################
345  
346 =pod
347  
348 =head1 NAME
349  
350 Mail::Sendmail::Enhanced v.0.03 - Pure Perl email sender with multibyte characters encoding and easy attachments managment
351  
352 =head1 SYNOPSIS
353  
354 #!/usr/bin/perl -w
355  
356 use strict;
357 use warnings;
358  
359 use Mail::Sendmail::Enhanced;
360  
361 # This part simulate the general setup of application mailer.
362 # It sets smtp server and size limit of attachments (1MB)
363 # This configuration is set by admin.
364 my $mail = Mail::Sendmail::Enhanced-> new(
365 charset => 'cp1250',
366 smtp => 'Your SMTP server',
367 from => 'Your mail',
368 user => 'user',
369 pass => 'password',
370 method => 'LOGIN',
371 required => 1,
372 attachments => {
373 'name for email of the file1' => 'OS file1 location',
374 'name for email of the file2' => 'OS file2 location',
375 },
376 attachments_size_max => '1MB',
377 commit => 0,
378 );
379  
380 # This part simulate how clients can use the mailer.
381 # Configuration here is set by clients themself.
382 my @client = qw(John Henry Newman);
383 for (@client) {
384  
385 my $lowercase = chr(185).chr(230).chr(234).chr(179).chr(241).chr(243).chr(156).chr(159).chr(191);
386 my $uppercase = chr(165).chr(198).chr(202).chr(163).chr(209).chr(211).chr(140).chr(143).chr(175);
387  
388 print $mail-> send( {
389 to => 'author of the module: <wb@webswing.co.uk>',
390 subject => "Subject longer than 80 characters with Polish letters: lowercase: $lowercase and uppercase: $uppercase.",
391 message => "This is the message from $_ in the character encoding ".$mail->{charset}.".
392  
393 This is an example of mailing Polish letters in a header field named \"Subject\".
394 Additionally this field is longer than 80 characters.
395  
396 Additional text:
397 Polish lowercase letters: $lowercase
398 Polish uppercase letters: $uppercase
399 ",
400 });
401 }
402  
403 __END__
404  
405 =head1 DESCRIPTION
406  
407 Mail::Sendmail::Enhanced is an enhanced version of the module
408 L<Mail::Sendmail>. It is still pure Perl solution. In the module the
409 problem of encoding multibyte characters in L<Mail::Sendmail> was
410 solved. Some procedure of sending very easily a list of attachments
411 was prepared.
412  
413 After preparing multibyte characters encoding and building message
414 with attachments the module calls I<sendmail> function from the
415 L<Mail::Sendmail> module which does all the job.So please read there
416 in L<Mail::Sendmail> about how to set up connections to email servers.
417 This module behaves identically.
418  
419 As already mentioned this adds two things:
420  
421 1. Multibyte characters encoding - which uses refurbish and imported
422 function I<encode_qp> from the module L<MIME::QuotedPrint::Perl>.
423  
424 The problem with encoding multibyte characters was that simple
425 implemented encoding - especially in the "Subject:" field of email
426 header - results that some characters are divided between two rows
427 when long lines are folded. Some email clients are not able to put
428 together these separated bytes into one character and letters are
429 displeyed inproperly. The new encoding function keeps bytes of one
430 character in one folded row.
431  
432 2. Simple attachments managment. List of attachments is a hash:
433  
434 attachments => {
435 'name for email of the file1' => 'OS file1 location',
436 'name for email of the file2' => 'OS file2 location',
437 },
438  
439 where the keys are the attachments email names and the values are
440 OS locations.
441  
442 It is possible to add some control to sending attachment. It is done
443 by the parameter B<attachments_size_max>. Possible values are:
444  
445  
446 attachments_size_max => -1, # Negative value means that sending attachments is forbidden.
447 # Every try of sending them with this value negative is fatal one.
448  
449 attachments_size_max => 0, # No size limit of attachments
450  
451 attachments_size_max => '50 000 B', # Positive value is a maximum size of attachment.
452 # When size is bigger then fatal error is return.
453 # Spaces and the letter B (byte) are ignored.
454  
455 # shorthand for sizes: k, K, m, M:
456 attachments_size_max => '100k', # k = 1000, so maximum = 100 000
457 attachments_size_max => '100 K', # K = 1024, so maximum = 102 400
458 attachments_size_max => '2 m', # m = 1000x1000, so maximum = 1 000 000
459 attachments_size_max => '2M', # M = 1024x1024, so maximum = 1 048 576
460  
461  
462 =head1 INTERFACE
463  
464 Interface L<Mail::Sendmail::Enhanced>, gets two methods:
465  
466 =head2 new()
467  
468 The method I<new> creates mail object.
469  
470 =head2 send()
471  
472 The method I<send> sends mail.
473  
474 Arguments of both methods are the same and discussed earlier.
475 Dispersing data between I<new> and I<send> is fully optional.
476 Assuming that we have three hashes %n, %s and %d which fullfiled the
477 abstract equality:
478  
479 "%n + %s = %d"
480  
481 all the three ways of sending email have the same effect:
482  
483 1. my $mail = Mail::Sendmail::Enhanced->new(%n); $mail->send(%s);
484  
485 2. my $mail = Mail::Sendmail::Enhanced->new(); $mail->send(%d);
486  
487 3. my $mail = Mail::Sendmail::Enhanced->new(%d); $mail->send();
488  
489 This third way can be replaced by only one call with additional argument
490 "commit" with the value 1 (look back at the SYNOPSIS):
491  
492 commit => 1,
493  
494 in that case email is sent at the end of the method I<new>.
495  
496  
497 =head1 BUGS
498  
499 Please report any bugs or feature requests to C<bug-mail-sendmail-enhanced at rt.cpan.org>, or through
500 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mail-Sendmail-Enhanced>. I will
501 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
502  
503 =head1 LICENSE AND COPYRIGHT
504  
505 Copyright (C) 2015 Waldemar Biernacki, C<< <wb at webswing.co.uk> >>
506  
507 This program is free software; you can redistribute it and/or modify
508 it under the terms of the the Artistic License (2.0). You may obtain
509 a copy of the full license at:
510  
511 L<http://www.perlfoundation.org/artistic_license_2_0>
512  
513 Any use, modification, and distribution of the Standard or Modified
514 Versions is governed by this Artistic License. By using, modifying
515 or distributing the Package, you accept this license. Do not use,
516 modify, or distribute the Package, if you do not accept this license.
517  
518 If your Modified Version has been derived from a Modified Version
519 made by someone other than you, you are nevertheless required to
520 ensure that your Modified Version complies with the requirements of
521 this license.
522  
523 This license does not grant you the right to use any trademark,
524 service mark, tradename, or logo of the Copyright Holder.
525  
526 This license includes the non-exclusive, worldwide, free-of-charge
527 patent license to make, have made, use, offer to sell, sell, import
528 and otherwise transfer the Package with respect to any patent claims
529 licensable by the Copyright Holder that are necessarily infringed by
530 the Package. If you institute patent litigation (including a
531 cross-claim or counterclaim) against any party alleging that the
532 Package constitutes direct or contributory patent infringement, then
533 this Artistic License to you shall terminate on the date that such
534 litigation is filed.
535  
536 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
537 HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
538 WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
539 A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE
540 EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO
541 COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
542 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY
543 OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF
544 SUCH DAMAGE.
545  
546 =head1 SEE ALSO
547  
548 L<Mail::Sendmail>, L<MIME::QuotedPrint::Perl>
549  
550 =cut
↑ Summary