I wonder what would be the recommended module[s] to extract the body from some incoming mail messages.
The 'Perl Cookbook' sec. edition recommends MIME::Parser for getting the attachments; however, I'm interested actually _only_ in the body of the main part, not caring about attachments.
So I thought about using good old Mail::Internet. Out of curiosity, I took a look on Email::Simple and MIME::Parser. Finding them potential interesting too, I went a step further, trying to benchmark them on a simple mail sample.
Which one would be the winner, what are your bets...?
Here the - at least for me - VERY surprising results:
Rate mime_parser email_simple mail_internet
mime_parser 39.9/s -- -89% -99%
email_simple 355/s 788% -- -91%
mail_internet 3846/s 9531% 985%
And this is the code used:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw( :all );
use Mail::Internet;
use Email::Simple;
use MIME::Parser;
my @message;
push (@message, $_) while <>;
my $message = join( '', @message );
cmpthese( 1000, { 'mail_internet' => \&_mail_internet,
'email_simple' => \&_email_simple,
'mime_parser' => \&_mime_parser,
}
);
exit 0;
sub _mail_internet {
my $mail_internet = Mail::Internet->new( \@message );
my $body = join( '', @{ $mail_internet->body } );
}
sub _email_simple {
my $email_simple = Email::Simple->new($message);
my $body = $email_simple->body;
}
sub _mime_parser {
my $parser = new MIME::Parser;
my $entity = $parser->parse_data($message);
my $body_handle = $entity->bodyhandle;
my $body = $body_handle->as_string
}
The clear winner seems to be MIME::Parser - despite the fact that I'd have bet on Email::Simple before seeing the numbers.
So - what modules/ways to parse mail messages do other perlers use ?
Am I alone in being surprized by the above benchmarking results ?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.