Mail/Make/Entity.pm

filter: Statementstmt 72.0% 302 / 419 Branchbran 50.3% 131 / 260 Conditioncond 56.9% 86 / 151 MC/DCmcdc 29.2% 24 / 82 Subroutinesub 73.0% 38 / 52 Podpod 93.7% 30 / 32 totaltotal 61.3% 611 / 996 CC 197 SCAR 42.1
CC
197
Cov
63%
length
211.5
build
131.8
print_body
70.9
CRAP 67.1
« Mail/Make/GPG.pm ↑ Summary Mail/Make/Stream.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/Entity.pm
3 ## Version v0.4.2
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/02
7 ## Modified 2026/05/11
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 7 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
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' );
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.2';
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 370 ▶{
Subroutine init: called CC=2 SCAR=6.9
Pod documented
44 370 my $self = shift( @_ );
45 370 $self->{body} = undef; # Mail::Make::Body object
46 370 $self->{effective_type} = undef; # cached mime type string
47 370 $self->{epilogue} = [];
48 370 $self->{headers} = undef; # Mail::Make::Headers object
49 370 $self->{is_encoded} = 0;
50 370 $self->{preamble} = [];
51 370 $self->{_parts} = []; # [ Mail::Make::Entity, ... ]
52 370 $self->{_exception_class} = $EXCEPTION_CLASS;
53 370 $self->{_init_strict_use_sub} = 1;
54 370 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 370 0 370
55 370 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 102 ▶{
Subroutine add_part: called CC=2 SCAR=6.9
Pod documented
62 102 my( $self, $part ) = @_;
63 102 ▶ unless( $self->_is_a( $part => 'Mail::Make::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($part, "Mail::Make::Entity")) 1 101 102
64 {
65 1 return( $self->error( "Argument must be a Mail::Make::Entity" ) );
66 }
67 101 push( @{$self->{_parts}}, $part );
68 101 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 36 ▶{
Subroutine as_string: called CC=3 SCAR=11.2
Pod documented
75 36 my $self = shift( @_ );
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: $!") 36 0 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 36 0 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 1 ▶{
Subroutine as_string_ref: called CC=3 SCAR=11.2
Pod documented
89 1 my $self = shift( @_ );
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: $!") 1 0 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 1 0 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 141 ▶{
Subroutine body: called CC=4 SCAR=14.2
Pod documented
102 141 my $self = shift( @_ );
103 141 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 131 10 141
104 {
105 131 my $body = shift( @_ );
106 131 ▶ if( defined( $body ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 131 0 131
107 {
108 131 ▶ unless( $self->_is_a( $body => 'Mail::Make::Body' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($body, "Mail::Make::Body")) 0 131 131
109 {
110 0 return( $self->error( "Argument must be a Mail::Make::Body-derived object" ) );
111 }
112 }
113 131 $self->{body} = $body;
114 131 return( $self );
115 }
116 10 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 0 ▶{
Subroutine body_as_string: not called CC=6 SCAR=37.4
Pod documented
123 0 my $self = shift( @_ );
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 186 ▶{
Subroutine build: called CC=78 SCAR=48.8
Pod documented
157 186 my $class = shift( @_ );
158 # Support both class and instance call
159 186 ▶ my $self = ref( $class ) ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 186 186
160 186 ▶ return( $self->pass_error ) if( !$self );
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 186 186
161 186 my $opts = $self->_get_args_as_hash( @_ );
162 186 $self->debug( delete( $opts->{debug} ) );
163  
164 # NOTE: 1. Extract and validate parameters
165 186 ▶ my $has_type = ( exists( $opts->{type} ) && defined( $opts->{type} ) );
Condition66%2/3
exists $opts->{'type'} && defined $opts->{'type'}
!ll&&!rl&&r
120174
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 186 ▶ my $type = lc( delete( $opts->{type} ) // 'text/plain' );
Condition100%2/2
delete $opts->{"type"} // "text/plain"
l!l
17412
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 186 my $charset = delete( $opts->{charset} );
168 my $encoding = defined( $opts->{encoding} )
169 186 ▶ ? lc( delete( $opts->{encoding} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'encoding'} ? : 81 105 186
170 : undef;
171 my $disposition = defined( $opts->{disposition} )
172 186 ▶ ? lc( delete( $opts->{disposition} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'disposition'} ? : 41 145 186
173 : undef;
174 186 my $filename = delete( $opts->{filename} );
175 186 ▶ my $cid = delete( $opts->{cid} ) || delete( $opts->{id} );
Condition100%3/3
delete $opts->{'cid'} || delete $opts->{'id'}
l!l&&r!l&&!r
251160
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 186 my $data = delete( $opts->{data} );
177 186 my $path = delete( $opts->{path} );
178 186 my $boundary = delete( $opts->{boundary} );
179 186 my $description = delete( $opts->{description} );
180 186 ▶ my $top = exists( $opts->{top} ) ? delete( $opts->{top} ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'top'} ? : 0 186 186
181  
182 186 ▶ 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 183 186
elsif (not $has_type and defined $path and CORE::length $path) { } 9 348 357
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
18303
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
18303
not defined $path || defined $data and exists $opts->{"attach"} and defined $opts->{"attach"}
!ll&&!rl&&r
18303
not defined $path || defined $data and exists $opts->{"attach"}
!ll&&!rl&&r
130533
defined $path || defined $data
l!l&&r!l&&!r
2810256
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
17409
not $has_type and defined $path
!ll&&!rl&&r
17409
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 9 my $f = $self->new_file( $path );
205 9 my $candidate = $f->finfo->mime_type;
206 9 ▶ $type = "$candidate" if( $candidate );
Branch: 1 branch
BranchTrueFalseTotal
if $candidate 9 0 9
207 }
208  
209 186 my $is_multipart = ( $type =~ m{^multipart/}i );
210 186 my $is_message = ( $type =~ m{^message/}i );
211  
212 # NOTE: 2. Validate the MIME type format
213 186 ▶ 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
1860
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 186 ▶ if( defined( $charset ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $charset) { } 81 105 186
218 {
219 81 ▶ if( !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart) { } 81 0 81
220 {
221 81 ▶ $ct_obj->param( charset => $charset ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("charset", $charset) or return $self->pass_error($ct_obj->error) 81 0 81
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 105 ▶ if( $type =~ m{^text/}i && !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[^text/]i and not $is_multipart) 10 95 105
Condition66%2/3
$type =~ m[^text/]i and not $is_multipart
!ll&&!rl&&r
95010
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 10 ▶ $ct_obj->param( charset => 'utf-8' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("charset", "utf-8") or return $self->pass_error($ct_obj->error) 10 0 10
236 return( $self->pass_error( $ct_obj->error ) );
237 }
238 }
239  
240 # NOTE: 4. Determine filename (from explicit param or basename of path)
241 186 ▶ if( !defined( $filename ) && defined( $path ) && CORE::length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $filename and defined $path and CORE::length $path) 20 166 186
Condition83%5/6
not defined $filename and defined $path and CORE::length $path
!ll&&!rl&&r
166020
not defined $filename and defined $path
!ll&&!rl&&r
1015620
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 20 ( $filename ) = ( $path =~ m{([^/\\]+)\z} );
244 }
245 # Empty string -> treat as no filename
246 186 ▶ undef( $filename ) if( defined( $filename ) && !CORE::length( $filename ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename and not CORE::length $filename 0 186 186
Condition66%2/3
defined $filename and not CORE::length $filename
!ll&&!rl&&r
156300
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 186 ▶ if( defined( $filename ) && !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and not $is_multipart) 30 156 186
Condition66%2/3
defined $filename and not $is_multipart
!ll&&!rl&&r
156030
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 30 ▶ $ct_obj->param( name => $filename ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("name", $filename) or return $self->pass_error($ct_obj->error) 30 0 30
256 return( $self->pass_error( $ct_obj->error ) );
257 }
258  
259 # NOTE: 6. Validate / generate boundary for multipart
260 186 ▶ if( $is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multipart) 53 133 186
261 {
262 53 ▶ if( defined( $boundary ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $boundary) 1 52 53
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 52 ▶ $boundary //= $self->make_boundary;
Condition33%1/3
$boundary //= $self->make_boundary
l!l&&r!l&&!r
0520
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 52 ▶ $ct_obj->param( boundary => $boundary ) ||
Branch: 1 branch
BranchTrueFalseTotal
$ct_obj->param("boundary", $boundary) or return $self->pass_error($ct_obj->error) 52 0 52
278 return( $self->pass_error( $ct_obj->error ) );
279 }
280  
281 # NOTE: 7. Validate Content-Transfer-Encoding
282 185 ▶ if( defined( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 81 104 185
283 {
284 81 ▶ if( $is_multipart )
Branch: 3 branches
BranchTrueFalseTotal
if ($is_multipart) { } 1 80 81
elsif ($is_message) { } 0 80 80
elsif ($encoding eq 'suggest') { } 0 80 80
285 {
286 1 return( $self->error( "build(): encoding '$encoding' is not permitted for multipart type '$type'." ) );
287 }
288 elsif( $is_message )
289 {
290 # RFC 2045 §6.4; multipart and message types must not have a CTE
291 0 undef( $encoding );
292 }
293 elsif( $encoding eq 'suggest' )
294 {
295 # Deferred: computed after we know the body type
296 0 undef( $encoding );
297 }
298 else
299 {
300 80 ▶ 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
791
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
301 return( $self->pass_error( Mail::Make::Headers::ContentTransferEncoding->error ) );
302 # 'binary' is not valid for text/* parts in SMTP contexts
303 79 ▶ if( $cte_obj->is_binary && $type =~ m{^text/}i )
Branch: 1 branch
BranchTrueFalseTotal
if ($cte_obj->is_binary and $type =~ m[^text/]i) 0 79 79
Condition33%1/3
$cte_obj->is_binary and $type =~ m[^text/]i
!ll&&!rl&&r
7900
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
304 {
305 0 return( $self->error( "Encoding 'binary' is not permitted for text/* type '$type'" ) );
306 }
307 }
308 }
309  
310 # NOTE: 8. Build a fresh entity object with fresh headers
311 183 ▶ my $entity = ref( $class ) ? $class->new : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 183 183
312 183 ▶ return( $self->pass_error ) if( !$entity );
Branch: 1 branch
BranchTrueFalseTotal
unless $entity 0 183 183
313 183 ▶ my $headers = Mail::Make::Headers->new ||
Condition50%1/2
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
l!l
1830
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
314 return( $self->pass_error( Mail::Make::Headers->error ) );
315 183 $entity->headers( $headers );
316  
317 # NOTE: 9. Attach body (single-part only)
318 183 ▶ if( !$is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_multipart) 132 51 183
319 {
320 132 ▶ if( defined( $path ) && CORE::length( $path ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path and CORE::length $path) { } 30 102 132
elsif (defined $data) { } 101 1 102
Condition66%2/3
defined $path and CORE::length $path
!ll&&!rl&&r
102030
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
321 {
322 30 ▶ 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
291
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
323 return( $self->pass_error( Mail::Make::Body::File->error ) );
324 29 $entity->body( $body );
325 }
326 elsif( defined( $data ) )
327 {
328 101 ▶ 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
1010
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
329 return( $self->pass_error( Mail::Make::Body::InCore->error ) );
330 101 $entity->body( $body );
331 }
332 else
333 {
334 1 return( $self->error( "A body is required for non-multipart type '$type'. Provide 'data' or 'path'" ) );
335 }
336 }
337  
338 # NOTE: 10. Auto-suggest encoding if not specified
339 181 ▶ if( !$is_multipart && !defined( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_multipart or defined $encoding) 51 130 181
Condition100%3/3
$is_multipart or defined $encoding
l!l&&r!l&&!r
517951
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
340 {
341 51 $entity->effective_type( $type );
342 51 $encoding = $entity->suggest_encoding;
343 }
344  
345 # NOTE: 11. Set the Content-Type header
346 181 $headers->replace( 'Content-Type' => $ct_obj->as_string );
347 181 $entity->effective_type( $type );
348  
349 # NOTE: 12. Set Content-Transfer-Encoding header (single-part only)
350 181 ▶ if( !$is_multipart && defined( $encoding ) && CORE::length( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart and defined $encoding and CORE::length $encoding) 130 51 181
Condition66%4/6
not $is_multipart and defined $encoding and CORE::length $encoding
!ll&&!rl&&r
510130
not $is_multipart and defined $encoding
!ll&&!rl&&r
510130
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
351 {
352 130 $headers->replace( 'Content-Transfer-Encoding' => $encoding );
353 }
354  
355 # NOTE: 13. Set Content-Disposition header
356 181 ▶ if( !$is_multipart && ( defined( $disposition ) || defined( $filename ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart and defined $disposition || defined $filename) 46 135 181
Condition100%6/6
not $is_multipart and defined $disposition || defined $filename
!ll&&!rl&&r
518446
defined $disposition or defined $filename
l!l&&r!l&&!r
41584
MC/DC100%3/3
not $is_multipart and defined $disposition || defined $filename
not $is_multipart defined $disposition defined $filename
Truth table100%4/4
not(A) and B or C
A= not $is_multipartB= defined $dispositionC= defined $filename
ABCresult
0XX0
1000
11X1
1011
357 {
358 # Default to 'attachment' when we have a filename but no explicit disposition
359 46 ▶ $disposition //= ( defined( $filename ) ? 'attachment' : 'inline' );
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 5 0 5
Condition100%2/2
$disposition //= defined $filename ? "attachment" : "inline"
l!l
415
MC/DC100%1/1
$disposition //= defined $filename ? "attachment" : "inline"
$disposition
Truth table100%2/2
A
A= $disposition
Aresult
11
00
360 46 ▶ 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
451
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
361 return( $self->pass_error( Mail::Make::Headers::ContentDisposition->error ) );
362 45 ▶ if( defined( $filename ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 29 16 45
363 {
364 29 ▶ $cd_obj->filename( $filename ) ||
Branch: 1 branch
BranchTrueFalseTotal
$cd_obj->filename($filename) or return $self->pass_error($cd_obj->error) 29 0 29
365 return( $self->pass_error( $cd_obj->error ) );
366 }
367 45 $headers->replace( 'Content-Disposition' => $cd_obj->as_string );
368 }
369  
370 # NOTE: 14. Set Content-ID header (for inline parts)
371 180 ▶ if( defined( $cid ) && CORE::length( $cid ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cid and CORE::length $cid) 26 154 180
Condition66%2/3
defined $cid and CORE::length $cid
!ll&&!rl&&r
154026
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
372 {
373 # Ensure it's wrapped in angle brackets
374 26 ▶ $cid = "<${cid}>" unless( $cid =~ /\A<[^>]+>\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cid =~ /\A<[^>]+>\z/ 26 0 26
375 26 $headers->replace( 'Content-ID' => $cid );
376 }
377  
378 # NOTE: 15. Set Content-Description header
379 180 ▶ if( defined( $description ) && CORE::length( $description ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $description and CORE::length $description) 0 180 180
Condition33%1/3
defined $description and CORE::length $description
!ll&&!rl&&r
18000
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
380 {
381 0 $headers->replace( 'Content-Description' => $description );
382 }
383  
384 180 return( $entity );
385 }
386  
387 # effective_type( [$type_string] )
388 # Gets or sets the cached effective MIME type string.
389 sub effective_type
390 584 ▶{
Subroutine effective_type: called CC=5 SCAR=19.6
Pod documented
391 584 my $self = shift( @_ );
392 584 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 234 350 584
393 {
394 234 $self->{effective_type} = shift( @_ );
395 234 return( $self );
396 }
397 # Lazy: read from headers if not cached
398 350 ▶ unless( defined( $self->{effective_type} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"effective_type"}) 0 350 350
399 {
400 my $ct = $self->{headers}
401 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
402 : '';
403 # Strip parametersl just want type/subtype
404 0 ( $self->{effective_type} ) = ( $ct =~ m{^([^;\s]+)} );
405 }
406 350 return( $self->{effective_type} );
407 }
408  
409 # encode_body()
410 # Encodes the body according to the Content-Transfer-Encoding header.
411 # No-op if already encoded or no encoding header is set.
412 sub encode_body
413 51 ▶{
Subroutine encode_body: called CC=18 SCAR=35.1
Pod documented
414 51 my $self = shift( @_ );
415 51 ▶ return( $self ) if( $self->{is_encoded} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_encoded"} 1 50 51
416 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
417 50 ▶ return( $self ) unless( CORE::length( $enc ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $enc 0 50 50
418 50 my $body = $self->{body};
419 50 ▶ unless( defined( $body ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 50 50
420 {
421 0 return( $self->error( "No body to encode." ) );
422 }
423  
424 # 7bit / 8bit: no transformation needed
425 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
426 {
427 0 $self->{is_encoded} = 1;
428 0 return( $self );
429 }
430  
431 # Open a read handle on the source body
432 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
433  
434 # Choose the output destination to mirror the source:
435 # Body::File → encode to a temp file (no large attachment loaded into RAM)
436 # Body::InCore → encode into a scalar buffer in memory
437 50 my $new_body;
438 50 ▶ if( $body->is_on_file )
Branch: 1 branch
BranchTrueFalseTotal
if ($body->is_on_file) { } 12 38 50
439 {
440 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
441 12 $tmp->binmode( ':raw' );
442 12 ▶ if( $enc eq 'base64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'base64') { } 12 0 12
443 {
444 12 my $encoder = Mail::Make::Stream::Base64->new;
445 12 ▶ $encoder->encode( $from_fh => $tmp ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, $tmp) or return $self->pass_error($encoder->error) 12 0 12
446 return( $self->pass_error( $encoder->error ) );
447 }
448 else
449 {
450 0 my $encoder = Mail::Make::Stream::QuotedPrint->new;
451 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
452 return( $self->pass_error( $encoder->error ) );
453 }
454 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
455 return( $self->pass_error( Mail::Make::Body::File->error ) );
456 }
457 else
458 {
459 38 my $out = '';
460 38 ▶ if( $enc eq 'base64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'base64') { } 0 38 38
461 {
462 0 my $encoder = Mail::Make::Stream::Base64->new;
463 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
464 return( $self->pass_error( $encoder->error ) );
465 }
466 else
467 {
468 38 my $encoder = Mail::Make::Stream::QuotedPrint->new;
469 38 ▶ $encoder->encode( $from_fh => \$out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$encoder->encode($from_fh, \$out) or return $self->pass_error($encoder->error) 38 0 38
470 return( $self->pass_error( $encoder->error ) );
471 }
472 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
473 return( $self->pass_error( Mail::Make::Body::InCore->error ) );
474 }
475  
476 50 $self->{body} = $new_body;
477 50 $self->{is_encoded} = 1;
478 50 return( $self );
479 }
480  
481 # epilogue( [$arrayref] )
482 sub epilogue
483 0 ▶{
Subroutine epilogue: not called CC=3 SCAR=24.8
Pod documented
484 0 my $self = shift( @_ );
485 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
486 {
487 0 my $val = shift( @_ );
488 0 ▶ $self->{epilogue} = ref( $val ) eq 'ARRAY' ? $val : [ $val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
489 0 return( $self );
490 }
491 0 return( $self->{epilogue} );
492 }
493  
494 # headers( [$headers_object] )
495 sub headers
496 611 ▶{
Subroutine headers: called CC=5 SCAR=17.8
Pod documented
497 611 my $self = shift( @_ );
498 611 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 184 427 611
499 {
500 184 my $h = shift( @_ );
501 184 ▶ if( !$self->_is_a( $h => 'Mail::Make::Headers' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($h, "Mail::Make::Headers")) 0 184 184
502 {
503 0 return( $self->error( "Argument must be a Mail::Make::Headers object" ) );
504 }
505 184 $self->{headers} = $h;
506 184 return( $self );
507 }
508 427 ▶ unless( defined( $self->{headers} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"headers"}) 0 427 427
509 {
510 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
511 return( $self->pass_error( Mail::Make::Headers->error ) );
512 }
513 427 return( $self->{headers} );
514 }
515  
516 # is_encoded( [$bool] )
517 sub is_encoded
518 0 ▶{
Subroutine is_encoded: not called CC=3 SCAR=24.8
Pod documented
519 0 my $self = shift( @_ );
520 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
521 {
522 0 ▶ $self->{is_encoded} = shift( @_ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift @_ ? : 0 0 0
523 0 return( $self );
524 }
525 0 return( $self->{is_encoded} );
526 }
527  
528 # is_multipart()
529 # Returns true if the effective MIME type is multipart/*.
530 sub is_multipart
531 113 ▶{
Subroutine is_multipart: called CC=3 SCAR=11.1
Pod documented
532 113 my $self = shift( @_ );
533 113 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
1130
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
534 113 ▶ return( ( $type =~ m{^multipart/}i ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^multipart/]i ? : 30 83 113
535 }
536  
537 # is_binary()
538 # Returns true if the MIME type is not a text type.
539 sub is_binary
540 0 ▶{
Subroutine is_binary: not called CC=2 SCAR=17.9
Pod documented
541 0 my $self = shift( @_ );
542 0 ▶ return( $self->textual_type( $self->effective_type ) ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$self->textual_type($self->effective_type) ? : 0 0 0
543 }
544  
545 # is_text()
546 0 ▶sub is_text { return( !shift->is_binary ); }
Subroutine is_text: not called CC=1 SCAR=6.9
Pod documented
547  
548 # length()
549 # Returns the exact serialised size in bytes of this entity (headers + CRLF separator +
550 # encoded body, recursively for multipart).
551 #
552 # The calculation mirrors print() exactly so no serialisation buffer is accumulated.
553 # For singlepart entities the body is encoded first (if not already done) and the
554 # encoded body's length is obtained via Body::File::length (stat on disk) or
555 # Body::InCore::length (in-memory scalar length) without loading the content into a
556 # fresh buffer.
557 # Headers are unavoidably stringified since they are always in memory.
558 sub length
559 0 ▶{
Subroutine length: not called CC=15 SCAR=53.5
Pod documented
560 0 my $self = shift( @_ );
561 0 my $total = 0;
562 7 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
563  
564 # Headers + the blank separator line
565 0 my $hdr_str = $self->headers->as_string;
566 0 $total += CORE::length( $hdr_str );
567 0 $total += CORE::length( $CRLF ); # blank line between headers and body
568  
569 0 ▶ if( $self->is_multipart )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_multipart) { } 0 0 0
elsif (@{$self->{'_parts'};}) { } 0 0 0
570 {
571 0 my $boundary = $self->_extract_boundary;
572 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
573 {
574 0 return( $self->error( "Cannot measure multipart entity: no boundary." ) );
575 }
576  
577 # Preamble
578 0 ▶ if( @{$self->{preamble}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"preamble"};}) 0 0 0
579 {
580 0 $total += CORE::length( join( $CRLF, @{$self->{preamble}} ) . $CRLF );
581 }
582  
583 0 for my $part ( @{$self->{_parts}} )
584 {
585 0 $total += CORE::length( "--${boundary}${CRLF}" );
586 0 my $part_len = $part->length;
587 0 ▶ return( $self->pass_error( $part->error ) ) unless( defined( $part_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part_len 0 0 0
588 0 $total += $part_len;
589 0 $total += CORE::length( $CRLF ); # post-part CRLF
590 }
591  
592 # Closing boundary
593 0 $total += CORE::length( "--${boundary}--${CRLF}" );
594  
595 # Epilogue
596 0 ▶ if( @{$self->{epilogue}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"epilogue"};}) 0 0 0
597 {
598 0 $total += CORE::length( join( $CRLF, @{$self->{epilogue}} ) . $CRLF );
599 }
600 }
601 0 elsif( @{$self->{_parts}} )
602 {
603 # Nested singlepart with sub-parts (e.g. message/rfc822)
604 0 my $need_sep = 0;
605 0 for my $part ( @{$self->{_parts}} )
606 {
607 0 ▶ $total += CORE::length( "${CRLF}${CRLF}" ) if( $need_sep++ );
Branch: 1 branch
BranchTrueFalseTotal
if $need_sep++ 0 0 0
608 0 my $part_len = $part->length;
609 0 ▶ return( $self->pass_error( $part->error ) ) unless( defined( $part_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part_len 0 0 0
610 0 $total += $part_len;
611 }
612 }
613 else
614 {
615 # Plain singlepart: encode if not already done, then ask the body for its byte
616 # length without reading it into a new buffer.
617 0 ▶ unless( defined( $self->{body} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 0 0
618 {
619 0 return( $self->error( "No body set on this entity." ) );
620 }
621 0 ▶ $self->encode_body || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body or return $self->pass_error 0 0 0
622 0 my $body_len = $self->{body}->length;
623 0 ▶ return( $self->pass_error( $self->{body}->error ) ) unless( defined( $body_len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body_len 0 0 0
624 0 $total += $body_len;
625 }
626  
627 0 return( $total );
628 }
629  
630 # make_boundary()
631 # Generates a unique boundary string suitable for MIME use.
632 53 ▶sub make_boundary { return( Data::UUID->new->create_str ); }
Subroutine make_boundary: called CC=1 SCAR=0.0
Pod documented
633  
634 # mime_type()
635 # Returns just the type/subtype portion (no parameters).
636 sub mime_type
637 51 ▶{
Subroutine mime_type: called CC=2 SCAR=7.0
Pod documented
638 51 my $self = shift( @_ );
639 51 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
510
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
640 51 ( my $bare ) = ( $type =~ m{^([^;\s]+)} );
641 51 return( $bare );
642 }
643  
644 # parts( [$arrayref_or_list] )
645 # Gets or sets the list of sub-parts.
646 sub parts
647 38 ▶{
Subroutine parts: called CC=6 SCAR=29.7
Pod documented
648 38 my $self = shift( @_ );
649 38 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 38 38
650 {
651 0 ▶ my $parts = ref( $_[0] ) eq 'ARRAY' ? $_[0] : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
652 0 for my $part ( @$parts )
653 {
654 0 ▶ unless( $self->_is_a( $part => 'Mail::Make::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($part, "Mail::Make::Entity")) 0 0 0
655 {
656 0 return( $self->error( "Each element must be a Mail::Make::Entity" ) );
657 }
658 }
659 0 $self->{_parts} = $parts;
660 0 return( $self );
661 }
662 0 ▶ return( wantarray() ? @{$self->{_parts}} : $self->{_parts} );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 38 38
663 }
664  
665 # preamble( [$arrayref] )
666 sub preamble
667 0 ▶{
Subroutine preamble: not called CC=3 SCAR=24.8
Pod documented
668 0 my $self = shift( @_ );
669 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
670 {
671 0 my $val = shift( @_ );
672 0 ▶ $self->{preamble} = ref( $val ) eq 'ARRAY' ? $val : [ $val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
673 0 return( $self );
674 }
675 0 return( $self->{preamble} );
676 }
677  
678 # print( [$fh] )
679 # Serialises the entity to a filehandle.
680 sub print
681 64 ▶{
Subroutine print: called CC=7 SCAR=23.2
Pod documented
682 64 my $self = shift( @_ );
683 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
684 return( $self->error( "No file handle was provided to print the mail entity to." ) );
685 64 ▶ unless( $self->_is_glob( $fh ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_glob($fh)) 0 64 64
686 {
687 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
688 }
689 # Headers
690 64 ▶ print( $fh $self->headers->as_string ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $self->headers->as_string or return $self->error("Cannot write headers: $!") 64 0 64
691 return( $self->error( "Cannot write headers: $!" ) );
692 # Blank line separating headers from body
693 64 ▶ print( $fh $CRLF ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $Mail::Make::Entity::CRLF or return $self->error("Cannot write header/body separator: $!") 64 0 64
694 return( $self->error( "Cannot write header/body separator: $!" ) );
695 # Body
696 64 ▶ $self->print_body( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_body($fh) or return $self->pass_error 64 0 64
697 64 return( $self );
698 }
699  
700 # print_body( [$fh] )
701 # Serialises the body (or multipart boundaries and sub-parts) to a filehandle.
702 sub print_body
703 64 ▶{
Subroutine print_body: called CC=22 SCAR=42.6
Pod documented
704 64 my $self = shift( @_ );
705 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
706 return( $self->error( "No file handle was provided to print the mail entity to." ) );
707 64 ▶ unless( $self->_is_glob( $fh ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_glob($fh)) 0 64 64
708 {
709 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
710 }
711 64 ▶ if( $self->is_multipart )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_multipart) { } 13 51 64
elsif (@{$self->{'_parts'};}) { } 0 51 51
712 {
713 13 my $boundary = $self->_extract_boundary;
714 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
715 {
716 0 return( $self->error( "Cannot serialise multipart entity: no boundary in Content-Type" ) );
717 }
718  
719 # Preamble
720 13 ▶ if( @{$self->{preamble}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"preamble"};}) 0 13 13
721 {
722 0 print( $fh join( $CRLF, @{$self->{preamble}} ) . $CRLF );
723 }
724 13 foreach my $part ( @{$self->{_parts}} )
725 {
726 27 ▶ print( $fh "--${boundary}${CRLF}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "--$boundary$Mail::Make::Entity::CRLF" or return $self->error("Cannot write part boundary: $!") 27 0 27
727 return( $self->error( "Cannot write part boundary: $!" ) );
728 27 ▶ $part->print( $fh ) ||
Branch: 1 branch
BranchTrueFalseTotal
$part->print($fh) or return $self->pass_error($part->error) 27 0 27
729 return( $self->pass_error( $part->error ) );
730 27 ▶ print( $fh $CRLF ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $Mail::Make::Entity::CRLF or return $self->error("Cannot write post-part CRLF: $!") 27 0 27
731 return( $self->error( "Cannot write post-part CRLF: $!" ) );
732 }
733 13 ▶ print( $fh "--${boundary}--${CRLF}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "--$boundary--$Mail::Make::Entity::CRLF" or return $self->error("Cannot write closing boundary: $!") 13 0 13
734 return( $self->error( "Cannot write closing boundary: $!" ) );
735 # Epilogue
736 13 ▶ if( @{$self->{epilogue}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"epilogue"};}) 0 13 13
737 {
738 0 print( $fh join( $CRLF, @{$self->{epilogue}} ) . $CRLF );
739 }
740 }
741 51 elsif( @{$self->{_parts}} )
742 {
743 # Nested singlepart with sub-parts (e.g. message/rfc822)
744 0 my $need_sep = 0;
745 0 foreach my $part ( @{$self->{_parts}} )
746 {
747 0 ▶ print( $fh "${CRLF}${CRLF}" ) if( $need_sep++ );
Branch: 1 branch
BranchTrueFalseTotal
if $need_sep++ 0 0 0
748 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
749 }
750 }
751 else
752 {
753 # Plain single part
754 51 ▶ unless( defined( $self->{body} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"body"}) 0 51 51
755 {
756 0 return( $self->error( "No body set on this entity" ) );
757 }
758 51 ▶ $self->encode_body || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body or return $self->pass_error 51 0 51
759 my $in_ref = $self->{body}->as_string ||
760 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
761 51 ▶ print( $fh $$in_ref ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh $$in_ref or return $self->error("Cannot write body: $!") 51 0 51
762 return( $self->error( "Cannot write body: $!" ) );
763 }
764 64 return( $self );
765 }
766  
767 # stringify()
768 # Alias for as_string.
769 0 ▶sub stringify { return( shift->as_string ); }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
770  
771 0 ▶sub stringify_ref { return( shift->as_string_ref ); }
Subroutine stringify_ref: not called CC=1 SCAR=6.9
Pod documented
772  
773 # stringify_body()
774 # Alias for body_as_string.
775 0 ▶sub stringify_body { return( shift->body_as_string ); }
Subroutine stringify_body: not called CC=1 SCAR=6.9
Pod documented
776  
777 # stringify_header()
778 0 ▶sub stringify_header { return( shift->headers->as_string ); }
Subroutine stringify_header: not called CC=1 SCAR=6.9
Pod documented
779  
780 # suggest_encoding()
781 # Returns a suitable Content-Transfer-Encoding for this entity's MIME type.
782 # Rules:
783 # multipart/* / message/* -> '' (no encoding)
784 # text/* -> quoted-printable
785 # everything else -> base64
786 sub suggest_encoding
787 51 ▶{
Subroutine suggest_encoding: called CC=5 SCAR=16.4
Pod documented
788 51 my $self = shift( @_ );
789 51 ▶ my $type = $self->effective_type // '';
Condition50%1/2
$self->effective_type // ""
l!l
510
MC/DC0%0/1
$self->effective_type // ""
$self->effective_type
Truth table50%1/2
A
A= $self->effective_type
Aresult
11
00
790 # Strip any parameters
791 51 ( $type ) = ( $type =~ m{^([^;\s]+)} );
792 51 $type = lc( $type );
793 51 ▶ return( '' ) if( $type =~ m{^multipart/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^multipart/] 0 51 51
794 51 ▶ return( '' ) if( $type =~ m{^message/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^message/] 0 51 51
795 51 ▶ return( 'quoted-printable' ) if( $type =~ m{^text/} );
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/] 9 42 51
796 42 return( 'base64' );
797 }
798  
799 # textual_type( $mime_type )
800 # Returns true if the given MIME type is a text or message type.
801 sub textual_type
802 0 ▶{
Subroutine textual_type: not called CC=4 SCAR=30.0
Pod documented
803 0 my( $self, $type ) = @_;
804 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
805 0 ▶ return( ( $type =~ m{^(text|message)(/|\z)}i ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^(text|message)(/|\z)]i ? : 0 0 0
806 }
807  
808 # make_multipart( [ $subtype [, %opts] ] )
809 # Promotes a single-part entity to multipart by wrapping the existing body in a child
810 # entity and replacing the Content-Type with multipart/$subtype.
811 sub make_multipart
812 2 ▶{
Subroutine make_multipart: called CC=12 SCAR=25.9
Pod documented
813 2 my $self = shift( @_ );
814 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
815 2 $subtype = lc( $subtype );
816 2 ▶ return( $self ) if( $self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_multipart 1 1 2
817 1 my $new_type = "multipart/${subtype}";
818 1 my $boundary = $self->make_boundary;
819 1 my $h = $self->headers;
820 1 ▶ if( $h )
Branch: 1 branch
BranchTrueFalseTotal
if ($h) 1 0 1
821 {
822 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
823 return( $self->pass_error( Mail::Make::Headers::ContentType->error ) );
824 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) 1 0 1
825 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) 1 0 1
826 1 $h->remove( 'Content-Transfer-Encoding' );
827 }
828 1 ▶ if( $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body) 1 0 1
829 {
830 1 my $old_type = $self->effective_type;
831 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
832 return( $self->pass_error );
833 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
834 return( $self->pass_error( Mail::Make::Headers->error ) );
835 1 ▶ my $orig_ct = $h ? $h->get( 'Content-Type' ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$h ? : 1 0 1
836 1 ▶ $child_h->set( 'Content-Type', $orig_ct ) if( defined( $orig_ct ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $orig_ct 1 0 1
837 1 $child->headers( $child_h );
838 1 $child->body( $self->body );
839 1 $child->effective_type( $old_type );
840 1 $self->{body} = undef;
841 1 push( @{$self->{_parts}}, $child );
842 }
843 1 $self->effective_type( $new_type );
844 1 return( $self );
845 }
846  
847 # purge()
848 # Recursively releases body content.
849 sub purge
850 1 ▶{
Subroutine purge: called CC=3 SCAR=11.2
Pod documented
851 1 my $self = shift( @_ );
852 1 ▶ if( my $body = $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $self->body) 1 0 1
853 {
854 1 $body->purge;
855 1 $self->{body} = undef;
856 }
857 1 for my $part ( @{$self->{_parts}} )
858 {
859 0 $part->purge;
860 }
861 1 return( $self );
862 }
863  
864 # _extract_boundary()
865 # Pulls the boundary parameter out of the Content-Type header string.
866 sub _extract_boundary
867 13 ▶{
Subroutine _extract_boundary: called CC=5 SCAR=18.9
868 13 my $self = shift( @_ );
869 # content_type() returns a typed object; we need the raw string.
870 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
871 13 ▶ return( undef ) unless( CORE::length( $ct ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $ct 0 13 13
872 # Match boundary="..." or boundary=...
873 13 ▶ if( $ct =~ /;\s*boundary=(?:"([^"]+)"|([^;\s]+))/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /;\s*boundary=(?:"([^"]+)"|([^;\s]+))/i) 13 0 13
874 {
875 13 ▶ return( $1 // $2 );
Condition33%1/3
$1 // $2
l!l&&r!l&&!r
0130
MC/DC0%0/2
$1 // $2
$1 $2
Truth table33%1/3
A // B
A= $1B= $2
ABresult
1X1
011
000
876 }
877 0 return( undef );
878 }
879  
880 # NOTE: STORABLE support
881 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
882  
883 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
884  
885 7 1;
886 # NOTE: POD
887 __END__
« Mail/Make/GPG.pm ↑ Summary Mail/Make/Stream.pm »