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

The only thing I'm trying to do is take files that have been zipped and emailed to me, and save them in a folder on my server.
I've got the following hodgepodge of copy/pasted/glued code, but haven't been able to figure out how to finish the part where the attachment is saved.
Is there an easier way to do this? A search of the CPAN modules didn't pan out.
use Net::IMAP::Simple; use Email::Simple; use Email::MIME::Attachment::Stripper; my $username = "user"; my $password= "pass"; my $server = Net::IMAP::Simple->new( 'server' ); $server->login( $username,$password) || warn "Bad login!\n"; my $nmessages = $server->select( 'inbox' ); $number_of_messages = $nmessages; foreach my $msg ( 1 .. $number_of_messages ) { if ($server->seen( $msg )){ # print "This message has been read before...\n"; } else { print "New!"; my $email = Email::Simple->new( join '', @{$server->get( $ +msg )} ); my $stripper = Email::MIME::Attachment::Stripper->new($msg +); my Email::MIME $msg = $stripper->message; #my attachments = ..; This returns a list of all the attachments we f +ound in the message, as a hash of { filename, content_type, payload } +. my @attachments = $stripper->attachments; # print $email->header('Subject'), "\n"; # $data = $email->body, "\n"; # print $data; # print "\n"; } } wend; $server->quit(); exit;

Replies are listed 'Best First'.
Re: Save Email Attachment to File
by brian_d_foy (Abbot) on Jan 23, 2006 at 19:51 UTC

    For this sort of thing I like MIME:Parser which figures it all out for me. It parses the message and saves the attachments in the directory I give it.

    # extract the payload my $parser = MIME::Parser->new(); $parser->output_dir( $tmpdir ); $parser->parse_data( $message );
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
    A reply falls below the community's threshold of quality. You may see it by logging in.