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

I'm trying to write an app that will access a POP3 mailbox and grab ms word document attachments off of the messages in that mailbox. I can access the mailbox fine and get messages, but I'm failing at being able to extract the attachment from the body of the message. Any help on this would be fabulous. Thanks!

Replies are listed 'Best First'.
Re: Using MIME::Tools to save attachments
by Aragorn (Curate) on May 13, 2002 at 14:11 UTC
    You're not showing what you do have, but I recommend reading the MIME::Tools perldocs, and in particular the docs about the parts()/parts_DFS(), body(), body_handle() methods. The MIME::Tools library is a pretty hairy beast. Some code I've recently written to save email messages including attachments to a database:
    for my $p ($m->parts_DFS()) { # Skip 'container' parts next if $p->effective_type() =~ /^multipart/; $sth_insert_bdy->execute($key, $p->stringify_body(), $m->head->multipart_boundary || "", $p->head->mime_type || "", $p->head->mime_encoding || "", $p->head->recommended_filename || ""); }
    The $m variable is a MIME::Entity object with the message and the attachment(s).
      Thanks for the tipe on the parts/parts_DFS procedures. Unfortunately they havn't gotten me anywhere. The mail message I'm dealing with has the following structure:
      X-POP3-Rcpt: hidden@hidden.org Received: from hidden (hidden [111.111.111.111]) by hidden.org (8.10.2/8.10.2) with ESMTP id g441dFH07987; Fri, 3 May 2002 21:39:15 -0400 From: "Hidden" <hidden@hidden.org> To: <hidden@hidden.org> Cc: <hidden@hidden.org> Subject: Hidden Date: Fri, 3 May 2002 20:52:30 -0500 Message-ID: <000801c1f30e$5aaccb90$0c0210ac@hidden.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0009_01C1F2E4.71D6C390" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.3416 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 This is a multi-part message in MIME format. ------=_NextPart_000_0009_01C1F2E4.71D6C390 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit # message here ------=_NextPart_000_0009_01C1F2E4.71D6C390 Content-Type: application/msword; name="File.DOC" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="File.DOC" # encoded datafile here ------=_NextPart_000_0009_01C1F2E4.71D6C390--
      The message is stored in an array and is obtained using the following code. The array is then fed into the parser entity parse_data():
      use Mail::POP3Client; use MIME::Parser; $pop = new Mail::POP3Client( USER => "hidden", PASSWORD=> "hidden", HOST => "hidden" ); @message = ($pop->Head(1),"\n",$pop->Body(1)); my $parser = new MIME::Parser; $entity = $parser->parse_data(\@message);
      I then try and do anything with that entity and I get nothing. The code creates a .txt file in the run directory with a size of 0.
      Any ideas what I'm doing wrong?
      Thanks!
Re: Using MIME::Tools to save attachments
by Lanthade (Initiate) on May 13, 2002 at 05:03 UTC
    Additional information. I get the message body into an array and it prints out fine doing a:
    foreach $line (@body) { print "$line\n"; }
    It's the taking of that array and getting the attachment out of it that is the problem.
    Thanks!
      If we could see some more code, it would make it easier to help you out.

      -- Yes, I am a criminal. My crime is that of defyance.
Re: Using MIME::Tools to save attachments
by xaphod (Monk) on May 14, 2002 at 07:42 UTC
    This works for me:
    use MIME::Parser; # Create a new MIME::Parser object $parser = MIME::Parser->new(); # Specify the location we'll put our output $parser->output_dir("./"); # Change how nameless message-component files are named: $parser->output_prefix("msg"); # Open the full message open(MSG, "< msg-full"); # Passs the open file handle to our parser $parser->parse(\*MSG); # Close the file close(FH);
    --
    TTFN, FNORD

    xaphod