File Coverage

blib/lib/Email/Simple.pm
Criterion Covered Total %
statement 83 83 100.0
branch 18 18 100.0
condition 13 16 81.2
subroutine 20 20 100.0
pod 9 9 100.0
total 143 146 97.9


line stmt bran cond sub pod time code
1 22     22   356144 use v5.12.0;
  22         150  
2 22     22   121 use warnings;
  22         42  
  22         948  
3             package Email::Simple 2.218;
4             # ABSTRACT: simple parsing of RFC2822 message format and headers
5              
6 22     22   121 use Carp ();
  22         57  
  22         444  
7              
8 22     22   8984 use Email::Simple::Creator;
  22         59  
  22         647  
9 22     22   9224 use Email::Simple::Header;
  22         225  
  22         15499  
10              
11             our $GROUCHY = 0;
12              
13             # We are liberal in what we accept.
14 69     69   248 sub __crlf_re { qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/; }
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 ');
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 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>
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             sub new {
73 54     54 1 14769 my ($class, $text, $arg) = @_;
74 54   50     328 $arg ||= {};
75              
76 54 100       651 Carp::croak 'Unable to parse undefined message' if ! defined $text;
77              
78 53 100 100     221 my $text_ref = (ref $text || '' eq 'SCALAR') ? $text : \$text;
79              
80 53 100       86 Carp::carp 'Message with wide characters' if ${$text_ref} =~ /[^\x00-\xFF]/;
  53         1042  
81              
82 53         203 my ($pos, $mycrlf) = $class->_split_head_from_body($text_ref);
83              
84 53         182 my $self = bless { mycrlf => $mycrlf } => $class;
85              
86 53         94 my $head;
87 53 100       111 if (defined $pos) {
88 43         211 $head = substr $$text_ref, 0, $pos, '';
89 43         121 substr($head, -(length $mycrlf)) = '';
90             } else {
91 10         20 $head = $$text_ref;
92 10         30 $text_ref = \'';
93             }
94              
95 53   33     244 my $header_class = $arg->{header_class} || $self->default_header_class;
96              
97 53         145 $self->header_obj_set(
98             $header_class->new(\$head, { crlf => $self->crlf })
99             );
100              
101 53         191 $self->body_set($text_ref);
102              
103 53         203 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             sub _split_head_from_body {
110 59     59   3455 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         210 my $re = qr{\x0a\x0d\x0a\x0d|\x0d\x0a\x0d\x0a|\x0d\x0d|\x0a\x0a};
116              
117 59 100       1108 if ($$text_ref =~ /($re)/gsm) {
118 48         238 my $crlf = substr $1, 0, length($1)/2;
119 48         261 return (pos($$text_ref), $crlf);
120             } else {
121              
122             # The body is, of course, optional.
123 11         52 my $re = $self->__crlf_re;
124 11         204 $$text_ref =~ /($re)/gsm;
125 11   100     109 return (undef, ($1 || "\n"));
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
parameter's value is a
135             #pod list reference containing a set of headers to be created. The C
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 header is specified, one will be provided for you based on the
140             #pod C of the local machine. This is because the C field is a required
141             #pod header and is a pain in the neck to create manually for every message. The
142             #pod C field is also a required header, but it is I provided for you.
143             #pod
144             #pod =cut
145              
146             our $CREATOR = 'Email::Simple::Creator';
147              
148             sub create {
149 13     13 1 7562 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   100     60 my $headers = $args{header} || [ Date => $CREATOR->_date_header ];
155 13   100     431 my $body = $args{body} || '';
156              
157 13         19 my $empty = q{};
158 13         39 my $header = \$empty;
159              
160 13         50 for my $idx (map { $_ * 2 } 0 .. @$headers / 2 - 1) {
  25         65  
161 25         65 my ($key, $value) = @$headers[ $idx, $idx + 1 ];
162 25         84 $CREATOR->_add_to_header($header, $key, $value);
163             }
164              
165 13         48 $CREATOR->_finalize_header($header);
166              
167 13         31 my $email = $class->new($header);
168              
169 13 100       61 $email->header_raw_set(Date => $CREATOR->_date_header)
170             unless defined $email->header_raw('Date');
171              
172 13         52 $body = (join $CREATOR->_crlf, split /\x0d\x0a|\x0a\x0d|\x0a|\x0d/, $body)
173             . $CREATOR->_crlf;
174              
175 13         36 $email->body_set($body);
176              
177 13         62 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.
187             #pod
188             #pod =cut
189              
190             sub header_obj {
191 143     143 1 248 my ($self) = @_;
192 143         550 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     22   2473 BEGIN { *__head = \&header_obj }
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             sub header_obj_set {
210 53     53 1 117 my ($self, $obj) = @_;
211 53         113 $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 value for the named header. If second
222             #pod parameter is specified then instead I 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
.
236             #pod
237             #pod =method header_raw_set
238             #pod
239             #pod This is another name (and the preferred one) for C.
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
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.
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.
270             #pod
271             #pod =cut
272              
273             BEGIN {
274 22     22   232 no strict 'refs';
  22         50  
  22         2281  
275 22     22   98 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     94   881 *$method = sub { (shift)->header_obj->$method(@_) };
  94         10693  
283             }
284 22         6676 *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             sub body {
294 60     60 1 520 my ($self) = @_;
295 60 100       80 return (defined ${ $self->{body} }) ? ${ $self->{body} } : '';
  60         163  
  58         459  
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             sub body_set {
305 78     78 1 178 my ($self, $text) = @_;
306 78 100       194 my $text_ref = ref $text ? $text : \$text;
307 78 100 100     112 Carp::carp 'Body with wide characters' if defined ${$text_ref} and ${$text_ref} =~ /[^\x00-\xFF]/;
  78         221  
  76         874  
308 78         214 $self->{body} = $text_ref;
309 78         151 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             sub as_string {
319 49     49 1 10683 my $self = shift;
320 49         119 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     110 1 588 sub crlf { $_[0]->{mycrlf} }
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     53 1 164 sub default_header_class { 'Email::Simple::Header' }
340              
341             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 ');
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 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>
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
parameter's value is a
427             list reference containing a set of headers to be created. The C
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 header is specified, one will be provided for you based on the
432             C of the local machine. This is because the C field is a required
433             header and is a pain in the neck to create manually for every message. The
434             C field is also a required header, but it is I 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.
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 value for the named header. If second
458             parameter is specified then instead I 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
.
472              
473             =head2 header_raw_set
474              
475             This is another name (and the preferred one) for C.
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
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.
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.
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 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
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
563              
564             =item *
565              
566             Christian Walde
567              
568             =item *
569              
570             Marc Bradshaw
571              
572             =item *
573              
574             Michael Stevens
575              
576             =item *
577              
578             Pali
579              
580             =item *
581              
582             Ricardo Signes
583              
584             =item *
585              
586             Ricardo Signes
587              
588             =item *
589              
590             Ronald F. Guilmette
591              
592             =item *
593              
594             William Yardley
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__