DST609 has asked for the wisdom of the Perl Monks concerning the following question:
This code saves email messages and attachments using MIME::Parse. The text body of the message is saved as a string +number like **msg-6108-1.txt**
How do I find out what this file is going to be named? I'd like to prefix the file attachment name with this name so I can match the message to the attachments when I process the folder contents on the server using cron at a later time.
this is currently saving files like this:
msg-6108-1.txt
fileattachment.pdf
I'd like to save something like this:
msg-6108-1.txt
msg-6108-1-fileattachment.pdf
#!/usr/bin/perl use Net::IMAP::Simple::SSL; use MIME::Parser; print "Content-type: text/html\n\n"; $server = new Net::IMAP::Simple::SSL('xxx'); $server->login('xxx','xxx'); my $newm=0; $newm = $server->select('INBOX'); if ($newm==0) { $server->quit(); print "No New Messages."; exit; } my $outputdir = "./temp"; my $parser = new MIME::Parser; $parser->output_dir($outputdir); for (my $i = 1; $i <= $newm; $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'); print "#$i $from / $subject / $timestamp<br />"; for my $part ($entity->parts()) { print " / ".$part->mime_type; if ( $part->mime_type eq 'application/octet-stream' || $part-> +mime_type eq 'application/pdf' ) { my $filename = $part->bodyhandle->path; print " / $filename"; } print "<br />"; } $server->copy($i,'dump'); $server->delete($i); } $server->quit();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MIME::Parser Customize Attachment Naming Convention
by McA (Priest) on Oct 12, 2012 at 12:20 UTC |