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

need a routine on IS7 (Perl 5.14.2 built for MSWin32-x64 and win32-x86)

1- download emails from pop3 server one by one,

2- examine contents an attachments

3- selectively save attachment files to a folder (keep original name)

4- delete the entry from pop server:

Here is the sample code I have but extracting attachments have been a challenge can anyone help. Thanks


#!perl # #Open Pop3 (Cell2email) Receives all Emails #Checks if they are destined for a cell phone Number #Saves the text in Send folder /waiting for Sm-SndRm.pl to #send via ftp to Cel phone for processing by Talk.pk #
use Mail::POP3Client; use Mail::Audit qw(Attach); use Email::MIME; require "tstLib.pl"; require "tstMain.pl"; SetVars(); # $ErrMsg =''; # ReadSetUp(); # ReadPopPar(); # my $line=''; $PopID ='xxxx'; $PopPWD='xxxx'; $PopSrvr='xxxxx'; $!=''; $pop = new Mail::POP3Client( USER => $PopID, PASSWORD => $PopPWD, HOST => $PopSrvr, , AUTH_MODE => 'PASS' ); for ($i = 1; $i <= $pop->Count(); $i++) { print "Message $i: -------------------------\n"; my $headerid = 1; my $From =''; my $FrmEM=''; my $To =''; my $Subj =''; my $mDate =''; my $iMail = $pop->Retrieve($i); my $mailRef= \@iMail ; my $rMail = Mail::Audit->new (data=>$mailRef); foreach ( $pop->Head( $i ) ) { #if ($_ =~ /^Date:.*([0-9]{1,2}\s[a-zA-Z]{3}\s[0-9]{2,4})/) { if ($_ =~ /^From:/ ) { $From =ltrim(substr($_,index($_,'From:')+5)) ; $FrmEM =substr($From,index($From,'<')+1,rindex($From,'>')-index($F +rom,'<')-1 ); $From =~ s/<|>// ; }; if ($_ =~ /^To:/i ) { $To =ltrim(substr($_,index($_,'To:')+3)) ; }; if ($_ =~ /^Date:.*([0-9]{1,2}\s[a-zA-Z]{3}\s[0-9]{2,4})/) { $mDate =ltrim(substr($_,index($_,'Date:')+5)) ; } if ($_ =~ /^Subject:/i ) { print "subj.: $_\n"; $Subj =ltrim(substr($_,index($_,'Subject:')+8)) ; }; }; my $BodyCoded= $pop->Retrieve( $i); my $Parsed = Email::MIME->new($BodyCoded); my $Text = handle_parts($Parsed); $Text=~s/\r//g ; $Text=~s/\n\n/\n/g ; my $mem = "From:$From\n\r To:$To\n\rDate:$mDate \n\rBody:$Text\n"; print $mem; SaveFl("temp/Eml/$i-text\.txt" , $mem); $headerid ++; } $pop->Close; exit; # sub handle_parts { my $part = shift; my $content_type = $part->content_type; print "** Cont-Type: $content_type \n"; # debug my $body = $part->body; if ($content_type =~ m#text/plain#) { $body; }elsif ($content_type =~ m#text/html#) { html2text($body); }elsif ($content_type =~ m#multipart/#) { for my $subpart ($part->parts) { my $text = handle_parts($subpart); # return $text if defined $text; } } $body} sub html2text { my $html = shift; # my $text = ... (left as an exercise) # return $text; return $html; }