Simple.pm

filter: Statementstmt 100% 86 / 86 Branchbran 100% 18 / 18 Conditioncond 81.2% 13 / 16 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 20 / 20 Podpod 100% 9 / 9 totaltotal 96.2% 152 / 158 CC 19 SCAR 12.8
CC
19
Cov
98%
new
8.0
create
5.0
body_set
4.0
CRAP 3.6
« Simple/Header.pm ↑ Summary Simple/Creator.pm »
1 22 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Simple 2.218;
4 # ABSTRACT: simple parsing of RFC2822 message format and headers
5  
6 22 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use Email::Simple::Creator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Email::Simple::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 our $GROUCHY = 0;
12  
13 # We are liberal in what we accept.
14 69 ▶sub __crlf_re { qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/; }
Subroutine __crlf_re: called CC=1 SCAR=0.0
15  
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod use Email::Simple;
19 #pod my $email = Email::Simple->new($text);
20 #pod
21 #pod my $from_header = $email->header("From");
22 #pod my @received = $email->header("Received");
23 #pod
24 #pod $email->header_set("From", 'Simon Cozens <simon@cpan.org>');
25 #pod
26 #pod my $old_body = $email->body;
27 #pod $email->body_set("Hello world\nSimon");
28 #pod
29 #pod print $email->as_string;
30 #pod
31 #pod ...or, to create a message from scratch...
32 #pod
33 #pod my $email = Email::Simple->create(
34 #pod header => [
35 #pod From => 'casey@geeknest.com',
36 #pod To => 'drain@example.com',
37 #pod Subject => 'Message in a bottle',
38 #pod ],
39 #pod body => '...',
40 #pod );
41 #pod
42 #pod $email->header_set( 'X-Content-Container' => 'bottle/glass' );
43 #pod
44 #pod print $email->as_string;
45 #pod
46 #pod =head1 DESCRIPTION
47 #pod
48 #pod The Email:: namespace was begun as a reaction against the increasing complexity
49 #pod and bugginess of Perl's existing email modules. C<Email::*> modules are meant
50 #pod to be simple to use and to maintain, pared to the bone, fast, minimal in their
51 #pod external dependencies, and correct.
52 #pod
53 #pod =method new
54 #pod
55 #pod my $email = Email::Simple->new($message, \%arg);
56 #pod
57 #pod This method parses an email from a scalar containing an RFC2822 formatted
58 #pod message and returns an object. C<$message> may be a reference to a message
59 #pod string, in which case the string will be altered in place. This can result in
60 #pod significant memory savings.
61 #pod
62 #pod If you want to create a message from scratch, you should use the C<L</create>>
63 #pod method.
64 #pod
65 #pod Valid arguments are:
66 #pod
67 #pod header_class - the class used to create new header objects
68 #pod The named module is not 'require'-ed by Email::Simple!
69 #pod
70 #pod =cut
71  
72 54 ▶sub new {
Subroutine new: called CC=8 SCAR=20.9
Pod documented
73 54 my ($class, $text, $arg) = @_;
74 54 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
054
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
75  
76 54 ▶ Carp::croak 'Unable to parse undefined message' if ! defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 53 54
77  
78 53 ▶ my $text_ref = (ref $text || '' eq 'SCALAR') ? $text : \$text;
Branch: 1 branch
BranchTrueFalseTotal
ref $text || !1 ? : 14 39 53
Condition100%2/2
ref $text or
l!l
1439
MC/DC100%1/1
ref $text or
ref $text
Truth table100%2/2
A
A= ref $text
Aresult
11
00
79  
80 53 ▶ Carp::carp 'Message with wide characters' if ${$text_ref} =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if ${$text_ref;} =~ /[^\x00-\xFF]/u 3 50 53
81  
82 53 my ($pos, $mycrlf) = $class->_split_head_from_body($text_ref);
83  
84 53 my $self = bless { mycrlf => $mycrlf } => $class;
85  
86 53 my $head;
87 53 ▶ if (defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 43 10 53
88 43 $head = substr $$text_ref, 0, $pos, '';
89 43 substr($head, -(length $mycrlf)) = '';
90 } else {
91 10 $head = $$text_ref;
92 10 $text_ref = \'';
93 }
94  
95 53 ▶ my $header_class = $arg->{header_class} || $self->default_header_class;
Condition33%1/3
$arg->{'header_class'} || $self->default_header_class
l!l&&r!l&&!r
0530
MC/DC0%0/2
$arg->{'header_class'} || $self->default_header_class
$arg->{'header_class'} $self->default_header_class
Truth table33%1/3
A || B
A= $arg->{'header_class'}B= $self->default_header_class
ABresult
1X1
011
000
96  
97 53 $self->header_obj_set(
98 $header_class->new(\$head, { crlf => $self->crlf })
99 );
100  
101 53 $self->body_set($text_ref);
102  
103 53 return $self;
104 }
105  
106 # Given the text of an email, return ($pos, $crlf) where $pos is the position
107 # at which the body text begins and $crlf is the type of newline used in the
108 # message.
109 59 ▶sub _split_head_from_body {
Subroutine _split_head_from_body: called CC=3 SCAR=11.0
110 59 my ($self, $text_ref) = @_;
111  
112 # For body/header division, see RFC 2822, section 2.1
113 #
114 # Honestly, are we *ever* going to have LFCR messages?? -- rjbs, 2015-10-11
115 59 my $re = qr{\x0a\x0d\x0a\x0d|\x0d\x0a\x0d\x0a|\x0d\x0d|\x0a\x0a};
116  
117 59 ▶ if ($$text_ref =~ /($re)/gsm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text_ref =~ /($re)/gmsu) { } 48 11 59
118 48 my $crlf = substr $1, 0, length($1)/2;
119 48 return (pos($$text_ref), $crlf);
120 } else {
121  
122 # The body is, of course, optional.
123 11 my $re = $self->__crlf_re;
124 11 $$text_ref =~ /($re)/gsm;
125 11 ▶ return (undef, ($1 || "\n"));
Condition100%2/2
$1 || "\n"
l!l
92
MC/DC100%1/1
$1 || "\n"
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
126 }
127 }
128  
129 #pod =method create
130 #pod
131 #pod my $email = Email::Simple->create(header => [ @headers ], body => '...');
132 #pod
133 #pod This method is a constructor that creates an Email::Simple object
134 #pod from a set of named parameters. The C<header> parameter's value is a
135 #pod list reference containing a set of headers to be created. The C<body>
136 #pod parameter's value is a scalar value holding the contents of the message
137 #pod body. Line endings in the body will normalized to CRLF.
138 #pod
139 #pod If no C<Date> header is specified, one will be provided for you based on the
140 #pod C<gmtime> of the local machine. This is because the C<Date> field is a required
141 #pod header and is a pain in the neck to create manually for every message. The
142 #pod C<From> field is also a required header, but it is I<not> provided for you.
143 #pod
144 #pod =cut
145  
146 22 our $CREATOR = 'Email::Simple::Creator';
147  
148 13 ▶sub create {
Subroutine create: called CC=5 SCAR=16.1
Pod documented
149 13 my ($class, %args) = @_;
150  
151 # We default it in here as well as below because by having it here, then we
152 # know that if there are no other headers, we'll get the proper CRLF.
153 # Otherwise, we get a message with incorrect CRLF. -- rjbs, 2007-07-13
154 13 ▶ my $headers = $args{header} || [ Date => $CREATOR->_date_header ];
Condition100%2/2
$args{'header'} || ['Date', $Email::Simple::CREATOR->_date_header]
l!l
103
MC/DC100%1/1
$args{'header'} || ['Date', $Email::Simple::CREATOR->_date_header]
$args{'header'}
Truth table100%2/2
A
A= $args{'header'}
Aresult
11
00
155 13 ▶ my $body = $args{body} || '';
Condition100%2/2
$args{'body'} || ''
l!l
76
MC/DC100%1/1
$args{'body'} || ''
$args{'body'}
Truth table100%2/2
A
A= $args{'body'}
Aresult
11
00
156  
157 13 my $empty = q{};
158 13 my $header = \$empty;
159  
160 13 for my $idx (map { $_ * 2 } 0 .. @$headers / 2 - 1) {
161 25 my ($key, $value) = @$headers[ $idx, $idx + 1 ];
162 25 $CREATOR->_add_to_header($header, $key, $value);
163 }
164  
165 13 $CREATOR->_finalize_header($header);
166  
167 13 my $email = $class->new($header);
168  
169 13 ▶ $email->header_raw_set(Date => $CREATOR->_date_header)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email->header_raw("Date") 7 6 13
170 unless defined $email->header_raw('Date');
171  
172 13 $body = (join $CREATOR->_crlf, split /\x0d\x0a|\x0a\x0d|\x0a|\x0d/, $body)
173 . $CREATOR->_crlf;
174  
175 13 $email->body_set($body);
176  
177 13 return $email;
178 }
179  
180  
181 #pod =method header_obj
182 #pod
183 #pod my $header = $email->header_obj;
184 #pod
185 #pod This method returns the object representing the email's header. For the
186 #pod interface for this object, see L<Email::Simple::Header>.
187 #pod
188 #pod =cut
189  
190 143 ▶sub header_obj {
Subroutine header_obj: called CC=1 SCAR=0.0
Pod documented
191 143 my ($self) = @_;
192 143 return $self->{header};
193 }
194  
195 # Probably needs to exist in perpetuity for modules released during the "__head
196 # is tentative" phase, until we have a way to force modules below us on the
197 # dependency tree to upgrade. i.e., never and/or in Perl 6 -- rjbs, 2006-11-28
198 22 ▶BEGIN { *__head = \&header_obj }
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 #pod =method header_obj_set
201 #pod
202 #pod $email->header_obj_set($new_header_obj);
203 #pod
204 #pod This method substitutes the given new header object for the email's existing
205 #pod header object.
206 #pod
207 #pod =cut
208  
209 53 ▶sub header_obj_set {
Subroutine header_obj_set: called CC=1 SCAR=0.0
Pod documented
210 53 my ($self, $obj) = @_;
211 53 $self->{header} = $obj;
212 }
213  
214 #pod =method header
215 #pod
216 #pod my @values = $email->header($header_name);
217 #pod my $first = $email->header($header_name);
218 #pod my $value = $email->header($header_name, $index);
219 #pod
220 #pod In list context, this returns every value for the named header. In scalar
221 #pod context, it returns the I<first> value for the named header. If second
222 #pod parameter is specified then instead I<first> value it returns value at
223 #pod position C<$index> (negative C<$index> is from the end).
224 #pod
225 #pod =method header_set
226 #pod
227 #pod $email->header_set($field, $line1, $line2, ...);
228 #pod
229 #pod Sets the header to contain the given data. If you pass multiple lines
230 #pod in, you get multiple headers, and order is retained. If no values are given to
231 #pod set, the header will be removed from to the message entirely.
232 #pod
233 #pod =method header_raw
234 #pod
235 #pod This is another name (and the preferred one) for C<header>.
236 #pod
237 #pod =method header_raw_set
238 #pod
239 #pod This is another name (and the preferred one) for C<header_set>.
240 #pod
241 #pod =method header_raw_prepend
242 #pod
243 #pod $email->header_raw_prepend($field => $value);
244 #pod
245 #pod This method adds a new instance of the name field as the first field in the
246 #pod header.
247 #pod
248 #pod =method header_names
249 #pod
250 #pod my @header_names = $email->header_names;
251 #pod
252 #pod This method returns the list of header names currently in the email object.
253 #pod These names can be passed to the C<header> method one-at-a-time to get header
254 #pod values. You are guaranteed to get a set of headers that are unique. You are not
255 #pod guaranteed to get the headers in any order at all.
256 #pod
257 #pod For backwards compatibility, this method can also be called as B<headers>.
258 #pod
259 #pod =method header_pairs
260 #pod
261 #pod my @headers = $email->header_pairs;
262 #pod
263 #pod This method returns a list of pairs describing the contents of the header.
264 #pod Every other value, starting with and including zeroth, is a header name and the
265 #pod value following it is the header value.
266 #pod
267 #pod =method header_raw_pairs
268 #pod
269 #pod This is another name (and the preferred one) for C<header_pairs>.
270 #pod
271 #pod =cut
272  
273 22 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
274 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 22 for my $method (qw(
276 header_raw header
277 header_raw_set header_set
278 header_raw_prepend
279 header_raw_pairs header_pairs
280 header_names
281 )) {
282 176 ▶ *$method = sub { (shift)->header_obj->$method(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
283 }
284 22 *headers = \&header_names;
285 }
286  
287 #pod =method body
288 #pod
289 #pod Returns the body text of the mail.
290 #pod
291 #pod =cut
292  
293 60 ▶sub body {
Subroutine body: called CC=2 SCAR=6.9
Pod documented
294 60 my ($self) = @_;
295 60 ▶ return (defined ${ $self->{body} }) ? ${ $self->{body} } : '';
Branch: 1 branch
BranchTrueFalseTotal
defined ${$$self{'body'};} ? : 58 2 60
296 }
297  
298 #pod =method body_set
299 #pod
300 #pod Sets the body text of the mail.
301 #pod
302 #pod =cut
303  
304 78 ▶sub body_set {
Subroutine body_set: called CC=4 SCAR=13.9
Pod documented
305 78 my ($self, $text) = @_;
306 78 ▶ my $text_ref = ref $text ? $text : \$text;
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 55 23 78
307 78 ▶ Carp::carp 'Body with wide characters' if defined ${$text_ref} and ${$text_ref} =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined ${$text_ref;} and ${$text_ref;} =~ /[^\x00-\xFF]/u 2 76 78
Condition100%3/3
defined ${$text_ref;} and ${$text_ref;} =~ /[^\x00-\xFF]/u
!ll&&!rl&&r
2742
MC/DC100%2/2
defined ${$text_ref;} and ${$text_ref;} =~ /[^\x00-\xFF]/u
defined ${$text_ref;} ${$text_ref;} =~ /[^\x00-\xFF]/u
Truth table100%3/3
A and B
A= defined ${$text_ref;}B= ${$text_ref;} =~ /[^\x00-\xFF]/u
ABresult
0X0
100
111
308 78 $self->{body} = $text_ref;
309 78 return;
310 }
311  
312 #pod =method as_string
313 #pod
314 #pod Returns the mail as a string, reconstructing the headers.
315 #pod
316 #pod =cut
317  
318 49 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
319 49 my $self = shift;
320 49 return $self->header_obj->as_string . $self->crlf . $self->body;
321 }
322  
323 #pod =method crlf
324 #pod
325 #pod This method returns the type of newline used in the email. It is an accessor
326 #pod only.
327 #pod
328 #pod =cut
329  
330 110 ▶sub crlf { $_[0]->{mycrlf} }
Subroutine crlf: called CC=1 SCAR=0.0
Pod documented
331  
332 #pod =method default_header_class
333 #pod
334 #pod This returns the class used, by default, for header objects, and is provided
335 #pod for subclassing. The default default is Email::Simple::Header.
336 #pod
337 #pod =cut
338  
339 53 ▶sub default_header_class { 'Email::Simple::Header' }
Subroutine default_header_class: called CC=1 SCAR=0.0
Pod documented
340  
341 22 1;
342  
343 =pod
344  
345 =encoding UTF-8
346  
347 =head1 NAME
348  
349 Email::Simple - simple parsing of RFC2822 message format and headers
350  
351 =head1 VERSION
352  
353 version 2.218
354  
355 =head1 SYNOPSIS
356  
357 use Email::Simple;
358 my $email = Email::Simple->new($text);
359  
360 my $from_header = $email->header("From");
361 my @received = $email->header("Received");
362  
363 $email->header_set("From", 'Simon Cozens <simon@cpan.org>');
364  
365 my $old_body = $email->body;
366 $email->body_set("Hello world\nSimon");
367  
368 print $email->as_string;
369  
370 ...or, to create a message from scratch...
371  
372 my $email = Email::Simple->create(
373 header => [
374 From => 'casey@geeknest.com',
375 To => 'drain@example.com',
376 Subject => 'Message in a bottle',
377 ],
378 body => '...',
379 );
380  
381 $email->header_set( 'X-Content-Container' => 'bottle/glass' );
382  
383 print $email->as_string;
384  
385 =head1 DESCRIPTION
386  
387 The Email:: namespace was begun as a reaction against the increasing complexity
388 and bugginess of Perl's existing email modules. C<Email::*> modules are meant
389 to be simple to use and to maintain, pared to the bone, fast, minimal in their
390 external dependencies, and correct.
391  
392 =head1 PERL VERSION
393  
394 This library should run on perls released even a long time ago. It should work
395 on any version of perl released in the last five years.
396  
397 Although it may work on older versions of perl, no guarantee is made that the
398 minimum required version will not be increased. The version may be increased
399 for any reason, and there is no promise that patches will be accepted to lower
400 the minimum required perl.
401  
402 =head1 METHODS
403  
404 =head2 new
405  
406 my $email = Email::Simple->new($message, \%arg);
407  
408 This method parses an email from a scalar containing an RFC2822 formatted
409 message and returns an object. C<$message> may be a reference to a message
410 string, in which case the string will be altered in place. This can result in
411 significant memory savings.
412  
413 If you want to create a message from scratch, you should use the C<L</create>>
414 method.
415  
416 Valid arguments are:
417  
418 header_class - the class used to create new header objects
419 The named module is not 'require'-ed by Email::Simple!
420  
421 =head2 create
422  
423 my $email = Email::Simple->create(header => [ @headers ], body => '...');
424  
425 This method is a constructor that creates an Email::Simple object
426 from a set of named parameters. The C<header> parameter's value is a
427 list reference containing a set of headers to be created. The C<body>
428 parameter's value is a scalar value holding the contents of the message
429 body. Line endings in the body will normalized to CRLF.
430  
431 If no C<Date> header is specified, one will be provided for you based on the
432 C<gmtime> of the local machine. This is because the C<Date> field is a required
433 header and is a pain in the neck to create manually for every message. The
434 C<From> field is also a required header, but it is I<not> provided for you.
435  
436 =head2 header_obj
437  
438 my $header = $email->header_obj;
439  
440 This method returns the object representing the email's header. For the
441 interface for this object, see L<Email::Simple::Header>.
442  
443 =head2 header_obj_set
444  
445 $email->header_obj_set($new_header_obj);
446  
447 This method substitutes the given new header object for the email's existing
448 header object.
449  
450 =head2 header
451  
452 my @values = $email->header($header_name);
453 my $first = $email->header($header_name);
454 my $value = $email->header($header_name, $index);
455  
456 In list context, this returns every value for the named header. In scalar
457 context, it returns the I<first> value for the named header. If second
458 parameter is specified then instead I<first> value it returns value at
459 position C<$index> (negative C<$index> is from the end).
460  
461 =head2 header_set
462  
463 $email->header_set($field, $line1, $line2, ...);
464  
465 Sets the header to contain the given data. If you pass multiple lines
466 in, you get multiple headers, and order is retained. If no values are given to
467 set, the header will be removed from to the message entirely.
468  
469 =head2 header_raw
470  
471 This is another name (and the preferred one) for C<header>.
472  
473 =head2 header_raw_set
474  
475 This is another name (and the preferred one) for C<header_set>.
476  
477 =head2 header_raw_prepend
478  
479 $email->header_raw_prepend($field => $value);
480  
481 This method adds a new instance of the name field as the first field in the
482 header.
483  
484 =head2 header_names
485  
486 my @header_names = $email->header_names;
487  
488 This method returns the list of header names currently in the email object.
489 These names can be passed to the C<header> method one-at-a-time to get header
490 values. You are guaranteed to get a set of headers that are unique. You are not
491 guaranteed to get the headers in any order at all.
492  
493 For backwards compatibility, this method can also be called as B<headers>.
494  
495 =head2 header_pairs
496  
497 my @headers = $email->header_pairs;
498  
499 This method returns a list of pairs describing the contents of the header.
500 Every other value, starting with and including zeroth, is a header name and the
501 value following it is the header value.
502  
503 =head2 header_raw_pairs
504  
505 This is another name (and the preferred one) for C<header_pairs>.
506  
507 =head2 body
508  
509 Returns the body text of the mail.
510  
511 =head2 body_set
512  
513 Sets the body text of the mail.
514  
515 =head2 as_string
516  
517 Returns the mail as a string, reconstructing the headers.
518  
519 =head2 crlf
520  
521 This method returns the type of newline used in the email. It is an accessor
522 only.
523  
524 =head2 default_header_class
525  
526 This returns the class used, by default, for header objects, and is provided
527 for subclassing. The default default is Email::Simple::Header.
528  
529 =head1 CAVEATS
530  
531 Email::Simple handles only RFC2822 formatted messages. This means you cannot
532 expect it to cope well as the only parser between you and the outside world,
533 say for example when writing a mail filter for invocation from a .forward file
534 (for this we recommend you use L<Email::Filter> anyway).
535  
536 =head1 AUTHORS
537  
538 =over 4
539  
540 =item *
541  
542 Simon Cozens
543  
544 =item *
545  
546 Casey West
547  
548 =item *
549  
550 Ricardo SIGNES <cpan@semiotic.systems>
551  
552 =back
553  
554 =head1 CONTRIBUTORS
555  
556 =for stopwords Brian Cassidy Christian Walde Marc Bradshaw Michael Stevens Pali Ricardo Signes Ronald F. Guilmette William Yardley
557  
558 =over 4
559  
560 =item *
561  
562 Brian Cassidy <bricas@cpan.org>
563  
564 =item *
565  
566 Christian Walde <walde.christian@googlemail.com>
567  
568 =item *
569  
570 Marc Bradshaw <marc@marcbradshaw.net>
571  
572 =item *
573  
574 Michael Stevens <mstevens@etla.org>
575  
576 =item *
577  
578 Pali <pali@cpan.org>
579  
580 =item *
581  
582 Ricardo Signes <rjbs@cpan.org>
583  
584 =item *
585  
586 Ricardo Signes <rjbs@semiotic.systems>
587  
588 =item *
589  
590 Ronald F. Guilmette <rfg@tristatelogic.com>
591  
592 =item *
593  
594 William Yardley <pep@veggiechinese.net>
595  
596 =back
597  
598 =head1 COPYRIGHT AND LICENSE
599  
600 This software is copyright (c) 2003 by Simon Cozens.
601  
602 This is free software; you can redistribute it and/or modify it under
603 the same terms as the Perl 5 programming language system itself.
604  
605 =cut
606  
607 __END__
« Simple/Header.pm ↑ Summary Simple/Creator.pm »