Email-Simple

 view release on metacpan or  search on metacpan

lib/Email/Simple.pm  view on Meta::CPAN

#pod   $email->header_set("From", 'Simon Cozens <simon@cpan.org>');
#pod
#pod   my $old_body = $email->body;
#pod   $email->body_set("Hello world\nSimon");
#pod
#pod   print $email->as_string;
#pod
#pod ...or, to create a message from scratch...
#pod
#pod   my $email = Email::Simple->create(
#pod       header => [
#pod         From    => 'casey@geeknest.com',
#pod         To      => 'drain@example.com',
#pod         Subject => 'Message in a bottle',
#pod       ],
#pod       body => '...',
#pod   );
#pod
#pod   $email->header_set( 'X-Content-Container' => 'bottle/glass' );
#pod
#pod   print $email->as_string;
#pod
#pod =head1 DESCRIPTION
#pod
#pod The Email:: namespace was begun as a reaction against the increasing complexity
#pod and bugginess of Perl's existing email modules.  C<Email::*> modules are meant
#pod to be simple to use and to maintain, pared to the bone, fast, minimal in their
#pod external dependencies, and correct.
#pod
#pod =method new
#pod
#pod   my $email = Email::Simple->new($message, \%arg);
#pod
#pod This method parses an email from a scalar containing an RFC2822 formatted
#pod message and returns an object.  C<$message> may be a reference to a message
#pod string, in which case the string will be altered in place.  This can result in
#pod significant memory savings.
#pod
#pod If you want to create a message from scratch, you should use the C<L</create>>
#pod method.
#pod
#pod Valid arguments are:
#pod
#pod   header_class - the class used to create new header objects
#pod                  The named module is not 'require'-ed by Email::Simple!
#pod
#pod =cut

sub new {
  my ($class, $text, $arg) = @_;
  $arg ||= {};

  Carp::croak 'Unable to parse undefined message' if ! defined $text;

  my $text_ref = (ref $text || '' eq 'SCALAR') ? $text : \$text;

  Carp::carp 'Message with wide characters' if ${$text_ref} =~ /[^\x00-\xFF]/;

  my ($pos, $mycrlf) = $class->_split_head_from_body($text_ref);

  my $self = bless { mycrlf => $mycrlf } => $class;

  my $head;
  if (defined $pos) {
    $head = substr $$text_ref, 0, $pos, '';
    substr($head, -(length $mycrlf)) = '';
  } else {
    $head     = $$text_ref;
    $text_ref = \'';
  }

  my $header_class = $arg->{header_class} || $self->default_header_class;

  $self->header_obj_set(
    $header_class->new(\$head, { crlf => $self->crlf })
  );

  $self->body_set($text_ref);

  return $self;
}

# Given the text of an email, return ($pos, $crlf) where $pos is the position
# at which the body text begins and $crlf is the type of newline used in the
# message.
sub _split_head_from_body {
  my ($self, $text_ref) = @_;

  # For body/header division, see RFC 2822, section 2.1
  #
  # Honestly, are we *ever* going to have LFCR messages?? -- rjbs, 2015-10-11
  my $re = qr{\x0a\x0d\x0a\x0d|\x0d\x0a\x0d\x0a|\x0d\x0d|\x0a\x0a};

  if ($$text_ref =~ /($re)/gsm) {
    my $crlf = substr $1, 0, length($1)/2;
    return (pos($$text_ref), $crlf);
  } else {

    # The body is, of course, optional.
    my $re = $self->__crlf_re;
    $$text_ref =~ /($re)/gsm;
    return (undef, ($1 || "\n"));
  }
}

#pod =method create
#pod
#pod   my $email = Email::Simple->create(header => [ @headers ], body => '...');
#pod
#pod This method is a constructor that creates an Email::Simple object
#pod from a set of named parameters. The C<header> parameter's value is a
#pod list reference containing a set of headers to be created. The C<body>
#pod parameter's value is a scalar value holding the contents of the message
#pod body.  Line endings in the body will normalized to CRLF.
#pod
#pod If no C<Date> header is specified, one will be provided for you based on the
#pod C<gmtime> of the local machine. This is because the C<Date> field is a required
#pod header and is a pain in the neck to create manually for every message. The
#pod C<From> field is also a required header, but it is I<not> provided for you.
#pod
#pod =cut

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.508 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )