David Foley wrote:
Hi Guys,
I'm using Net::POP3 to collect some mail. But some of the
messages contain attachments. I need to get these attachments into a
file. How do I do this?
Thanks
There are a number of tools that all claim to do this and many of them
do it reasonably well.
MIME::Tools is a series of packages that I have ended up with as a good
starting point. It might not be the smallest/leanest package out there
but I have trouble getting it to fail with all the varieties of mail
attachments and MIME abuses.
Approximately:
taking an email that is just a string ($message).
use MIME::Parser;
my $parser = new MIME::Parser;
$parser->ignore_errors(1);
$parser->output_dir('/tmp');
$parser->tmp_recycling(1);
$parser->extract_uuencode(1);
my $entity = eval { $parser->parse_data($args{message}) };
if ($@) {
cluck "Problem parsing Entity: $!";
my $results = $parser->results;
my $decapitated = $parser->last_head;
warn $results;
warn $decapitated,"\n";
}
$entity or croak "Unable to continue, no entity defined!\n$!\n";
From here you have MIME::Entity objects
They come in generally two flavors multipart/* and everything else
(non-multipart) It's the non-multipart that you want. If you are
willing to take a simple approach that works well without being to
particular about multipart consistency see 'parts_DFS' in the docs.
Foreach MIME::Entity get a bodyhandle()
If the bodyhandle is a MIME::Entity::File (non-multipart) then you can
do something with it depending on what your objectives are based on the
'effective_type' matching some string (text/plain...) or just find a
pull the 'recommended_filename' and away you go...
The docs are kind of extensive. After I got through about 60 pages
things started to gel and then it started to make sense.
There are two other approaches that I know of which are not quite as
detailed. You are putting some trust in the package developers to do
the right thing for you. Perhaps they do, or not...
Mail::Box also does MIME management and probably does it very well. I
had some problems with my application and Mail::Box/Mail::Message
modules. They don't have great IMAP support and are very oriented
towards local mail files (mbox/maildir). So if you are working off this
type of mail source these might do well for you.
Email::Simple have some tools but they are very new and require a little
more development. But the interface is pretty clean.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>