Home > Archive > PERL Beginners > August 2005 > email attachments
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| David Foley 2005-08-04, 9:01 am |
| 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
--
*David Foley*
Reflex IS, Ireland
david@reflex.ie <mailto:david@reflex.ie>
tel: +353 51 841051
fax:+353 51 841052
http://www.reflex.ie
*Antivirus protection:Can you afford less than the best?*
Highest Tested
Detection Rate
Fastest Scanning Rate
Lowest Memory Use
New Advanced Heuristics
Detects Unknown Viruses
Automatic Internet Updates
Easy Administration
NOD 32 <http://www.nod32.ie>
| |
| Jeff Eggen 2005-08-04, 5:01 pm |
| >>> David Foley <david@reflex.ie> 04/08/2005 7:58:14 am >>>
>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?
MIME::Entity comes to mind.
http://search.cpan.org/~dskoll/MIME-tools-5.417/
Hope this helps,
Jeff Eggen
IT Programmer Analyst
Saskatchewan Government Insurance
Ph (306) 751-1795
email jeggen@sgi.sk.ca
************DISCLAIMER*************
This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the named addressee, please notify the sender immediately by e-mail if you have received
this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that using, disclosing, copying or distributing the contents of this information is strictly prohibited.
************DISCLAIMER*************
| |
| Tom Allison 2005-08-05, 8:59 am |
| 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.
|
|
|
|
|