Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I've looked through the Super Search for examples of receiving attachments with MIME::Parser. I've been through the docs for Parser, MIME::Parser::Filer, and Parser::Head and for the life of me I cannot get the recomended filename for an attached file.

Anyone know what is causing the script to fail to find the suggested filename?

These are the errors I get with the various methods:
Method 1:
Can't call method "path" on an undefined value at ./ line 59.
Method 2 & 3:
Use of uninitialized value in concatenation (.) or string at ./ line 67

I have the three methods I've seen and tried below.

much thanks in advance.

#!/usr/bin/perl -w use Mail::POP3Client; use MIME::Parser; use MIME::Head; use strict; my $pop=new Mail::POP3Client( USER=>"xxxx", PASSWORD=>"xxxx", HOST=>""); my $msgcount=$pop->Count(); my $msgnum; for $msgnum (1 .. $msgcount){ print "message number: $msgnum\n"; # get headers my $header=$pop->Head($msgnum); # get body my $body=$pop->Body($msgnum); # get whole message my $message=$pop->HeadAndBody($msgnum); my $parser=new MIME::Parser; # disable output to file/dir $parser->output_to_core(1); my $entity = $parser->parse_data($message); # get the number of parts of the message (if multipart) my $num_parts = $entity->parts; my @parts = $entity->parts; my $bodydata; if ($num_parts > 0) { # more than one part, mime-ahoy? my $part; for $part(@parts){ # get mime type my $type=$part->mime_type; my $bhandle=$part->bodyhandle; # special treatment for text and html if (($type =~ /text/i) || ($type=~ /html/i)){ if (my $io = $part->open("r")) { while (defined($_ = $io->getline)) { $bodydata .= $_; } $io->close; } print $bodydata,"\n"; } else { # must be an attachment of sorts or # at least not text/html my $content .= $bhandle->as_string if defined $bhandle; # method 1 # my $file_name = $entity->head->recommended_filename || # $entity->bodyhandle->path; # method 2 #my $mimehead=MIME::Head->new; #my $file_name=$mimehead->recommended_filename; # method 3 #my $file_name = $bhandle->{MB_Path}; print "path: $file_name\n"; # dump attachment to outfile # (works when name is specified) open(OUT,"> /path/to/attachments/$file_name"); # is this needed? local $/ = undef; print OUT $content,"\n"; close(OUT); } } } else { # plaintext or html! if (my $io = $entity->open("r")) { while (defined($_ = $io->getline)) { $bodydata .= $_; } $io->close; } print $bodydata,"\n"; } $entity->purge; } exit;

In reply to Getting recommened_filename from MIME::Parser by thatguy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2018-03-17 15:16 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (224 votes). Check out past polls.