Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Program to detach e-mail attachment


On Wed, Jul 06, 2005 at 09:42:36 +1000, Peter Rundle wrote:

> looking for some pointers on getting together some code to auto process 
> incoming e-mails, detach their attachments and save them as files. The 

[snip]
 
> anyone have some examples, web pages to go visit...

Something like this should do what you want:

    #!/usr/bin/perl -w

    use strict;
    use MIME::Parser;

    my $parser = new MIME::Parser;
    $parser->output_dir("$ENV{HOME}/attachments");
    $parser->parse(\*STDIN) or die "parse failed\n";

MIME::Parser::parse() returns a MIME::Entity which can be used to
process each attachment separately, like this:

    $entity = $parser->parse(\*STDIN) or die "parse failed\n";
    # now deal with each part
    foreach $part ($entity->parts)
    {
        # do what you want with this part
    }


Cheers,

John
-- 
As David pointed out, soon-to-be postgrad couldn't install software. Sounds
like a good idea to me, particularly at a uni. I sometimes think giving
them keyboards is being overly generous.
            -- Craig 'Stevo' Stephenson