http://qs321.pair.com?node_id=1201120

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

Hi gang, CENTOS 7, perl 5.

I'm having problems with MIME::Parser extracting (correctly) attachments but incorrectly naming them. An attachment defined as :

Content-Type: application/vnd.openxmlformats-officedocument.spreadshee +tml.sheet; name="Copy of 1501733944381-20170803 Dataload.xlsx" Content-Description: Copy of 1501733944381-20170803 Dataload.xlsx Content-Disposition: attachment; filename="Copy of 1501733944381-20170803 Dataload.xlsx"; size=23847; creation-date="Mon, 09 Oct 2017 12:39:28 GMT"; modification-date="Mon, 09 Oct 2017 12:38:40 GMT" Content-Transfer-Encoding: base64
... and code defined as...
my $emailfile = basename("$email"); my $tmpdir = "/tmp/$emailfile"; mkdir "$tmpdir"; my $parser = MIME::Parser->new(); $parser->decode_headers(1); $parser->output_dir("$tmpdir"); my $message = $parser->parse_open("$email");
... and variable $email defined as a full path and filename, I get a file created in /tmp/$emailfilename as
Copy of 150173.xls
Two things. The name is wrong and the suffix is wrong. I can live with the suffix being wrong but it does point to possibly the problem being with the content-type being something MIME::Parser doesn't fully recognise. Anyone come across anything like this? Anyone solve it?

Replies are listed 'Best First'.
Re: Problem with MIME::Parser and filenames
by tangent (Parson) on Oct 10, 2017 at 21:40 UTC
    $parser->decode_headers(1);
    I'm not sure if relevant, but from a quick look at the docs for MIME::Parser it says you should not use decode_headers() unless you really know what you are doing. Have you tried commenting out that line?
      Good idea. Didn't help though... exactly the same outcome.
        FWIW I sent myself a xlsx file with the same name as yours. It came in with the same Content-Type (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) and MIME::Parser saved the attached file correctly. So I don't think Content-Type is the problem. Could it be some type of filename length restriction - try adding something like this to your script:
        system('getconf NAME_MAX ' . $tmpdir);
Re: Problem with MIME::Parser and filenames
by huck (Prior) on Oct 11, 2017 at 02:07 UTC
Re: Problem with MIME::Parser and filenames
by choroba (Cardinal) on Oct 11, 2017 at 07:34 UTC
    Crossposted to reddit. It's considered polite to inform about crossposting to avoid unnecessary work of hackers who don't attend both the sites (in this case, how should the Monks know that you already got an answer at reddit?) Moreover, posting a link to reddit is easy and common.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,