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\@mydomain.com", PASSWORD => "mypassword", HOST => "mail.mydomain.com" ); 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
michael@inblosam.com

In reply to Saving attachments from email files by inblosam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.