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