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

I wrote this script (some parts removed) to grab read emails and save pdf's, when attached. The parser also saves a .txt file and .html file (when multi-type email) of the body of the message. This file is saved and named automatically. The files are named similar to: 26952-1.txt, 26952-2.txt. I'd like to know how to capture these file names so I can save them to a database or locate and move them after the pparser has finished.
use MIME::Parser; use MIME::Lite; use Email::Simple; use POSIX; $server = new Net::IMAP::Simple::SSL($AuthSrvr); $server->login($AuthUser,$AuthPass); my $parser = new MIME::Parser; my $outputdir="tmpMail/"; my $rsx = POSIX::strftime("%Y%m%d%H%M%S", localtime); $parser->output_dir($outputdir); $parser->output_prefix($rsx); my ($unseen, $recent, $num_messages) = $server->status($folder); $num_offset=$num_messages; $num_offset=2; # test a few if ($num_offset>$num_messages) { $num_offset=$num_messages; } for (my $i = 1; $i <= $num_offset; $i++) { my $entity = $parser->parse($server->getfh($i)); my $from = $entity->head->get('From'); my $subject = $entity->head->get('Subject'); my $timestamp = $entity->head->get('Date'); my $parts=0; for my $part ($entity->parts()) { if ($part->mime_type eq 'application/octet-stream' || $part->mime_ty +pe eq 'application/pdf' ) { $parts++; my $filename = $part->bodyhandle->path; if ($filename=~/^(.*)document_/) { move ($filename, $outputdir.$rsx.".pdf"); } } } $server->copy($i, 'dump'); $server->delete($i); } $server->quit();

Replies are listed 'Best First'.
Re: Mime::Parser - File Naming Convention
by Anonymous Monk on Apr 24, 2013 at 11:01 UTC

    I'd like to know how to capture these file names so I can save them to a database or locate and move them after the pparser has finished.

    What? Doesn't  my $filename = $part->bodyhandle->path; capture the filename?

      It does when there is an attachment. if there's no attachment a text file is produces seemingly out of the entity parts loop.

        It does when there is an attachment. if there's no attachment a text file is produces seemingly out of the entity parts loop.

        It's not that I don't believe you, but

        You test if filename contains document_

        When filename doesn't contain document_, your programs skip the filename

        What did you expect to happen?