Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Retrieving email and decoding attachments

by jlongino (Parson)
on Sep 26, 2002 at 06:56 UTC ( #200826=note: print w/replies, xml ) Need Help??

in reply to Retrieving email and decoding attachments

This script should do what you want. I looked at several modules trying to find one that does everything, but lately I find that most mail related modules do one or two very specific things well, but for anything but the simplest tasks end up using more than one to get the job done. In this case, Mail::POP3Client is a good POP3 access tool but MIME::Parser is hard to beat for saving attachments to disk:
#!/usr/local/bin/perl use strict; use warnings; use Mail::POP3Client; use MIME::Parser; my $pop = new Mail::POP3Client( USER => "user", PASSWORD => "password", HOST => "pop3.server" ); ## for HeadAndBodyToFile() to use my $fh = new IO::Handle(); ## Initialize stuff for MIME::Parser; my $outputdir = "./mimemail"; my $parser = new MIME::Parser; $parser->output_dir($outputdir); my $i; ## process all messages in pop3 inbox for ($i = 1; $i <= $pop->Count(); $i++) { open (MAILOUT, ">pop3.msg$i"); $fh->fdopen( fileno( MAILOUT ), "w" ); ## write current msg to file $pop->HeadAndBodyToFile( $fh, $i ); close MAILOUT; ## MIME::Parser handles only one msg at-a-time open (MAILIN, "<pop3.msg$i"); ## flush all attachments this msg to ./mimemail dir using internal +filename my $entity = $parser->read(\*MAILIN); close MAILIN; }
BTW, this was done hastily and could use more error testing, could probably be done more simply/elegantly, but I leave that for the poster.

Does anyone know how to read the messages directly to MIME::Parser without having to write the intermediate files first? I've encountered this dilemma recently trying to combine other unrelated mail modules.


Replies are listed 'Best First'.
Re^2: Retrieving email and decoding attachments
by Anonymous Monk on Jun 13, 2007 at 19:44 UTC
    This works too and is a bit simpler.
    for ($i = 1; $i <= $pop->Count(); $i++) { print "$i \n"; my $msg; $msg = $pop->HeadAndBody( $i ); my $entity = $parser->parse_data($msg); }
      Wow! That was great! It works like a charm! Thank you!
Re^2: Retrieving email and decoding attachments
by Anonymous Monk on Dec 05, 2012 at 05:25 UTC
    Thanks, works well after all these years

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200826]
[thezip]: Ooops... I lied. I guess Cygwin is back. I'll just do a tail -f instead. Better. Sorry for the noise.
[Corion]: Once more, I'm looking for a sane client-side framework, but I guess these don't exist. Everything I look at either uses a weirdo home-grown templating language (like Angular in all its incarnations) or uses weirdo Javascript incarnations (like ...
[Corion]: ... Inferno.js, which uses ES2015) or uses some horrible amount of Javascript infrastructure before you can even render a single file.
[Corion]: I'd really like to create a dynamic frontend for my Google Keep clone, but so far, all the templating solutions seem to bring their own template language or require me to hand-code everything in (their own flavour of) Javascript. I'd like something ...
[Corion]: ... that can use templates from the HTML (via <script language="text/ template">... my template ...</script> and otherwise gets out of my way. But it seems that is not how the Javascript world is, currently :-/

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-03-27 18:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (321 votes). Check out past polls.