Courriel - High level email parsing and manipulation
version 0.11
my $email = Courriel->parse( text => $raw_email ); print $email->subject(); print $_->address() for $email->participants(); print $email->datetime()->year(); if ( my $part = $email->plain_body_part() ) { print $part->content(); }
This software is still very alpha, and the API may change without warning in future versions.
This class exists to provide a high level API for working with emails, particular for processing incoming email. It is primarily a wrapper around the other classes in the Courriel distro, especially Courriel::Headers, Courriel::Part::Single, and Courriel::Part::Multipart. If you need lower level information about an email, it should be available from one of this classes.
This class provides the following methods:
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.
Returns an array (not a reference) of the parts this email contains.
Returns the number of parts this email contains.
Returns true if the top-level part is a multipart part, false otherwise.
Returns the actual top level part for the object. You're probably better off just calling $email->parts() most of the time, since when the email is multipart, the top level part is just a container.
$email->parts()
Returns the email's Subject header value, or undef if it doesn't have one.
undef
Returns a DateTime object for the email. The DateTime object is always in the "UTC" time zone.
This uses the Date header by default one. Otherwise it looks at the date in the first Received header, and then it looks for a Resent-Date header. If none of these exists, it just returns DateTime->now().
DateTime->now()
This returns a list of Email::Address objects, one for each unique participant in the email. This includes any address in the From, To, or CC headers.
This returns a list of Email::Address objects, one for each unique recipient in the email. This includes any address in the To or CC headers.
This returns the first Courriel::Part::Single object in the email with a mime type of "text/plain" and an inline disposition, if one exists.
This returns the first Courriel::Part::Single object in the email with a mime type of "text/html" and an inline disposition, if one exists.
Returns a new Courriel object that only contains inline parts from the original email, effectively removing all attachments.
Given a subroutine reference, this method calls that subroutine for each part in the email, in a depth-first search.
The subroutine receives the part as its only argument. If it returns true, this method returns that part.
The subroutine receives the part as its only argument. If it returns true, this method includes that part.
This method returns all of the parts that match the subroutine.
Returns the Courriel::ContentType object associated with the email.
Returns the Courriel::Headers object for this email.
Returns the email as a string, along with its headers. Lines will be terminated with "\r\n".
This release is still rough, and I have some plans for additional features:
Some more methods for walking/collecting multiple parts would be useful.
Stay tuned for details.
There a lot of email modules/distros on CPAN. Why didn't I use/fix one of them?
Mail::Box
This one probably does everything this module does and more, but it's really, really big and complicated, forcing the end user to make a lot of choices just to get started. If you need it, it's great, but I generally find it to be too much module for me.
Email::Simple and Email::MIME
These are surprisingly not simple. They suffer from a problematic API (too high level in some spots, too low in others), and a poor separation of concerns. I've hacked on these enough to know that I can never make them do what I want.
Everything Else
There's a lot of other email modules on CPAN, but none of them really seem any better than the ones mentioned above.
This module rips some chunks of code from a few other places, notably several of the Email suite modules.
Dave Rolsky <autarch@urth.org>
This software is Copyright (c) 2011 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Courriel, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Courriel
CPAN shell
perl -MCPAN -e shell install Courriel
For more information on module installation, please visit the detailed CPAN module installation guide.