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

I currently have a perl script that removes a pdf attatchment from an email sent to a specific account and runs it through various processes. I also parses various parts of the attatchment name to determine how to handle the attatchment.

The problem I am having is often the attatchment name will have brackets in it, i.e. 01267-OHN-CC-000088733[1].pdf and for some reason if it has these brackets it wont read the file name. Instead of $var = 01267-OHN-CC-000088733[1].pdf it equals -1.pdf or -2.pdf (the number depending on how many other files are already in that folder with the blank .pdf name). If the attatcment name does not have the brackets it reads the attatchment name no problem.

If I do a $entity->dump_skeleton I get the following output:
OUTPUT: Num-parts: 2 -- Content-type: multipart/alternative Effective-type: multipart/alternative Body-file: NONE Num-parts: 2 -- Content-type: text/plain Effective-type: text/plain Body-file: this/is/my/path/2006-02-17/msg-32404-1.txt -- Content-type: text/html Effective-type: text/html Body-file: this/is/my/path/saved/2006-02-17/msg-32404-2.html -- Content-type: application/octet-stream Effective-type: application/octet-stream Body-file: this/is/my/path/saved/2006-02-17/-3.pdf Recommended-filename: 01267-TXN-CC-000086475[1].pdf

So it appears its showing the right file name in

Recommended-filename: 01267-TXN-CC-000086475[1].pdf

I am just not sure how to get that value instead of what I currently get in

Body-file: this/is/my/path/saved/2006-02-17/-3.pdf

Here is the code snippet how I currently extract the file name

# create a MIME::Parser object to # extract any attachments found within. my $parser = new MIME::Parser; $parser->output_dir( $savedir ); my $entity = $parser->parse_data($msg); # extract our mime parts and go through each one. my @parts = $entity->parts; foreach my $part (@parts) { # determine the path to the file in question. my $path = ($part->bodyhandle) ? $part->bodyhandle->path : undef +; print "This is the $path \n"; #right here if it doesnt have the + brackets it will give the correct file name if it does it will come +back -1.pdf or some other count

Replies are listed 'Best First'.
Re: file attatchment name
by brian_d_foy (Abbot) on Feb 20, 2006 at 08:18 UTC

    In MIME::Parser::Filer::exorcise_filename, the module tries to make the filename safe. It doesn't like the square brackets, so it ends up getting rid of everything up to them. If you really wanted those filenames, you'd have to subclass the module, or perhaps save the files to core then write them to disk yourself.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
      when you say subclass the module, could you give me an example on how to do that or point me in the right direction of a tutorial?
Re: file attatchment name
by McD (Chaplain) on Feb 20, 2006 at 13:01 UTC
    $path is the path to where the MIME parser has parsed out your message on disk, and it does indeed try to parse into "safe" filenames.

    To get to the name suggested in the MIME structure, you want:
    $part->head->recommended_filename();

    See the manpage for MIME::Head

    Peace,
    -McD

    Update: fixed typo, tnx. That will teach me to post before coffee...
      Make that "recommended"
      ### The recommended name when extracted: $file_name = $head->recommended_filename;