Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Saving attachments from email files

by inblosam (Monk)
on May 02, 2005 at 17:00 UTC ( #453326=perlquestion: print w/replies, xml ) Need Help??

inblosam has asked for the wisdom of the Perl Monks concerning the following question:

I need to save attachments from emails using Mail::POP3Client. I need to decode base64 files and then save, and other attachments just save them as is. Is there something that I haven't found that does this better than doing it myself? The text file contains the headers for the email, the body of the email, and then for each attachment a small header like these (2 attachments):
--Apple-Mail-16--694122637 Content-Transfer-Encoding: base64 Content-Type: application/pdf; x-unix-mode=0644; name="bus-schedule.pdf" Content-Disposition: inline; filename=some-file.pdf (then base64 code here) --Apple-Mail-18--693549197 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-mac-type=2A2A2A2A; x-unix-mode=0644; x-mac-creator=48647261; name="somefilename" Content-Disposition: attachment; filename=somefilename (then the file is here)
Here is my POP3Client script that right now returns the subject and from, and saves the whole email as a file.
#!/usr/bin/perl -w use strict; use Mail::POP3Client; use MIME::Parser; my $pop = new Mail::POP3Client( USER => "email\", PASSWORD => "mypassword", HOST => "" ); my $i; for( $i = 111; $i <= $pop->Count(); $i++ ) { foreach( $pop->Head( $i ) ) { /^(From|Subject):\s+/i && print $_, "\n"; my $dir = '/some/folder/somewhere/attachments'; my $parser = new MIME::Parser; my $io = new IO::File; if ($io->open( "> $dir/raw-message.$i")) { $pop->HeadAndBodyToFile( $io, $i ); $io->close; print "Parsing $dir/raw-message.$i...\n"; $parser->parse( IO::File->new( "$dir/raw-message.$i") ); } } } $pop->Close();
Can anyone advise? I need to be able to handle multiple files too, and pretty much any type of attachment.

Thanks in advance!

-Michael Jensen

Replies are listed 'Best First'.
Re: Saving attachments from email files
by dave0 (Friar) on May 02, 2005 at 17:26 UTC
    You're part-way there with MIME::Parser, but you're not actually using the return value of ->parse() for some reason. Try something like this as a starting point:

    my $entity = $parser->parse( IO::File->new( "$dir/raw-message.$i") ); foreach my $part ($entity->parts()) { # Look at $part (a MIME::Entity object) and determine what to do +with it }

    MIME::Parser::parse() will return a top-level MIME::Entity object for the message. If it's a multipart message, you can get the separate MIME::Entity objects for each subpart by iterating over $entity->parts().

    Then in that foreach loop, you can examine the content-type or filename of each part and determine what you want to do with it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://453326]
Approved by moot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-05-12 21:08 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (133 votes). Check out past polls.