Email::Simple

Section: User Contributed Perl Documentation (3)
Updated: 2021-01-27
Page Index
 

NAME

Email::Simple - simple parsing of RFC2822 message format and headers  

VERSION

version 2.216  

SYNOPSIS

  use Email::Simple;
  my $email = Email::Simple->new($text);

  my $from_header = $email->header("From");
  my @received = $email->header("Received");

  $email->header_set("From", 'Simon Cozens <simon@cpan.org>');

  my $old_body = $email->body;
  $email->body_set("Hello world\nSimon");

  print $email->as_string;

...or, to create a message from scratch...

  my $email = Email::Simple->create(
      header => [
        From    => 'casey@geeknest.com',
        To      => 'drain@example.com',
        Subject => 'Message in a bottle',
      ],
      body => '...',
  );

  $email->header_set( 'X-Content-Container' => 'bottle/glass' );

  print $email->as_string;

 

DESCRIPTION

The Email:: namespace was begun as a reaction against the increasing complexity and bugginess of Perl's existing email modules. "Email::*" modules are meant to be simple to use and to maintain, pared to the bone, fast, minimal in their external dependencies, and correct.  

METHODS

 

new

  my $email = Email::Simple->new($message, \%arg);

This method parses an email from a scalar containing an RFC2822 formatted message and returns an object. $message may be a reference to a message string, in which case the string will be altered in place. This can result in significant memory savings.

If you want to create a message from scratch, you should use the "create" method.

Valid arguments are:

  header_class - the class used to create new header objects
                 The named module is not 'require'-ed by Email::Simple!

 

create

  my $email = Email::Simple->create(header => [ @headers ], body => '...');

This method is a constructor that creates an Email::Simple object from a set of named parameters. The "header" parameter's value is a list reference containing a set of headers to be created. The "body" parameter's value is a scalar value holding the contents of the message body. Line endings in the body will normalized to CRLF.

If no "Date" header is specified, one will be provided for you based on the "gmtime" of the local machine. This is because the "Date" field is a required header and is a pain in the neck to create manually for every message. The "From" field is also a required header, but it is not provided for you.  

header_obj

  my $header = $email->header_obj;

This method returns the object representing the email's header. For the interface for this object, see Email::Simple::Header.  

header_obj_set

  $email->header_obj_set($new_header_obj);

This method substitutes the given new header object for the email's existing header object.  

header

  my @values = $email->header($header_name);
  my $first  = $email->header($header_name);
  my $value  = $email->header($header_name, $index);

In list context, this returns every value for the named header. In scalar context, it returns the first value for the named header. If second parameter is specified then instead first value it returns value at position $index (negative $index is from the end).  

header_set

    $email->header_set($field, $line1, $line2, ...);

Sets the header to contain the given data. If you pass multiple lines in, you get multiple headers, and order is retained. If no values are given to set, the header will be removed from to the message entirely.  

header_raw

This is another name (and the preferred one) for "header".  

header_raw_set

This is another name (and the preferred one) for "header_set".  

header_raw_prepend

  $email->header_raw_prepend($field => $value);

This method adds a new instance of the name field as the first field in the header.  

header_names

    my @header_names = $email->header_names;

This method returns the list of header names currently in the email object. These names can be passed to the "header" method one-at-a-time to get header values. You are guaranteed to get a set of headers that are unique. You are not guaranteed to get the headers in any order at all.

For backwards compatibility, this method can also be called as headers.  

header_pairs

  my @headers = $email->header_pairs;

This method returns a list of pairs describing the contents of the header. Every other value, starting with and including zeroth, is a header name and the value following it is the header value.  

header_raw_pairs

This is another name (and the preferred one) for "header_pairs".  

body

Returns the body text of the mail.  

body_set

Sets the body text of the mail.  

as_string

Returns the mail as a string, reconstructing the headers.  

crlf

This method returns the type of newline used in the email. It is an accessor only.  

default_header_class

This returns the class used, by default, for header objects, and is provided for subclassing. The default default is Email::Simple::Header.  

CAVEATS

Email::Simple handles only RFC2822 formatted messages. This means you cannot expect it to cope well as the only parser between you and the outside world, say for example when writing a mail filter for invocation from a .forward file (for this we recommend you use Email::Filter anyway).  

AUTHORS

Simon Cozens
Casey West
Ricardo SIGNES
 

CONTRIBUTORS

Brian Cassidy <bricas@cpan.org>
Christian Walde <walde.christian@googlemail.com>
Marc Bradshaw <marc@marcbradshaw.net>
Michael Stevens <mstevens@etla.org>
Pali <pali@cpan.org>
Ricardo SIGNES <rjbs@cpan.org>
Ronald F. Guilmette <rfg@tristatelogic.com>
William Yardley <pep@veggiechinese.net>
 

COPYRIGHT AND LICENSE

This software is copyright (c) 2003 by Simon Cozens.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


 

Index

NAME
VERSION
SYNOPSIS
DESCRIPTION
METHODS
new
create
header_obj
header_obj_set
header
header_set
header_raw
header_raw_set
header_raw_prepend
header_names
header_pairs
header_raw_pairs
body
body_set
as_string
crlf
default_header_class
CAVEATS
AUTHORS
CONTRIBUTORS
COPYRIGHT AND LICENSE