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

I am woundering if there is a way to extract MIME encoded images from a linux mail file in /var/spool/mail/username file. I would like to use perl to iterate throgh messages and extract the atachments. Is there a way to do this? I thank you in andvance. I forgot to add the reason for this. Spam Assasin is converting the atachments of my mail (some of it) to mime encoded text in the body. I would like to grab that string and generate the file.
  • Comment on Extracting MIME Encoded pictures from email

Replies are listed 'Best First'.
Re: Extracting MIME Encoded pictures from email
by davorg (Chancellor) on Nov 04, 2004 at 16:00 UTC

    Email::MIME is the simplest solution I've found for extracting attachments from MIME messages.

    And Email::Folder looks like what you want to parse individual messages out of a mailbox.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Extracting MIME Encoded pictures from email
by gellyfish (Monsignor) on Nov 04, 2004 at 16:14 UTC

    I must have read davorg's mind. To follow on from his suggestion you could try something like:

    use strict; use warnings; + use Email::MIME; use Email::Folder::Mbox; + my $box = Email::Folder::Mbox->new('/home/jonathan/mail/spam'); while ( my $mail = $box->next_message ) { my $mess = Email::MIME->new($mail); + foreach my $part ($mess->parts()) { if ($part->content_type() =~ /image/i ) { open OUT, ">", "tmp/" .$part->filename or next; print OUT $part->body(); close OUT; } } + }
    You might find this noisy if you have broken MIME messages in your mailbox (like I have in the collection of spam above). In real life you might want to be a bit more defensive as to how you use the filename from the message.

    /J\

Re: Extracting MIME Encoded pictures from email
by Mutant (Priest) on Nov 04, 2004 at 15:41 UTC
    There's a bunch of modules to do this. Have a look at some of these.
Re: Extracting MIME Encoded pictures from email
by bart (Canon) on Nov 04, 2004 at 16:21 UTC
Re: Extracting MIME Encoded pictures from email
by pg (Canon) on Nov 04, 2004 at 15:42 UTC

    If you are interested in rolling it yourself, all what you need is MIME::Base64. Read that part in line by line, decode them, and write out to a local binary file (assume you have no problem to capture the email.)