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

I know this question has been asked before but I still am not understanding how to acomplish this task. I have downloaded the message using Mail::POP3Client. The email comes with text in the body as well as three attachments. The single attachment I want will always be named the same way so it seems this should not be hard to acomplish. I have looked at code to use MIME::Parser and also Email::MIME::Attachment::Stripper but I haven't gotten either of these to work. What method do you suggest using and could you give an example? Here is the code I was attempting to use to get the attachment using Email::MIME::Attachment::Stripper
#!/usr/bin/perl use Mail::POP3Client; use Email::MIME; use Tie::File; use Email::MIME::Attachment::Stripper; my $pop_user = "user@server.com"; my $pop_pass = "password"; my $pop_server = "pop.server.com"; $pop = new Mail::POP3Client( USER => $pop_user, PASSWORD => $pop_pass, HOST => $pop_server, AUTH_MODE => 'PASS'); $message = $pop->Body(1); my $parser = Email::MIME->new($message); my @parts = $parser->parts; print @parts[1]; $stripper = Email::MIME::Attachment::Stripper->new(@parts[1]); my @attachments = $stripper->attachments; print @attachments[0]; $pop->Close();

Replies are listed 'Best First'.
Re: Extracting Attachment from Email
by zwon (Abbot) on Jan 14, 2010 at 18:58 UTC
    use strict; use warnings; use File::Slurp qw(slurp); use Email::MIME; my $mail = slurp('sample.eml'); my ($part) = grep { $_->filename && $_->filename eq 'archive.tgz' } Em +ail::MIME->new($mail)->parts; print $part->body if defined $part;