Courriel

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

[ENHANCEMENTS]

- The is_binary flag has been replaced with an is_character flag. This flag
  defaults to false. Suggested by zby.


0.23     2011-09-13

[ENHANCEMENTS]

- Courriel->parse now accepts an is_binary flag so you can pass decoded
  (utf-8) data as the email text. However, it's probably smarter to simply
  pass binary data to Courriel and let it do the decoding. The issue was
  brought up by zby.


0.22     2011-09-13

[BUG FIXES]

- Require List::MoreUtils 0.28+, as tests fail with earlier versions.

Changes  view on Meta::CPAN

  setting the transfer encoding to 7bit, 8bit, or binary, then you'll end up
  with a noncomformant email. However, by default all raw content is base64
  encoded. It is strongly recommended that you pass raw content and let
  Courriel take care of the transfer encoding.


0.12     2011-06-07

[BUG FIXES]

- The value returned by Courriel::Part::Single->content() was not decoded to
  Perl's native utf-8 character set. Similarly, when encoding content for
  transfer, it was not first transformed to raw bytes.


0.11     2011-06-07

[BUG FIXES]

- Courriel::Builder now adds a "MIME-Version: 1.0" header to all emails it
  creates unless you explicitly set this header, which you probably shouldn't,

README.md  view on Meta::CPAN


## Courriel->parse( text => $raw\_email, is\_character => 0|1 )

This parses the given text and returns a new Courriel object. The text can be
provided as a string or a reference to a string.

If you pass a reference, then the scalar underlying the reference _will_ be
modified, so don't pass in something you don't want modified.

By default, Courriel expects that content passed in text is binary data. This
means that it has not been decoded into utf-8 with `Encode::decode()` or by
using a `:encoding(UTF-8)` IO layer.

In practice, this doesn't matter for most emails, since they either contain
only ASCII data or they actually do contain binary (non-character) data.
However, if an email is using the 8bit Content-Transfer-Encoding, then this
does matter.

If the email has already been decoded, you must set `is_character` to a true
value.

It's safest to simply pass binary data to Courriel and let it handle decoding
internally.

## $email->parts()

Returns an array (not a reference) of the parts this email contains.

## $email->part\_count()

lib/Courriel.pm  view on Meta::CPAN


=head2 Courriel->parse( text => $raw_email, is_character => 0|1 )

This parses the given text and returns a new Courriel object. The text can be
provided as a string or a reference to a string.

If you pass a reference, then the scalar underlying the reference I<will> be
modified, so don't pass in something you don't want modified.

By default, Courriel expects that content passed in text is binary data. This
means that it has not been decoded into utf-8 with C<Encode::decode()> or by
using a C<:encoding(UTF-8)> IO layer.

In practice, this doesn't matter for most emails, since they either contain
only ASCII data or they actually do contain binary (non-character) data.
However, if an email is using the 8bit Content-Transfer-Encoding, then this
does matter.

If the email has already been decoded, you must set C<is_character> to a true
value.

It's safest to simply pass binary data to Courriel and let it handle decoding
internally.

=head2 $email->parts()

Returns an array (not a reference) of the parts this email contains.

=head2 $email->part_count()

lib/Courriel/Part/Single.pm  view on Meta::CPAN

=back

You must pass a C<content> or C<encoded_content> value when creating a new
part, but there's really no point in passing both.

It is strongly recommended that you pass a C<content> parameter and letting
this module do the encoding for you internally.

=head2 $part->content()

This returns returns the decoded content for the part. It will be in Perl's
native utf-8 encoding, decoded from whatever character set the content is in.

=head2 $part->encoded_content()

This returns returns the encoded content for the part.

=head2 $part->mime_type()

Returns the mime type for this part.

=head2 $part->has_charset()

lib/Courriel/Part/Single.pm  view on Meta::CPAN


Returns false.

=head2 $part->container()

Returns the L<Courriel> or L<Courriel::Part::Multipart> object to which this
part belongs, if any. This is set when the part is added to another object.

=head2 $part->content_ref()

This returns returns a reference to a scalar containing the decoded content for
the part.

=head2 $part->encoded_content_ref()

This returns returns a reference to a scalar containing the encoded content for
the part, without any decoding.

=head2 $part->as_string()

Returns the part as a string, along with its headers. Lines will be terminated

t/Courriel.t  view on Meta::CPAN

EOF

    my $email = Courriel->parse( text => \$text, is_character => 1 );

    my $content = $email->plain_body_part->content;
    $content =~ s/[\r\n]//g;

    is(
        $content,
        "Vel\x{00E1}zquez",
        '8bit encoded body (marked as utf8) ends up decoded properly'
    );

    ok(
        is_utf8($content),
        'content ends up marked as is_utf8'
    );
}

{
    my $text = <<"EOF";

t/Courriel.t  view on Meta::CPAN

    $text = encode( 'utf-8', $text );

    my $email = Courriel->parse( text => \$text, is_character => 0 );

    my $content = $email->plain_body_part->content;
    $content =~ s/[\r\n]//g;

    is(
        $content,
        "Vel\x{00E1}zquez",
        '8bit encoded body (marked as binary) ends up decoded properly'
    );

    ok(
        is_utf8($content),
        'content ends up marked as is_utf8'
    );
}

{
    my $text = <<'EOF';

t/Part-Single.t  view on Meta::CPAN

            mime_type  => 'text/plain',
            attributes => { charset => 'UTF-8' },
        ),
        encoding        => 'base64',
        encoded_content => $encoded,
    );

    is(
        $part->content(),
        $orig_content,
        'decoded content matches original content',
    );
}

{
    my $orig_content = "foo \x{4E00} bar";

    my $part = Courriel::Part::Single->new(
        headers      => Courriel::Headers->new(),
        content_type => Courriel::Header::ContentType->new(
            mime_type  => 'text/plain',

t/Part-Single.t  view on Meta::CPAN

            mime_type  => 'text/plain',
            attributes => { charset => 'UTF-8' },
        ),
        encoding        => '8bit',
        encoded_content => $encoded,
    );

    is(
        $part->content(),
        $orig_content,
        'decoded content matches original content (8bit transfer encoding)',
    );
}

{
    my $orig_content = "foo \x{4E00} bar";

    my $part = Courriel::Part::Single->new(
        headers      => Courriel::Headers->new(),
        content_type => Courriel::Header::ContentType->new(
            mime_type  => 'text/plain',

t/Part-Single.t  view on Meta::CPAN

{
    my $part = Courriel::Part::Single->new(
        headers      => Courriel::Headers->new(),
        content_type =>
            Courriel::Header::ContentType->new( mime_type => 'image/jpeg' ),
        encoded_content => 'foo',
    );

    ok(
        !Encode::is_utf8( $part->content() ),
        'part is not decoded as utf8 when content-type has no charset'
    );
}

{
    my $part = Courriel::Part::Single->new(
        headers      => Courriel::Headers->new(),
        content_type =>
            Courriel::Header::ContentType->new( mime_type => 'image/jpeg' ),
        content => 'foo',
    );



( run in 0.361 second using v1.01-cache-2.11-cpan-26ccb49234f )