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

Hi, By using Mail::POP3Client we were able to download mails from our mail server (qmail). This downloaded mail was given EMail::MIME::Attachment::Stripper to strip the attachments. But for one particular mail (mentioned below) we were not able to strip the attachment from it. Any hint on this?

Return-Path: <noc@specusa.com> Delivered-To: sales7_9707@sales.spectramedi.com Received: (qmail 2678 invoked from network); 2 Dec 2009 11:44:15 -0500 Received: from unknown (HELO pns.specusa.com) (204.15.236.5) by cms.spectramedi.com with SMTP; 2 Dec 2009 11:44:15 -0500 Received: from Canon0ADB1C.spectramedi.com (unknown [204.15.236.3]) by pns.specusa.com (Postfix) with SMTP id 81A16179B8 for <sales7_9707@sales.spectramedi.com>; Wed, 2 Dec 2009 11:41:51 + -0500 (EST) X-Priority: 1 (Highest) From: "SpectraMedi" <noc@specusa.com> To: "sales_7" <sales7_9707@sales.spectramedi.com> Subject: Attached Image Date: Wed, 02 Dec 2009 11:46:09 -0500 Message-Id: <20091202114609.000c.CanonTxNo.1386@Canon0ADB1C.spectramed +i.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="AHNJAMACDCDIDADFDGGBAAALCOAJ" --AHNJAMACDCDIDADFDGGBAAALCOAJ Content-Type: application/pdf; name="1386_001.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="1386_001.pdf" JVBERi0xLjIKJSDi48/TCjEgMCBvYmoNPDwNL1R5cGUgL1BhZ2UNL01lZGlhQm94IFswID +AgMTIy ...

Readmore and Code tags added by GrandFather. Email data truncated to allow tags.

Replies are listed 'Best First'.
Re: Stripping attachment by EMail::MIME::Attachment::Stripper
by gmargo (Hermit) on Dec 03, 2009 at 17:36 UTC
Re: Stripping attachment by EMail::MIME::Attachment::Stripper
by zentara (Cardinal) on Dec 03, 2009 at 13:20 UTC
    ... when you put a raw mail header from a successful strip, next to this bad one above.....what differences do you see? ..... for example...look at your bad raw mail with a binary file editor, and see if there are unprintable chars being sent..... maybe the pdf has a spurious 0x0 in it ?

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Stripping attachment by EMail::MIME::Attachment::Stripper
by gmargo (Hermit) on Dec 03, 2009 at 22:08 UTC

    Here's a possible hack to extract those single-part attachments. It works for this example, but your mileage may vary.

    # $mail is content of message as string. my $stripper = Email::MIME::Attachment::Stripper->new($mail); my $msg = $stripper->message; my @attachments = $stripper->attachments; # Add fake text so there is more than one part. # How do we determine if we need to add fake part? # check content disposition if ($msg->parts == 1 && scalar(@attachments) == 0) { my $part1 = ($msg->parts)[0]; # These will be Email::MIME objects, + too. my $disp = $part1->header("Content-Disposition"); if ($disp && $disp =~ /attachment\s*;/i) { my @fake_bonus_parts = ( Email::MIME->create( attributes => { content_type => "text/plain", disposition => "inline", charset => "US-ASCII", }, body => "Hello, World!",), ); $msg->parts_add(\@fake_bonus_parts); $stripper = Email::MIME::Attachment::Stripper->new($msg->as_st +ring); $msg = $stripper->message; @attachments = $stripper->attachments; } } # Continue attachment processing....

      Thanks for your reply. Is there any other modules, we can strip all the attachments of any type mail.

        .... is there any other?..... here is an old note i have from Roger, maybe it will help
        #!/usr/bin/perl use strict; use IO::File; use Mail::Message::Attachment::Stripper; #by Roger of Perlmonks #There are many ways to do this. I recommend the following #modules: Mail::Box to manage the mail box and #Mail::Message::Attachment::Stripper to strip (multiple) #attachments. #Batch mode - I would use the powerful Mail::Box module to #parse each user's mailbox, and for each of the user's mail #messages, extract the (folder) name of the user from the #email address line, strip all the attachments with #Mail::Message::Attachment::Stripper (or similar module), #and put the attachments into user's folder. #Procmail mode - When email message comes in, load it into #a scalar, extract the folder name from email address, and #use Mail::Message::Attachment::Stripper to strip the #attachments. You might also want to delete the attachment #from the original email, just save the email message, and #append some sort of notice to the modified mail message #that their mail attachments are saved under their user #directories. # load the mail message into $mail # extract user folder path into $user_folder #... my $m = Mail::Message::Attachment::Stripper->new($mail); my @attachments = $m->attachments; foreach my $a(@attachments) { next if $a->{content_type} !~ /jpe?g|gif/i; # ignore non-jpg/gif atta +chemnts my $f = new IO::File "/home/$user_folder/" . $a->{filename}, "w" or die "Can not create file!"; print $f $a->{payload}; }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku