Courriel::Headers - The headers for an email part


version 0.19


    my $email = Courriel->parse( text => ... );
    my $headers = $email->headers;

    print "$_\n" for $headers->get('Received');


This class represents the headers of an email.

Any sub part of an email can have its own headers, so every part has an associated object representing its headers. This class makes no distinction between top-level headers and headers for a sub part.


This class supports the following methods:

Courriel::Headers->parse( ... )

This method creates a new object by parsing a string. It accepts the following parameters:

  • text

    The text to parse. This can either be a plain scalar or a reference to a scalar. If you pass a reference, the underlying scalar may be modified.

  • line_sep

    The line separator. This default to a "\r\n", but you can change it if necessary. Note that this only affects parsing, header objects are always output with RFC-compliant line endings.

Header parsing unfolds folded headers, and decodes any MIME-encoded values (as described in RFC 2047).

Courriel::Headers->new( headers => [ ... ] )

This method creates a new object. It accepts one parameter, headers, which should be an array reference of header names and values.

A given header key can appear multiple times.

This object does not (yet, perhaps) enforce RFC restrictions on repetition of certain headers.

Header order is preserved, per RFC 5322.


Given a header name, this returns a list of the values found for the header. Each occurrence of the header is returned as a separate value.

$headers->add( $name => $value )

Given a header name and value, this adds the headers to the object. If any of the headers already have values in the object, then new values are added after the existing values, rather than at the end of headers.

$headers->unshift( $name => $value )

This is like add(), but this pushes the headers onto the front of the internal headers array. This is useful if you are adding "Received" headers, which per RFC 5322, should always be added at the top of the headers.


Given a header name, this removes all instances of that header from the object.

$headers->replace( $name => $value )

A shortcut for calling remove() and add().

$headers->as_string( charset => ... )

This returns a string representing the headers in the object. The values will be folded and/or MIME-encoded as needed.

The charset parameter specifies what character set to use for MIME-encoding non-ASCII values. This defaults to "utf8". The charset name must be one recognized by the Encode module.

MIME encoding is always done using the "B" (Base64) encoding, never the "Q" encoding.


Dave Rolsky <>


This software is Copyright (c) 2011 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)