Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to use Email::MIME::Attachment::Stripper to download email attachments?

by pankaj_it09 (Scribe)
on May 06, 2014 at 12:53 UTC ( [id://1085170]=perlquestion: print w/replies, xml ) Need Help??

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

When I run the below code the attachments are not downloaded .

The OS is Windows 7 and Perl version is 5.16.3

use Net::IMAP::Client; use Email::MIME; use Email::MIME::Attachment::Stripper ; use Data::Dumper; my $imap = Net::IMAP::Client->new( server => 'imap.gmail.com', user => 'casetest@gmail.com', pass => 'go123', ssl => 1, # (use SSL? default no) # ssl_verify_peer => 1, # (use ca to verify se +rver, default yes) # ssl_ca_file => '/etc/ssl/certs/certa.pm', # (CA file used for ve +rify server) or # ssl_ca_path => '/etc/ssl/certs/', # (CA path used for SS +L) port => 993 # (but defaults are sane +) ) or die "Could not connect to IMAP server"; # everything's useless if you can't login $imap->login or die('Login failed: ' . $imap->last_error); # select folder $imap->select('INBOX'); # fetch all message ids (as array reference) my $messages = $imap->search('ALL'); foreach $msg (@$messages) { my $data = $imap->get_rfc822_body($msg); my $parsed = Email::MIME->new($data); #print "Parsed content :\n". Dumper( $parsed) . "\n"; my $parts = $parsed->parts; print "Number of email parts : $parts\n"; my @parts = $parsed->parts; # Give the Email MIME content to Attachment::Stripper for extracti +on my $stripper; if ($parts > 1) { $stripper = Email::MIME::Attachment::Stripper->new($parts[1]); } else { next; } # The extraction method itself my @attachments = $stripper->attachments; # Save the attachments on the local disk foreach my $att ( @attachments ) { my $file = $att->{filename}; open my $fh, '>', $file or die $!; print $fh $att->{payload}; close $fh; chmod 0644, $file; } } # Close the IMAP connection $imap->logout();
  • Comment on How to use Email::MIME::Attachment::Stripper to download email attachments?
  • Download Code

Replies are listed 'Best First'.
Re: How to use Email::MIME::Attachment::Stripper to download email attachments?
by marto (Cardinal) on May 06, 2014 at 13:03 UTC
      Yes I took the code from one of those posts?

      I tried debugging but unable to solve the problem.

        How did you debug this? What did you notice? There doesn't look like a lot of debugging code here, even less than the examples I linked to.

        When posting it's a good idea to tell (or even better show) how you've tried to resolve the problem, but we've been through this many times over the years, haven't we?

        You seriously want to take a step back and debug this, with focus on the part of your code which actually calls Email::MIME::Attachment::Stripper. Consider also that code posted elsewhere may not be generic and suit someones specific requirement.

Re: How to use Email::MIME::Attachment::Stripper to download email attachments?
by moritz (Cardinal) on May 06, 2014 at 12:58 UTC
      I gave like this -->

      $stripper = Email::MIME::Attachment::Stripper->new($parsed);
      How to run for the whole email ?
Re: How to use Email::MIME::Attachment::Stripper to download email attachments?
by pankaj_it09 (Scribe) on May 06, 2014 at 13:53 UTC
    I put the below code. It seems to work.
    ###### Attachment Parse Here ### Create a new parser object: my $parser = new MIME::Parser; ### Tell it where to put things: $parser->output_under("I:/test"); ### Parse an in-core MIME message: my $entity = $parser->parse_data($data);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1085170]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found