File Coverage

blib/lib/Email/Simple.pm
Criterion Covered Total %
statement 86 86 100.0
branch 18 18 100.0
condition 13 16 81.2
subroutine 21 21 100.0
pod 9 9 100.0
total 147 150 98.0


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