lib/Mail/Make/Entity.pm

filter: Statementstmt 72.0% 302 / 419 Branchbran 50.0% 130 / 260 Conditioncond 57.2% 87 / 152 MC/DCmcdc 26.5% 22 / 83 Subroutinesub 73.0% 38 / 52 Podpod 93.7% 30 / 32 totaltotal 61.0% 609 / 998 CC 197 SCAR 42.3
CC
197
Cov
62%
length
211.5
build
137.2
print_body
70.9
CRAP 68.5
« lib/Mail/Make/GPG.pm ↑ Summary lib/Mail/Make/Stream.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/Entity.pm
3 ## Version v0.4.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/02
7 ## Modified 2026/03/18
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Mail::Make::Entity;
15 BEGIN
16 {
17 7 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶ warnings::register_categories( 'Mail::Make' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶ use vars qw( $VERSION $EXCEPTION_CLASS $CRLF );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶ use Mail::Make::Body::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶ use Mail::Make::Body::InCore;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶ use Mail::Make::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶ use Mail::Make::Headers::ContentDisposition;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶ use Mail::Make::Headers::ContentTransferEncoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶ use Mail::Make::Headers::ContentType;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶ use Mail::Make::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶ use Mail::Make::Stream::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶ use Mail::Make::Stream::QuotedPrint;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 our $CRLF = "\015\012";
34 7 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
35 7 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
36 7 our $VERSION = 'v0.4.1';
37 }
38  
39 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 sub init
43 {
44 218 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
45 218 $self->{body} = undef; # Mail::Make::Body object
46 218 $self->{effective_type} = undef; # cached mime type string
47 218 $self->{epilogue} = [];
48 218 $self->{headers} = undef; # Mail::Make::Headers object
49 218 $self->{is_encoded} = 0;
50 218 $self->{preamble} = [];
51 218 $self->{_parts} = []; # [ Mail::Make::Entity, ... ]
52 218 $self->{_exception_class} = $EXCEPTION_CLASS;
53 218 $self->{_init_strict_use_sub} = 1;
54 218 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 218 218
55 218 return( $self );
56 }
57  
58 # add_part( $entity )
59 # Appends a Mail::Make::Entity as a sub-part of this entity.
60 sub add_part
61 {
62 43 ▶ my( $self, $part ) = @_;
Subroutine add_part: called CC=2 SCAR=6.9
Pod documented
63 43 ▶ unless( $self->_is_a( $part => 'Mail::Make::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($part, "Mail::Make::Entity")) 1 42 43
64 {
65 1 return( $self->error( "add_part: argument must be a Mail::Make::Entity" ) );
66 }
67 42 push( @{$self->{_parts}}, $part );
68 42 return( $self );
69 }
70  
71 # as_string()
72 # Returns the serialised entity (headers + blank line + body) as a plain string.
73 sub as_string
74 {
75 36 ▶ my $self = shift( @_ );
Subroutine as_string: called CC=3 SCAR=11.2
Pod documented
76 36 my $out = '';
77 36 ▶ open( my $fh, '>:raw', \$out ) ||
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", \$out or return $self->error("Cannot open in-memory buffer for as_string: $!") 0 36 36
78 return( $self->error( "Cannot open in-memory buffer for as_string: $!" ) );
79 36 ▶ $self->print( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print($fh) or return $self->pass_error 0 36 36
80 36 close( $fh );
81 36 return( $out );
82 }
83  
84 # Returns the serialised entity as a scalar reference, avoiding a string copy.
85 # Useful for large messages where the caller can pass the ref directly to
86 # print() or write() without materialising a second copy.
87 sub as_string_ref
88 {
89 1 ▶ my $self = shift( @_ );
Subroutine as_string_ref: called CC=3 SCAR=11.2
Pod documented
90 1 my $out = '';
91 1 ▶ open( my $fh, '>:raw', \$out ) ||
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", \$out or return $self->error("Cannot open in-memory buffer for as_string_ref: $!") 0 1 1
92 return( $self->error( "Cannot open in-memory buffer for as_string_ref: $!" ) );
93 1 ▶ $self->print( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print($fh) or return $self->pass_error 0 1 1
94 1 close( $fh );
95 1 return( \$out );
96 }
97  
98 # body( [$body_object] )
99 # Gets or sets the Mail::Make::Body object.
100 sub body
101 {
102 89 ▶ my $self = shift( @_ );
Subroutine body: called CC=4 SCAR=14.2
Pod documented
103 89 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 84 5 89
104 {
105 84 my $body = shift( @_ );
106 84 ▶ if( defined( $body ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 84 0 84
107 {
108 84 ▶ unless( $self->_is_a( $body => 'Mail::Make::Body' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($body, "Mail::Make::Body")) 0 84 84
109 {
110 0 return( $self->error( "body: argument must be a Mail::Make::Body-derived object" ) );
111 }
112 }
113 84 $self->{body} = $body;
114 84 return( $self );
115 }
116 5 return( $self->{body} );
117 }
118  
119 # body_as_string()
120 # Returns the encoded body content as a scalar reference.
121 sub body_as_string
122 {
123 0 ▶ my $self = shift( @_ );
Subroutine body_as_string: not called CC=6 SCAR=37.4
Pod documented
124 0 ▶ unless( defined( $self->{body} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 0 0
125 {
126 0 return( $self->error( "No body is set on this entity" ) );
127 }
128  
129 0 ▶ my $enc = $self->headers->content_transfer_encoding // '';
Condition0%0/2
$self->headers->content_transfer_encoding // ""
l!l
00
MC/DC0%0/1
$self->headers->content_transfer_encoding // ""
$self->headers->content_transfer_encoding
Truth table0%0/2
A
A= $self->headers->content_transfer_encoding
Aresult
11
00
130 0 ▶ if( CORE::length( $enc ) && !$self->{is_encoded} )
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $enc and not $self->{"is_encoded"}) 0 0 0
Condition0%0/3
CORE::length $enc and not $self->{"is_encoded"}
!ll&&!rl&&r
000
MC/DC0%0/2
CORE::length $enc and not $self->{"is_encoded"}
CORE::length $enc not $self->{"is_encoded"}
Truth table0%0/3
A and not(B)
A= CORE::length $encB= not $self->{"is_encoded"}
ABresult
0X0
100
111
131 {
132 0 ▶ $self->encode_body || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body or return $self->pass_error 0 0 0
133 }
134 0 return( $self->{body}->as_string );
135 }
136  
137 # build( %params )
138 # Factory / class method: build a single MIME entity from parameters.
139 # This is the key method - it performs strict validation and correct encoding.
140 #
141 # Parameters:
142 # attach Shorthand for 'path'
143 # boundary boundary for multipart/* types (auto-generated if omitted)
144 # charset charset for text/* types
145 # cid Content-ID for inline parts (wrapped in <...> automatically if needed)
146 # data scalar body content
147 # debug debug level (default: 0)
148 # description Content-Description value
149 # disposition inline | attachment (default: none unless filename provided)
150 # encoding CTE (default: auto-suggested)
151 # filename filename for Content-Type name= and Content-Disposition filename=
152 # path file path body content
153 # top boolean - is this the top-level entity? (default: 1)
154 # type MIME type string (default: 'text/plain')
155 sub build
156 {
157 110 ▶ my $class = shift( @_ );
Subroutine build: called CC=78 SCAR=49.2
Pod documented
158 # Support both class and instance call
159 110 ▶ my $self = ref( $class ) ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 110 110
160 110 ▶ return( $self->pass_error ) if( !$self );
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 110 110
161 110 my $opts = $self->_get_args_as_hash( @_ );
162 110 $self->debug( delete( $opts->{debug} ) );
163  
164 # NOTE: 1. Extract and validate parameters
165 110 ▶ my $has_type = ( exists( $opts->{type} ) && defined( $opts->{type} ) );
Condition66%2/3
exists $opts->{'type'} && defined $opts->{'type'}
!ll&&!rl&&r
11099
MC/DC50%1/2
exists $opts->{'type'} && defined $opts->{'type'}
exists $opts->{'type'} defined $opts->{'type'}
Truth table66%2/3
A && B
A= exists $opts->{'type'}B= defined $opts->{'type'}
ABresult
0X0
100
111
166 110 ▶ my $type = lc( delete( $opts->{type} ) // 'text/plain' );
Condition100%2/2
delete $opts->{"type"} // "text/plain"
l!l
9911
MC/DC100%1/1
delete $opts->{"type"} // "text/plain"
delete $opts->{"type"}
Truth table100%2/2
A
A= delete $opts->{"type"}
Aresult
11
00
167 110 my $charset = delete( $opts->{charset} );
168 my $encoding = defined( $opts->{encoding} )
169 110 ▶ ? lc( delete( $opts->{encoding} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'encoding'} ? : 54 56 110
170 : undef;
171 my $disposition = defined( $opts->{disposition} )
172 110 ▶ ? lc( delete( $opts->{disposition} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'disposition'} ? : 21 89 110
173 : undef;
174 110 my $filename = delete( $opts->{filename} );
175 110 ▶ my $cid = delete( $opts->{cid} ) || delete( $opts->{id} );
Condition100%3/3
delete $opts->{'cid'} || delete $opts->{'id'}
l!l&&r!l&&!r
61103
MC/DC100%2/2
delete $opts->{'cid'} || delete $opts->{'id'}
delete $opts->{'cid'} delete $opts->{'id'}
Truth table100%3/3
A || B
A= delete $opts->{'cid'}B= delete $opts->{'id'}
ABresult
1X1
011
000
176 110 my $data = delete( $opts->{data} );
177 110 my $path = delete( $opts->{path} );
178 110 my $boundary = delete( $opts->{boundary} );
179 110 my $description = delete( $opts->{description} );
180 110 ▶ my $top = exists( $opts->{top} ) ? delete( $opts->{top} ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'top'} ? : 0 110 110
181  
182 110 ▶ if( !defined( $path ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $path || defined $data and exists $opts->{'attach'} and defined $opts->{'attach'} and !ref($opts->{'attach'}) || $self->_can_overload($opts->{'attach'}, '""') and index("$opts->{'attach'}", "\n") == -1) { } 3 107 110
elsif (not $has_type and defined $path and CORE::length $path) { } 8 198 206
Condition72%13/18
not defined $path || defined $data and exists $opts->{"attach"} and defined $opts->{"attach"} and !ref($opts->{"attach"}) || $self->_can_overload($opts->{"attach"}, "\"\"") and index(qq[$opts->{"attach"}], "\n") == -1
!ll&&!rl&&r
10703
not defined $path || defined $data and exists $opts->{"attach"} and defined $opts->{"attach"} and !ref($opts->{"attach"}) || $self->_can_overload($opts->{"attach"}, "\"\"")
!ll&&!rl&&r
10703
not defined $path || defined $data and exists $opts->{"attach"} and defined $opts->{"attach"}
!ll&&!rl&&r
10703
not defined $path || defined $data and exists $opts->{"attach"}
!ll&&!rl&&r
83243
defined $path || defined $data
l!l&&r!l&&!r
245927
not ref $opts->{"attach"} or $self->_can_overload($opts->{"attach"}, "\"\"")
l!l&&r!l&&!r
300
MC/DC42%3/7
not defined $path || defined $data and exists $opts->{"attach"} and defined $opts->{"attach"} and !ref($opts->{"attach"}) || $self->_can_overload($opts->{"attach"}, "\"\"") and index(qq[$opts->{"attach"}], "\n") == -1
defined $path defined $data exists $opts->{"attach"} defined $opts->{"attach"} not ref $opts->{"attach"} $self->_can_overload($opts->{"attach"}, "\"\"") index(qq[$opts->{"attach"}], "\n") == -1
Truth table44%4/9
not(A || B) and C and D and not(E) or F and G
A= defined $pathB= defined $dataC= exists $opts->{"attach"}D= defined $opts->{"attach"}E= not ref $opts->{"attach"}F= $self->_can_overload($opts->{"attach"}, "\"\"")G= index(qq[$opts->{"attach"}], "\n") == -1
ABCDEFGresult
1XXXXXX0
01XXXXX0
000XXXX0
0010XXX0
001100X0
00111X00
00110100
00111X11
00110111
183 !defined( $data ) &&
184 exists( $opts->{attach} ) &&
185 defined( $opts->{attach} ) &&
186 ( !ref( $opts->{attach} ) || $self->_can_overload( $opts->{attach} => '""' ) ) &&
187 index( "$opts->{attach}", "\n" ) == -1 )
188 {
189 3 my $f = $self->new_file( delete( $opts->{attach} ) ); # Module::Generic::File will trigger stringification
190 3 ▶ if( $f->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->exists) 2 1 3
191 {
192 2 $path = $f;
193 # Auto-detect the MIME type from the file content if not explicitly provided
194 2 ▶ if( !$has_type )
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_type) 2 0 2
195 {
196 2 my $candidate = $f->finfo->mime_type;
197 2 ▶ $type = "$candidate" if( $candidate );
Branch: 1 branch
BranchTrueFalseTotal
if $candidate 2 0 2
198 }
199 }
200 }
201 # Auto-detect MIME type from path if not explicitly provided
202 ▶ elsif( !$has_type && defined( $path ) && CORE::length( $path ) )
Condition66%4/6
not $has_type and defined $path and CORE::length $path
!ll&&!rl&&r
9908
not $has_type and defined $path
!ll&&!rl&&r
9908
MC/DC33%1/3
not $has_type and defined $path and CORE::length $path
not $has_type defined $path CORE::length $path
Truth table50%2/4
not(A) and B and C
A= not $has_typeB= defined $pathC= CORE::length $path
ABCresult
0XX0
10X0
1100
1111
203 {
204 8 my $f = $self->new_file( $path );
205 8 my $candidate = $f->finfo->mime_type;
206 8 ▶ $type = "$candidate" if( $candidate );
Branch: 1 branch
BranchTrueFalseTotal
if $candidate 8 0 8
207 }
208  
209 110 my $is_multipart = ( $type =~ m{^multipart/}i );
210 110 my $is_message = ( $type =~ m{^message/}i );
211  
212 # NOTE: 2. Validate the MIME type format
213 110 ▶ my $ct_obj = Mail::Make::Headers::ContentType->new( $type ) ||
Condition50%1/2
'Mail::Make::Headers::ContentType'->new($type) || (return $self->pass_error('Mail::Make::Headers::ContentType'->error))
l!l
1100
MC/DC0%0/1
'Mail::Make::Headers::ContentType'->new($type) || (return $self->pass_error('Mail::Make::Headers::ContentType'->error))
'Mail::Make::Headers::ContentType'->new($type)
Truth table50%1/2
A
A= 'Mail::Make::Headers::ContentType'->new($type)
Aresult
11
00
214 return( $self->pass_error( Mail::Make::Headers::ContentType->error ) );
215  
216 # NOTE: 3. Validate charset (text/* types only)
217 110 ▶ if( defined( $charset ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $charset) { } 54 56 110
218 {
219 54 ▶ if( !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart) { } 54 0 54
220 {
221 54 ▶ $ct_obj->param( charset => $charset ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("charset", $charset) or return $self->pass_error($ct_obj->error) 0 54 54
222 return( $self->pass_error( $ct_obj->error ) );
223 }
224 else
225 {
226 # Silently discard charset on multipart - do not pass invalid param
227 0 undef( $charset );
228 }
229 }
230 else
231 {
232 # Default charset for text/* parts
233 56 ▶ if( $type =~ m{^text/}i && !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[^text/]i and not $is_multipart) 9 47 56
Condition66%2/3
$type =~ m[^text/]i and not $is_multipart
!ll&&!rl&&r
4709
MC/DC50%1/2
$type =~ m[^text/]i and not $is_multipart
$type =~ m[^text/]i not $is_multipart
Truth table66%2/3
A and not(B)
A= $type =~ m[^text/]iB= not $is_multipart
ABresult
0X0
100
111
234 {
235 9 ▶ $ct_obj->param( charset => 'utf-8' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("charset", "utf-8") or return $self->pass_error($ct_obj->error) 0 9 9
236 return( $self->pass_error( $ct_obj->error ) );
237 }
238 }
239  
240 # NOTE: 4. Determine filename (from explicit param or basename of path)
241 110 ▶ if( !defined( $filename ) && defined( $path ) && CORE::length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $filename and defined $path and CORE::length $path) 16 94 110
Condition83%5/6
not defined $filename and defined $path and CORE::length $path
!ll&&!rl&&r
94016
not defined $filename and defined $path
!ll&&!rl&&r
108416
MC/DC66%2/3
not defined $filename and defined $path and CORE::length $path
not defined $filename defined $path CORE::length $path
Truth table75%3/4
not(A) and B and C
A= not defined $filenameB= defined $pathC= CORE::length $path
ABCresult
0XX0
10X0
1100
1111
242 {
243 16 ( $filename ) = ( $path =~ m{([^/\\]+)\z} );
244 }
245 # Empty string -> treat as no filename
246 110 ▶ undef( $filename ) if( defined( $filename ) && !CORE::length( $filename ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename and not CORE::length $filename 0 110 110
Condition66%2/3
defined $filename and not CORE::length $filename
!ll&&!rl&&r
84260
MC/DC0%0/2
defined $filename and not CORE::length $filename
defined $filename not CORE::length $filename
Truth table66%2/3
A and not(B)
A= defined $filenameB= not CORE::length $filename
ABresult
0X0
100
111
247  
248 # NOTE: 5. Content-Type set for filename
249 # 5. Set Content-Type name= parameter if we have a filename
250 # THIS IS THE KEY FIX: we go through ContentType->param() which
251 # handles RFC 2231 encoding, not a raw string that Mail::Field
252 # would misparse on commas.
253 110 ▶ if( defined( $filename ) && !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and not $is_multipart) 26 84 110
Condition66%2/3
defined $filename and not $is_multipart
!ll&&!rl&&r
84026
MC/DC50%1/2
defined $filename and not $is_multipart
defined $filename not $is_multipart
Truth table66%2/3
A and not(B)
A= defined $filenameB= not $is_multipart
ABresult
0X0
100
111
254 {
255 26 ▶ $ct_obj->param( name => $filename ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("name", $filename) or return $self->pass_error($ct_obj->error) 0 26 26
256 return( $self->pass_error( $ct_obj->error ) );
257 }
258  
259 # NOTE: 6. Validate / generate boundary for multipart
260 110 ▶ if( $is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multipart) 24 86 110
261 {
262 24 ▶ if( defined( $boundary ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $boundary) 1 23 24
263 {
264 1 ▶ if( !CORE::length( $boundary ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not CORE::length $boundary) { } 0 1 1
elsif ($boundary =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]]) { } 1 0 1
265 {
266 # Empty boundary: warn and generate a fresh one
267 0 warn( "Empty boundary string provided; generating a new one\n" );
268 0 undef( $boundary );
269 }
270 elsif( $boundary =~ /[^0-9a-zA-Z'()+_,\-.\/:=? ]/ )
271 {
272 1 return( $self->error( "Boundary '$boundary' contains illegal characters" ) );
273 }
274 }
275 23 ▶ $boundary //= $self->make_boundary;
Condition33%1/3
$boundary //= $self->make_boundary
l!l&&r!l&&!r
0230
MC/DC0%0/2
$boundary //= $self->make_boundary
$boundary $self->make_boundary
Truth table33%1/3
A //= B
A= $boundaryB= $self->make_boundary
ABresult
1X1
011
000
276 # Boundary is safe to pass directly (we just validated it)
277 23 ▶ $ct_obj->param( boundary => $boundary ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("boundary", $boundary) or return $self->pass_error($ct_obj->error) 0 23 23
278 return( $self->pass_error( $ct_obj->error ) );
279 }
280  
281 # NOTE: 7. Validate Content-Transfer-Encoding
282 109 ▶ if( defined( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 54 55 109
283 {
284 54 ▶ if( $is_multipart )
Branch: 3 branches
BranchTrueFalseTotal
if ($is_multipart) { } 1 53 54
elsif ($is_message) { } 0 53 53
elsif ($encoding eq 'suggest') { } 0 53 53
285 {
286 1 return( $self->error(
287 "build(): encoding '$encoding' is not permitted for multipart type '$type'."
288 ) );
289 }
290 elsif( $is_message )
291 {
292 # RFC 2045 §6.4 - multipart and message types must not have a CTE
293 0 undef( $encoding );
294 }
295 elsif( $encoding eq 'suggest' )
296 {
297 # Deferred: computed after we know the body type
298 0 undef( $encoding );
299 }
300 else
301 {
302 53 ▶ my $cte_obj = Mail::Make::Headers::ContentTransferEncoding->new( $encoding ) ||
Condition100%2/2
'Mail::Make::Headers::ContentTransferEncoding'->new($encoding) || (return $self->pass_error('Mail::Make::Headers::ContentTransferEncoding'->error))
l!l
521
MC/DC100%1/1
'Mail::Make::Headers::ContentTransferEncoding'->new($encoding) || (return $self->pass_error('Mail::Make::Headers::ContentTransferEncoding'->error))
'Mail::Make::Headers::ContentTransferEncoding'->new($encoding)
Truth table100%2/2
A
A= 'Mail::Make::Headers::ContentTransferEncoding'->new($encoding)
Aresult
11
00
303 return( $self->pass_error( Mail::Make::Headers::ContentTransferEncoding->error ) );
304 # 'binary' is not valid for text/* parts in SMTP contexts
305 52 ▶ if( $cte_obj->is_binary && $type =~ m{^text/}i )
Branch: 1 branch
BranchTrueFalseTotal
if ($cte_obj->is_binary and $type =~ m[^text/]i) 0 52 52
Condition33%1/3
$cte_obj->is_binary and $type =~ m[^text/]i
!ll&&!rl&&r
5200
MC/DC0%0/2
$cte_obj->is_binary and $type =~ m[^text/]i
$cte_obj->is_binary $type =~ m[^text/]i
Truth table33%1/3
A and B
A= $cte_obj->is_binaryB= $type =~ m[^text/]i
ABresult
0X0
100
111
306 {
307 0 return( $self->error( "Encoding 'binary' is not permitted for text/* type '$type'" ) );
308 }
309 }
310 }
311  
312 # NOTE: 8. Build a fresh entity object with fresh headers
313 107 ▶ my $entity = ref( $class ) ? $class->new : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 107 107
314 107 ▶ return( $self->pass_error ) if( !$entity );
Branch: 1 branch
BranchTrueFalseTotal
unless $entity 0 107 107
315 107 ▶ my $headers = Mail::Make::Headers->new ||
Condition50%1/2
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
l!l
1070
MC/DC0%0/1
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
'Mail::Make::Headers'->new
Truth table50%1/2
A
A= 'Mail::Make::Headers'->new
Aresult
11
00
316 return( $self->pass_error( Mail::Make::Headers->error ) );
317 107 $entity->headers( $headers );
318  
319 # NOTE: 9. Attach body (single-part only)
320 107 ▶ if( !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_multipart) 85 22 107
321 {
322 85 ▶ if( defined( $path ) && CORE::length( $path ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path and CORE::length $path) { } 26 59 85
elsif (defined $data) { } 58 1 59
Condition66%2/3
defined $path and CORE::length $path
!ll&&!rl&&r
59026
MC/DC50%1/2
defined $path and CORE::length $path
defined $path CORE::length $path
Truth table66%2/3
A and B
A= defined $pathB= CORE::length $path
ABresult
0X0
100
111
323 {
324 26 ▶ my $body = Mail::Make::Body::File->new( $path ) ||
Condition100%2/2
'Mail::Make::Body::File'->new($path) || (return $self->pass_error('Mail::Make::Body::File'->error))
l!l
251
MC/DC100%1/1
'Mail::Make::Body::File'->new($path) || (return $self->pass_error('Mail::Make::Body::File'->error))
'Mail::Make::Body::File'->new($path)
Truth table100%2/2
A
A= 'Mail::Make::Body::File'->new($path)
Aresult
11
00
325 return( $self->pass_error( Mail::Make::Body::File->error ) );
326 25 $entity->body( $body );
327 }
328 elsif( defined( $data ) )
329 {
330 58 ▶ my $body = Mail::Make::Body::InCore->new( $data ) ||
Condition50%1/2
'Mail::Make::Body::InCore'->new($data) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
l!l
580
MC/DC0%0/1
'Mail::Make::Body::InCore'->new($data) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
'Mail::Make::Body::InCore'->new($data)
Truth table50%1/2
A
A= 'Mail::Make::Body::InCore'->new($data)
Aresult
11
00
331 return( $self->pass_error( Mail::Make::Body::InCore->error ) );
332 58 $entity->body( $body );
333 }
334 else
335 {
336 1 return( $self->error( "build: a body is required for non-multipart type '$type' - provide 'data' or 'path'" ) );
337 }
338 }
339  
340 # NOTE: 10. Auto-suggest encoding if not specified
341 105 ▶ if( !$is_multipart && !defined( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_multipart or defined $encoding) 31 74 105
Condition100%3/3
$is_multipart or defined $encoding
l!l&&r!l&&!r
225231
MC/DC100%2/2
$is_multipart or defined $encoding
$is_multipart defined $encoding
Truth table100%3/3
A or B
A= $is_multipartB= defined $encoding
ABresult
1X1
011
000
342 {
343 31 $entity->effective_type( $type );
344 31 $encoding = $entity->suggest_encoding;
345 }
346  
347 # NOTE: 11. Set the Content-Type header
348 105 $headers->replace( 'Content-Type' => $ct_obj->as_string );
349 105 $entity->effective_type( $type );
350  
351 # NOTE: 12. Set Content-Transfer-Encoding header (single-part only)
352 105 ▶ if( !$is_multipart && defined( $encoding ) && CORE::length( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart and defined $encoding and CORE::length $encoding) 83 22 105
Condition66%4/6
not $is_multipart and defined $encoding and CORE::length $encoding
!ll&&!rl&&r
22083
not $is_multipart and defined $encoding
!ll&&!rl&&r
22083
MC/DC33%1/3
not $is_multipart and defined $encoding and CORE::length $encoding
not $is_multipart defined $encoding CORE::length $encoding
Truth table50%2/4
not(A) and B and C
A= not $is_multipartB= defined $encodingC= CORE::length $encoding
ABCresult
0XX0
10X0
1100
1111
353 {
354 83 $headers->replace( 'Content-Transfer-Encoding' => $encoding );
355 }
356  
357 # NOTE: 13. Set Content-Disposition header
358 105 ▶ if( !$is_multipart && ( defined( $disposition ) || defined( $filename ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart and defined $disposition || defined $filename) 26 79 105
Condition100%6/6
not $is_multipart and defined $disposition || defined $filename
!ll&&!rl&&r
22785
defined $disposition or defined $filename
l!l&&r!l&&!r
21557
MC/DC66%2/3
not $is_multipart and defined $disposition || defined $filename
not $is_multipart defined $disposition defined $filename
Truth table75%3/4
not(A) and B or C
A= not $is_multipartB= defined $dispositionC= defined $filename
ABCresult
0XX0
1000
11X1
1011
359 {
360 # Default to 'attachment' when we have a filename but no explicit disposition
361 26 ▶ $disposition //= ( defined( $filename ) ? 'attachment' : 'inline' );
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 5 0 5
Condition66%2/3
$disposition //= defined $filename ? "attachment" : "inline"
l!l&&r!l&&!r
2150
MC/DC0%0/2
$disposition //= defined $filename ? "attachment" : "inline"
$disposition defined $filename ? "attachment" : "inline"
Truth table66%2/3
A //= B
A= $dispositionB= defined $filename ? "attachment" : "inline"
ABresult
1X1
011
000
362 26 ▶ my $cd_obj = Mail::Make::Headers::ContentDisposition->new( $disposition ) ||
Condition100%2/2
'Mail::Make::Headers::ContentDisposition'->new($disposition) || (return $self->pass_error('Mail::Make::Headers::ContentDisposition'->error))
l!l
251
MC/DC100%1/1
'Mail::Make::Headers::ContentDisposition'->new($disposition) || (return $self->pass_error('Mail::Make::Headers::ContentDisposition'->error))
'Mail::Make::Headers::ContentDisposition'->new($disposition)
Truth table100%2/2
A
A= 'Mail::Make::Headers::ContentDisposition'->new($disposition)
Aresult
11
00
363 return( $self->pass_error( Mail::Make::Headers::ContentDisposition->error ) );
364 25 ▶ if( defined( $filename ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 25 0 25
365 {
366 25 ▶ $cd_obj->filename( $filename ) ||
Branch: 1 branch
BranchTrueFalseTotal
$cd_obj->filename($filename) or return $self->pass_error($cd_obj->error) 0 25 25
367 return( $self->pass_error( $cd_obj->error ) );
368 }
369 25 $headers->replace( 'Content-Disposition' => $cd_obj->as_string );
370 }
371  
372 # NOTE: 14. Set Content-ID header (for inline parts)
373 104 ▶ if( defined( $cid ) && CORE::length( $cid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cid and CORE::length $cid) 7 97 104
Condition66%2/3
defined $cid and CORE::length $cid
!ll&&!rl&&r
9707
MC/DC50%1/2
defined $cid and CORE::length $cid
defined $cid CORE::length $cid
Truth table66%2/3
A and B
A= defined $cidB= CORE::length $cid
ABresult
0X0
100
111
374 {
375 # Ensure it's wrapped in angle brackets
376 7 ▶ $cid = "<${cid}>" unless( $cid =~ /\A<[^>]+>\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cid =~ /\A<[^>]+>\z/ 7 0 7
377 7 $headers->replace( 'Content-ID' => $cid );
378 }
379  
380 # NOTE: 15. Set Content-Description header
381 104 ▶ if( defined( $description ) && CORE::length( $description ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $description and CORE::length $description) 0 104 104
Condition33%1/3
defined $description and CORE::length $description
!ll&&!rl&&r
10400
MC/DC0%0/2
defined $description and CORE::length $description
defined $description CORE::length $description
Truth table33%1/3
A and B
A= defined $descriptionB= CORE::length $description
ABresult
0X0
100
111
382 {
383 0 $headers->replace( 'Content-Description' => $description );
384 }
385  
386 104 return( $entity );
387 }
388  
389 # effective_type( [$type_string] )
390 # Gets or sets the cached effective MIME type string.
391 sub effective_type
392 {
393 344 ▶ my $self = shift( @_ );
Subroutine effective_type: called CC=5 SCAR=19.6
Pod documented
394 344 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 138 206 344
395 {
396 138 $self->{effective_type} = shift( @_ );
397 138 return( $self );
398 }
399 # Lazy: read from headers if not cached
400 206 ▶ unless( defined( $self->{effective_type} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"effective_type"}) 0 206 206
401 {
402 my $ct = $self->{headers}
403 0 ▶ ? ( $self->{headers}->content_type // '' )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'headers'} ? : 0 0 0
Condition0%0/2
$self->{"headers"}->content_type // ""
l!l
00
MC/DC0%0/1
$self->{"headers"}->content_type // ""
$self->{"headers"}->content_type
Truth table0%0/2
A
A= $self->{"headers"}->content_type
Aresult
11
00
404 : '';
405 # Strip parameters - just want type/subtype
406 0 ( $self->{effective_type} ) = ( $ct =~ m{^([^;\s]+)} );
407 }
408 206 return( $self->{effective_type} );
409 }
410  
411 # encode_body()
412 # Encodes the body according to the Content-Transfer-Encoding header.
413 # No-op if already encoded or no encoding header is set.
414 sub encode_body
415 {
416 51 ▶ my $self = shift( @_ );
Subroutine encode_body: called CC=18 SCAR=35.1
Pod documented
417 51 ▶ return( $self ) if( $self->{is_encoded} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_encoded"} 1 50 51
418 50 ▶ my $enc = lc( $self->headers->content_transfer_encoding // '' );
Condition50%1/2
$self->headers->content_transfer_encoding // ""
l!l
500
MC/DC0%0/1
$self->headers->content_transfer_encoding // ""
$self->headers->content_transfer_encoding
Truth table50%1/2
A
A= $self->headers->content_transfer_encoding
Aresult
11
00
419 50 ▶ return( $self ) unless( CORE::length( $enc ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $enc 0 50 50
420 50 my $body = $self->{body};
421 50 ▶ unless( defined( $body ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 50 50
422 {
423 0 return( $self->error( "encode_body: no body to encode." ) );
424 }
425  
426 # 7bit / 8bit: no transformation needed
427 50 ▶ if( $enc ne 'base64' && $enc ne 'quoted-printable' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc ne "base64" and $enc ne "quoted-printable") 0 50 50
Condition66%2/3
$enc ne "base64" and $enc ne "quoted-printable"
!ll&&!rl&&r
12380
MC/DC0%0/2
$enc ne "base64" and $enc ne "quoted-printable"
$enc ne "base64" $enc ne "quoted-printable"
Truth table66%2/3
A and B
A= $enc ne "base64"B= $enc ne "quoted-printable"
ABresult
0X0
100
111
428 {
429 0 $self->{is_encoded} = 1;
430 0 return( $self );
431 }
432  
433 # Open a read handle on the source body
434 50 ▶ my $from_fh = $body->open || return( $self->pass_error( $body->error ) );
Condition50%1/2
$body->open || (return $self->pass_error($body->error))
l!l
500
MC/DC0%0/1
$body->open || (return $self->pass_error($body->error))
$body->open
Truth table50%1/2
A
A= $body->open
Aresult
11
00
435  
436 # Choose the output destination to mirror the source:
437 # Body::File → encode to a temp file (no large attachment loaded into RAM)
438 # Body::InCore → encode into a scalar buffer in memory
439 50 my $new_body;
440 50 ▶ if( $body->is_on_file )
Branch: 1 branch
BranchTrueFalseTotal
if ($body->is_on_file) { } 12 38 50
441 {
442 12 ▶ my $tmp = $self->new_tempfile( open => 1 ) || return( $self->pass_error );
Condition50%1/2
$self->new_tempfile('open', 1) || (return $self->pass_error)
l!l
120
MC/DC0%0/1
$self->new_tempfile('open', 1) || (return $self->pass_error)
$self->new_tempfile('open', 1)
Truth table50%1/2
A
A= $self->new_tempfile('open', 1)
Aresult
11
00
443 12 $tmp->binmode( ':raw' );
444 12 ▶ if( $enc eq 'base64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'base64') { } 12 0 12
445 {
446 12 my $encoder = Mail::Make::Stream::Base64->new;
447 12 ▶ $encoder->encode( $from_fh => $tmp ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, $tmp) or return $self->pass_error($encoder->error) 0 12 12
448 return( $self->pass_error( $encoder->error ) );
449 }
450 else
451 {
452 0 my $encoder = Mail::Make::Stream::QuotedPrint->new;
453 0 ▶ $encoder->encode( $from_fh => $tmp ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, $tmp) or return $self->pass_error($encoder->error) 0 0 0
454 return( $self->pass_error( $encoder->error ) );
455 }
456 12 ▶ $new_body = Mail::Make::Body::File->new( "$tmp" ) ||
Condition50%1/2
'Mail::Make::Body::File'->new("$tmp") || (return $self->pass_error('Mail::Make::Body::File'->error))
l!l
120
MC/DC0%0/1
'Mail::Make::Body::File'->new("$tmp") || (return $self->pass_error('Mail::Make::Body::File'->error))
'Mail::Make::Body::File'->new("$tmp")
Truth table50%1/2
A
A= 'Mail::Make::Body::File'->new("$tmp")
Aresult
11
00
457 return( $self->pass_error( Mail::Make::Body::File->error ) );
458 }
459 else
460 {
461 38 my $out = '';
462 38 ▶ if( $enc eq 'base64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'base64') { } 0 38 38
463 {
464 0 my $encoder = Mail::Make::Stream::Base64->new;
465 0 ▶ $encoder->encode( $from_fh => \$out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, \$out) or return $self->pass_error($encoder->error) 0 0 0
466 return( $self->pass_error( $encoder->error ) );
467 }
468 else
469 {
470 38 my $encoder = Mail::Make::Stream::QuotedPrint->new;
471 38 ▶ $encoder->encode( $from_fh => \$out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, \$out) or return $self->pass_error($encoder->error) 0 38 38
472 return( $self->pass_error( $encoder->error ) );
473 }
474 38 ▶ $new_body = Mail::Make::Body::InCore->new( $out ) ||
Condition50%1/2
'Mail::Make::Body::InCore'->new($out) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
l!l
380
MC/DC0%0/1
'Mail::Make::Body::InCore'->new($out) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
'Mail::Make::Body::InCore'->new($out)
Truth table50%1/2
A
A= 'Mail::Make::Body::InCore'->new($out)
Aresult
11
00
475 return( $self->pass_error( Mail::Make::Body::InCore->error ) );
476 }
477  
478 50 $self->{body} = $new_body;
479 50 $self->{is_encoded} = 1;
480 50 return( $self );
481 }
482  
483 # epilogue( [$arrayref] )
484 sub epilogue
485 {
486 0 ▶ my $self = shift( @_ );
Subroutine epilogue: not called CC=3 SCAR=24.8
Pod documented
487 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
488 {
489 0 my $val = shift( @_ );
490 0 ▶ $self->{epilogue} = ref( $val ) eq 'ARRAY' ? $val : [ $val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
491 0 return( $self );
492 }
493 0 return( $self->{epilogue} );
494 }
495  
496 # headers( [$headers_object] )
497 sub headers
498 {
499 390 ▶ my $self = shift( @_ );
Subroutine headers: called CC=5 SCAR=17.8
Pod documented
500 390 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 108 282 390
501 {
502 108 my $h = shift( @_ );
503 108 ▶ if( !$self->_is_a( $h => 'Mail::Make::Headers' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($h, "Mail::Make::Headers")) 0 108 108
504 {
505 0 return( $self->error( "headers: argument must be a Mail::Make::Headers object" ) );
506 }
507 108 $self->{headers} = $h;
508 108 return( $self );
509 }
510 282 ▶ unless( defined( $self->{headers} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"headers"}) 0 282 282
511 {
512 0 ▶ $self->{headers} = Mail::Make::Headers->new ||
Condition0%0/2
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
'Mail::Make::Headers'->new
Truth table0%0/2
A
A= 'Mail::Make::Headers'->new
Aresult
11
00
513 return( $self->pass_error( Mail::Make::Headers->error ) );
514 }
515 282 return( $self->{headers} );
516 }
517  
518 # is_encoded( [$bool] )
519 sub is_encoded
520 {
521 0 ▶ my $self = shift( @_ );
Subroutine is_encoded: not called CC=3 SCAR=24.8
Pod documented
522 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
523 {
524 0 ▶ $self->{is_encoded} = shift( @_ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift @_ ? : 0 0 0
525 0 return( $self );
526 }
527 0 return( $self->{is_encoded} );
528 }
529  
530 # is_multipart()
531 # Returns true if the effective MIME type is multipart/*.
532 sub is_multipart
533 {
534 72 ▶ my $self = shift( @_ );
Subroutine is_multipart: called CC=3 SCAR=11.1
Pod documented
535 72 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
720
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
536 72 ▶ return( ( $type =~ m{^multipart/}i ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^multipart/]i ? : 17 55 72
537 }
538  
539 # is_binary()
540 # Returns true if the MIME type is not a text type.
541 sub is_binary
542 {
543 0 ▶ my $self = shift( @_ );
Subroutine is_binary: not called CC=2 SCAR=17.9
Pod documented
544 0 ▶ return( $self->textual_type( $self->effective_type ) ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$self->textual_type($self->effective_type) ? : 0 0 0
545 }
546  
547 # is_text()
548 0 ▶sub is_text { return( !shift->is_binary ); }
Subroutine is_text: not called CC=1 SCAR=6.9
Pod documented
549  
550 # length()
551 # Returns the exact serialised size in bytes of this entity (headers + CRLF separator +
552 # encoded body, recursively for multipart).
553 #
554 # The calculation mirrors print() exactly - no serialisation buffer is accumulated.
555 # For singlepart entities the body is encoded first (if not already done) and the
556 # encoded body's length is obtained via Body::File::length (stat on disk) or
557 # Body::InCore::length (in-memory scalar length) without loading the content into a
558 # fresh buffer.
559 # Headers are unavoidably stringified since they are always in memory.
560 sub length
561 {
562 0 ▶ my $self = shift( @_ );
Subroutine length: not called CC=15 SCAR=53.5
Pod documented
563 0 my $total = 0;
564 7 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 # Headers + the blank separator line
567 0 my $hdr_str = $self->headers->as_string;
568 0 $total += CORE::length( $hdr_str );
569 0 $total += CORE::length( $CRLF ); # blank line between headers and body
570  
571 0 ▶ if( $self->is_multipart )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_multipart) { } 0 0 0
elsif (@{$self->{'_parts'};}) { } 0 0 0
572 {
573 0 my $boundary = $self->_extract_boundary;
574 0 ▶ unless( defined( $boundary ) && CORE::length( $boundary ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $boundary and CORE::length $boundary) 0 0 0
Condition0%0/3
defined $boundary and CORE::length $boundary
!ll&&!rl&&r
000
MC/DC0%0/2
defined $boundary and CORE::length $boundary
defined $boundary CORE::length $boundary
Truth table0%0/3
A and B
A= defined $boundaryB= CORE::length $boundary
ABresult
0X0
100
111
575 {
576 0 return( $self->error( "length: cannot measure multipart entity: no boundary." ) );
577 }
578  
579 # Preamble
580 0 ▶ if( @{$self->{preamble}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"preamble"};}) 0 0 0
581 {
582 0 $total += CORE::length( join( $CRLF, @{$self->{preamble}} ) . $CRLF );
583 }
584  
585 0 for my $part ( @{$self->{_parts}} )
586 {
587 0 $total += CORE::length( "--${boundary}${CRLF}" );
588 0 my $part_len = $part->length;
589 0 ▶ return( $self->pass_error( $part->error ) ) unless( defined( $part_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part_len 0 0 0
590 0 $total += $part_len;
591 0 $total += CORE::length( $CRLF ); # post-part CRLF
592 }
593  
594 # Closing boundary
595 0 $total += CORE::length( "--${boundary}--${CRLF}" );
596  
597 # Epilogue
598 0 ▶ if( @{$self->{epilogue}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"epilogue"};}) 0 0 0
599 {
600 0 $total += CORE::length( join( $CRLF, @{$self->{epilogue}} ) . $CRLF );
601 }
602 }
603 0 elsif( @{$self->{_parts}} )
604 {
605 # Nested singlepart with sub-parts (e.g. message/rfc822)
606 0 my $need_sep = 0;
607 0 for my $part ( @{$self->{_parts}} )
608 {
609 0 ▶ $total += CORE::length( "${CRLF}${CRLF}" ) if( $need_sep++ );
Branch: 1 branch
BranchTrueFalseTotal
if $need_sep++ 0 0 0
610 0 my $part_len = $part->length;
611 0 ▶ return( $self->pass_error( $part->error ) ) unless( defined( $part_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part_len 0 0 0
612 0 $total += $part_len;
613 }
614 }
615 else
616 {
617 # Plain singlepart: encode if not already done, then ask the body for its byte
618 # length without reading it into a new buffer.
619 0 ▶ unless( defined( $self->{body} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 0 0
620 {
621 0 return( $self->error( "length: no body set on this entity." ) );
622 }
623 0 ▶ $self->encode_body || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body or return $self->pass_error 0 0 0
624 0 my $body_len = $self->{body}->length;
625 0 ▶ return( $self->pass_error( $self->{body}->error ) ) unless( defined( $body_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body_len 0 0 0
626 0 $total += $body_len;
627 }
628  
629 0 return( $total );
630 }
631  
632 # make_boundary()
633 # Generates a unique boundary string suitable for MIME use.
634 24 ▶sub make_boundary { return( Data::UUID->new->create_str ); }
Subroutine make_boundary: called CC=1 SCAR=0.0
Pod documented
635  
636 # mime_type()
637 # Returns just the type/subtype portion (no parameters).
638 sub mime_type
639 {
640 25 ▶ my $self = shift( @_ );
Subroutine mime_type: called CC=2 SCAR=7.0
Pod documented
641 25 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
250
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
642 25 ( my $bare ) = ( $type =~ m{^([^;\s]+)} );
643 25 return( $bare );
644 }
645  
646 # parts( [$arrayref_or_list] )
647 # Gets or sets the list of sub-parts.
648 sub parts
649 {
650 11 ▶ my $self = shift( @_ );
Subroutine parts: called CC=6 SCAR=29.7
Pod documented
651 11 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 11 11
652 {
653 0 ▶ my $parts = ref( $_[0] ) eq 'ARRAY' ? $_[0] : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
654 0 for my $part ( @$parts )
655 {
656 0 ▶ unless( $self->_is_a( $part => 'Mail::Make::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($part, "Mail::Make::Entity")) 0 0 0
657 {
658 0 return( $self->error( "parts: each element must be a Mail::Make::Entity" ) );
659 }
660 }
661 0 $self->{_parts} = $parts;
662 0 return( $self );
663 }
664 11 ▶ return( wantarray() ? @{$self->{_parts}} : $self->{_parts} );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 11 11
665 }
666  
667 # preamble( [$arrayref] )
668 sub preamble
669 {
670 0 ▶ my $self = shift( @_ );
Subroutine preamble: not called CC=3 SCAR=24.8
Pod documented
671 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
672 {
673 0 my $val = shift( @_ );
674 0 ▶ $self->{preamble} = ref( $val ) eq 'ARRAY' ? $val : [ $val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
675 0 return( $self );
676 }
677 0 return( $self->{preamble} );
678 }
679  
680 # print( [$fh] )
681 # Serialises the entity to a filehandle.
682 sub print
683 {
684 64 ▶ my $self = shift( @_ );
Subroutine print: called CC=7 SCAR=23.2
Pod documented
685 64 ▶ my $fh = shift( @_ ) ||
Condition50%1/2
shift @_ || (return $self->error('No file handle was provided to print the mail entity to.'))
l!l
640
MC/DC0%0/1
shift @_ || (return $self->error('No file handle was provided to print the mail entity to.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
686 return( $self->error( "No file handle was provided to print the mail entity to." ) );
687 64 ▶ unless( $self->_is_glob( $fh ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_glob($fh)) 0 64 64
688 {
689 0 ▶ return( $self->error( "Value provided (", $self->_str_val( $fh // 'undef' ), ") is not a file handle." ) );
Condition0%0/2
$fh // "undef"
l!l
00
MC/DC0%0/1
$fh // "undef"
$fh
Truth table0%0/2
A
A= $fh
Aresult
11
00
690 }
691 # Headers
692 64 ▶ print( $fh $self->headers->as_string ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $self->headers->as_string or return $self->error("Cannot write headers: $!") 0 64 64
693 return( $self->error( "Cannot write headers: $!" ) );
694 # Blank line separating headers from body
695 64 ▶ print( $fh $CRLF ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $Mail::Make::Entity::CRLF or return $self->error("Cannot write header/body separator: $!") 0 64 64
696 return( $self->error( "Cannot write header/body separator: $!" ) );
697 # Body
698 64 ▶ $self->print_body( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_body($fh) or return $self->pass_error 0 64 64
699 64 return( $self );
700 }
701  
702 # print_body( [$fh] )
703 # Serialises the body (or multipart boundaries and sub-parts) to a filehandle.
704 sub print_body
705 {
706 64 ▶ my $self = shift( @_ );
Subroutine print_body: called CC=22 SCAR=42.6
Pod documented
707 64 ▶ my $fh = shift( @_ ) ||
Condition50%1/2
shift @_ || (return $self->error('No file handle was provided to print the mail entity to.'))
l!l
640
MC/DC0%0/1
shift @_ || (return $self->error('No file handle was provided to print the mail entity to.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
708 return( $self->error( "No file handle was provided to print the mail entity to." ) );
709 64 ▶ unless( $self->_is_glob( $fh ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_glob($fh)) 0 64 64
710 {
711 0 ▶ return( $self->error( "Value provided (", $self->_str_val( $fh // 'undef' ), ") is not a file handle." ) );
Condition0%0/2
$fh // "undef"
l!l
00
MC/DC0%0/1
$fh // "undef"
$fh
Truth table0%0/2
A
A= $fh
Aresult
11
00
712 }
713 64 ▶ if( $self->is_multipart )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_multipart) { } 13 51 64
elsif (@{$self->{'_parts'};}) { } 0 51 51
714 {
715 13 my $boundary = $self->_extract_boundary;
716 13 ▶ unless( defined( $boundary ) && CORE::length( $boundary ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $boundary and CORE::length $boundary) 0 13 13
Condition33%1/3
defined $boundary and CORE::length $boundary
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $boundary and CORE::length $boundary
defined $boundary CORE::length $boundary
Truth table33%1/3
A and B
A= defined $boundaryB= CORE::length $boundary
ABresult
0X0
100
111
717 {
718 0 return( $self->error( "Cannot serialise multipart entity: no boundary in Content-Type" ) );
719 }
720  
721 # Preamble
722 13 ▶ if( @{$self->{preamble}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"preamble"};}) 0 13 13
723 {
724 0 print( $fh join( $CRLF, @{$self->{preamble}} ) . $CRLF );
725 }
726 13 foreach my $part ( @{$self->{_parts}} )
727 {
728 27 ▶ print( $fh "--${boundary}${CRLF}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "--$boundary$Mail::Make::Entity::CRLF" or return $self->error("Cannot write part boundary: $!") 0 27 27
729 return( $self->error( "Cannot write part boundary: $!" ) );
730 27 ▶ $part->print( $fh ) ||
Branch: 1 branch
BranchTrueFalseTotal
$part->print($fh) or return $self->pass_error($part->error) 0 27 27
731 return( $self->pass_error( $part->error ) );
732 27 ▶ print( $fh $CRLF ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $Mail::Make::Entity::CRLF or return $self->error("Cannot write post-part CRLF: $!") 0 27 27
733 return( $self->error( "Cannot write post-part CRLF: $!" ) );
734 }
735 13 ▶ print( $fh "--${boundary}--${CRLF}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "--$boundary--$Mail::Make::Entity::CRLF" or return $self->error("Cannot write closing boundary: $!") 0 13 13
736 return( $self->error( "Cannot write closing boundary: $!" ) );
737 # Epilogue
738 13 ▶ if( @{$self->{epilogue}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"epilogue"};}) 0 13 13
739 {
740 0 print( $fh join( $CRLF, @{$self->{epilogue}} ) . $CRLF );
741 }
742 }
743 51 elsif( @{$self->{_parts}} )
744 {
745 # Nested singlepart with sub-parts (e.g. message/rfc822)
746 0 my $need_sep = 0;
747 0 foreach my $part ( @{$self->{_parts}} )
748 {
749 0 ▶ print( $fh "${CRLF}${CRLF}" ) if( $need_sep++ );
Branch: 1 branch
BranchTrueFalseTotal
if $need_sep++ 0 0 0
750 0 ▶ $part->print( $fh ) || return( $self->pass_error( $part->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$part->print($fh) or return $self->pass_error($part->error) 0 0 0
751 }
752 }
753 else
754 {
755 # Plain single part
756 51 ▶ unless( defined( $self->{body} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 51 51
757 {
758 0 return( $self->error( "print_body: no body set on this entity" ) );
759 }
760 51 ▶ $self->encode_body || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body or return $self->pass_error 0 51 51
761 my $in_ref = $self->{body}->as_string ||
762 51 ▶ return( $self->pass_error( $self->{body}->error ) );
Condition50%1/2
$self->{'body'}->as_string || (return $self->pass_error($self->{'body'}->error))
l!l
510
MC/DC0%0/1
$self->{'body'}->as_string || (return $self->pass_error($self->{'body'}->error))
$self->{'body'}->as_string
Truth table50%1/2
A
A= $self->{'body'}->as_string
Aresult
11
00
763 51 ▶ print( $fh $$in_ref ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $$in_ref or return $self->error("Cannot write body: $!") 0 51 51
764 return( $self->error( "Cannot write body: $!" ) );
765 }
766 64 return( $self );
767 }
768  
769 # stringify()
770 # Alias for as_string.
771 0 ▶sub stringify { return( shift->as_string ); }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
772  
773 0 ▶sub stringify_ref { return( shift->as_string_ref ); }
Subroutine stringify_ref: not called CC=1 SCAR=6.9
Pod documented
774  
775 # stringify_body()
776 # Alias for body_as_string.
777 0 ▶sub stringify_body { return( shift->body_as_string ); }
Subroutine stringify_body: not called CC=1 SCAR=6.9
Pod documented
778  
779 # stringify_header()
780 0 ▶sub stringify_header { return( shift->headers->as_string ); }
Subroutine stringify_header: not called CC=1 SCAR=6.9
Pod documented
781  
782 # suggest_encoding()
783 # Returns a suitable Content-Transfer-Encoding for this entity's MIME type.
784 # Rules:
785 # multipart/* / message/* -> '' (no encoding)
786 # text/* -> quoted-printable
787 # everything else -> base64
788 sub suggest_encoding
789 {
790 31 ▶ my $self = shift( @_ );
Subroutine suggest_encoding: called CC=5 SCAR=16.4
Pod documented
791 31 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
310
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
792 # Strip any parameters
793 31 ( $type ) = ( $type =~ m{^([^;\s]+)} );
794 31 $type = lc( $type );
795 31 ▶ return( '' ) if( $type =~ m{^multipart/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^multipart/] 0 31 31
796 31 ▶ return( '' ) if( $type =~ m{^message/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^message/] 0 31 31
797 31 ▶ return( 'quoted-printable' ) if( $type =~ m{^text/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/] 8 23 31
798 23 return( 'base64' );
799 }
800  
801 # textual_type( $mime_type )
802 # Returns true if the given MIME type is a text or message type.
803 sub textual_type
804 {
805 0 ▶ my( $self, $type ) = @_;
Subroutine textual_type: not called CC=4 SCAR=30.0
Pod documented
806 0 ▶ return(0) unless( defined( $type ) && CORE::length( $type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and CORE::length $type 0 0 0
Condition0%0/3
defined $type and CORE::length $type
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and CORE::length $type
defined $type CORE::length $type
Truth table0%0/3
A and B
A= defined $typeB= CORE::length $type
ABresult
0X0
100
111
807 0 ▶ return( ( $type =~ m{^(text|message)(/|\z)}i ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^(text|message)(/|\z)]i ? : 0 0 0
808 }
809  
810 # make_multipart( [ $subtype [, %opts] ] )
811 # Promotes a single-part entity to multipart by wrapping the existing body in a child
812 # entity and replacing the Content-Type with multipart/$subtype.
813 sub make_multipart
814 {
815 2 ▶ my $self = shift( @_ );
Subroutine make_multipart: called CC=12 SCAR=25.9
Pod documented
816 2 ▶ my $subtype = shift( @_ ) // 'mixed';
Condition50%1/2
shift @_ // "mixed"
l!l
20
MC/DC0%0/1
shift @_ // "mixed"
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
817 2 $subtype = lc( $subtype );
818 2 ▶ return( $self ) if( $self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_multipart 1 1 2
819 1 my $new_type = "multipart/${subtype}";
820 1 my $boundary = $self->make_boundary;
821 1 my $h = $self->headers;
822 1 ▶ if( $h )
Branch: 1 branch
BranchTrueFalseTotal
if ($h) 1 0 1
823 {
824 1 ▶ my $ct_obj = Mail::Make::Headers::ContentType->new( $new_type ) ||
Condition50%1/2
'Mail::Make::Headers::ContentType'->new($new_type) || (return $self->pass_error('Mail::Make::Headers::ContentType'->error))
l!l
10
MC/DC0%0/1
'Mail::Make::Headers::ContentType'->new($new_type) || (return $self->pass_error('Mail::Make::Headers::ContentType'->error))
'Mail::Make::Headers::ContentType'->new($new_type)
Truth table50%1/2
A
A= 'Mail::Make::Headers::ContentType'->new($new_type)
Aresult
11
00
825 return( $self->pass_error( Mail::Make::Headers::ContentType->error ) );
826 1 ▶ $ct_obj->boundary( $boundary ) || return( $self->pass_error( $ct_obj->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->boundary($boundary) or return $self->pass_error($ct_obj->error) 0 1 1
827 1 ▶ $h->set( 'Content-Type', "$ct_obj" ) || return( $self->pass_error( $h->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$h->set("Content-Type", "$ct_obj") or return $self->pass_error($h->error) 0 1 1
828 1 $h->remove( 'Content-Transfer-Encoding' );
829 }
830 1 ▶ if( $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body) 1 0 1
831 {
832 1 my $old_type = $self->effective_type;
833 1 ▶ my $child = Mail::Make::Entity->new ||
Condition50%1/2
'Mail::Make::Entity'->new || (return $self->pass_error)
l!l
10
MC/DC0%0/1
'Mail::Make::Entity'->new || (return $self->pass_error)
'Mail::Make::Entity'->new
Truth table50%1/2
A
A= 'Mail::Make::Entity'->new
Aresult
11
00
834 return( $self->pass_error );
835 1 ▶ my $child_h = Mail::Make::Headers->new ||
Condition50%1/2
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
l!l
10
MC/DC0%0/1
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
'Mail::Make::Headers'->new
Truth table50%1/2
A
A= 'Mail::Make::Headers'->new
Aresult
11
00
836 return( $self->pass_error( Mail::Make::Headers->error ) );
837 1 ▶ my $orig_ct = $h ? $h->get( 'Content-Type' ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$h ? : 1 0 1
838 1 ▶ $child_h->set( 'Content-Type', $orig_ct ) if( defined( $orig_ct ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $orig_ct 1 0 1
839 1 $child->headers( $child_h );
840 1 $child->body( $self->body );
841 1 $child->effective_type( $old_type );
842 1 $self->{body} = undef;
843 1 push( @{$self->{_parts}}, $child );
844 }
845 1 $self->effective_type( $new_type );
846 1 return( $self );
847 }
848  
849 # purge()
850 # Recursively releases body content.
851 sub purge
852 {
853 1 ▶ my $self = shift( @_ );
Subroutine purge: called CC=3 SCAR=11.2
Pod documented
854 1 ▶ if( my $body = $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $self->body) 1 0 1
855 {
856 1 $body->purge;
857 1 $self->{body} = undef;
858 }
859 1 for my $part ( @{$self->{_parts}} )
860 {
861 0 $part->purge;
862 }
863 1 return( $self );
864 }
865  
866 # _extract_boundary()
867 # Pulls the boundary parameter out of the Content-Type header string.
868 sub _extract_boundary
869 {
870 13 ▶ my $self = shift( @_ );
Subroutine _extract_boundary: called CC=5 SCAR=17.8
871 # content_type() returns a typed object; we need the raw string.
872 13 ▶ my $ct = $self->headers->get( 'Content-Type' ) // '';
Condition50%1/2
$self->headers->get("Content-Type") // ""
l!l
130
MC/DC0%0/1
$self->headers->get("Content-Type") // ""
$self->headers->get("Content-Type")
Truth table50%1/2
A
A= $self->headers->get("Content-Type")
Aresult
11
00
873 13 ▶ return( undef ) unless( CORE::length( $ct ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $ct 0 13 13
874 # Match boundary="..." or boundary=...
875 13 ▶ if( $ct =~ /;\s*boundary=(?:"([^"]+)"|([^;\s]+))/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /;\s*boundary=(?:"([^"]+)"|([^;\s]+))/i) 13 0 13
876 {
877 13 ▶ return( $1 // $2 );
Condition66%2/3
$1 // $2
l!l&&r!l&&!r
940
MC/DC0%0/2
$1 // $2
$1 $2
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
878 }
879 0 return( undef );
880 }
881  
882 # NOTE: STORABLE support
883 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
884  
885 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
886  
887 7 1;
888 # NOTE: POD
889 __END__
« lib/Mail/Make/GPG.pm ↑ Summary lib/Mail/Make/Stream.pm »