in reply to mail attachment extraction

There are a lot of things here that would need or could be done differently - really a bit too much of them to insist now.

Anyway, I've tried to just a bit modify your code so that it would allow you to go ahead.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Email::Simple; use Email::MIME; use Mail::IMAPTalk; use Email::MIME::Attachment::Stripper ; use constant DUMP => '/home/jc'; my $server = '192.168.0.7'; my $imap_port = '143'; my $login = 'jc'; my $password = 'pass'; my $FolderName = 'INBOX'; print "******************\n"; print "* MAIL EXTRACT *\n"; print "******************\n\n"; print "Connecting to IMAP server at ".$server.":".$imap_port."...\n"; # open the imap connection using IMAP::Talk my $imap = Mail::IMAPTalk->new( Server => $server, Port => $imap_port, Username => $login, Password => $password, Separator => '.', RootFolder => 'Inbox', CaseInsensitive => 1) || die "Connection failed. Reason: $@"; # Select folder and get first unseen message $imap->select($FolderName) || die $@; my $MsgId = $imap->search('not', 'seen')->[0]; if ($MsgId) { # Get the enveloppe print "The message with ID ".$MsgId." has the following enveloppe +:\n"; my $MsgEV = $imap->fetch($MsgId, 'envelope')->{$MsgId}->{envelope} +; print "From: " . $MsgEV->{From}."\n"; print "To: " . $MsgEV->{To}."\n"; print "Subject: " . $MsgEV->{Subject}."\n"; print "Sender: " . $MsgEV->{Sender}."\n"; print "Continue ?\n"; getc(STDIN); # Get the message body structure my $MsgBS = $imap->fetch($MsgId, 'bodystructure')->{$MsgId}->{bodystructure}; print "The size of the message is ".$MsgBS->{Size}."kB\n"; print "Continue ?\n"; getc(STDIN); # Find imap part number of text part of message my $MsgTxtHash = Mail::IMAPTalk::find_message($MsgBS); my $MsgPart = $MsgTxtHash->{text}->{'IMAP-Partnum'}; # Retrieve message text body my $MsgTxt = $imap->fetch($MsgId, "body[$MsgPart]")->{$MsgId}->{bo +dy}; print "Full message : \n"; print "\n<-------------------------------------->\n"; print $MsgTxt."\n"; print "\n<-------------------------------------->\n"; print "Continue ?\n"; getc(STDIN); # Transform the content from IMAP:Talk into a MIME object using Em +ail:MIME my $parsed = Email::MIME->new($MsgTxt); print "Parsed content :\n". Dumper( $parsed) . "\n"; # display the MIME structure print "MIME structure :" . $parsed->debug_structure . "\n"; my $parts = $parsed->parts; print "Number of email parts : $parts\n"; my @parts = $parsed->parts; my $i=0; #foreach (@parts) { # print "Dumped email part $i:\n", # Dumper( $parts[$i] ), # "\n"; # $i++; #} my $decoded = $parsed->body; #print "body: " . Dumper( $decoded ) . "\n"; # 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 { die "This message consists of a single part.\n"; } die "Not an regular MIME part given to stripper:\n" if not ref $stripper; # The extraction method itself my @attachments = $stripper->attachments; # Display the resulting attachments hash $i=0; foreach (@parts) { my $j=0; foreach ($parts[$i]) { print "hash:\n" . Dumper( $attachments[$i]->{payload}) . " +\n"; $j++; } $i++; } # Close the IMAP connection $imap->logout(); # Save the attachments on the local disk foreach $i (@attachments) { my $file = 'test'.$i; #DUMP . @attachments->filename(1); print "cou"; open FILE, '>', $file or die $!; print FILE @attachments; close FILE; chmod 0644, $file; print "cou"; } } else { print "No new message in the mailbox\n" }

Hope that helps.

Replies are listed 'Best First'.
Re^2: mail attachment extraction
by phocean (Novice) on May 03, 2007 at 18:39 UTC
    Thank you for your nice corrections. I now can figure out where the problem precisely is. Imap:Talk through its fetch() method returns a structure. But Email::Mime is waiting for a string... I am trying now to find a way... I have tried a method parse_mode(ParseOption => $ParseMode) given by Imap::Talk, but without success for now... Or maybe should I use a different module ?
      I guess you don't really need Email::MIME explicitely.

      Once you got the message you want in $MsgTxt, just do
      my $stripper = Email::MIME::Attachment::Stripper->new( $MsgTxt); my @attachments = $stripper->attachments;
      and you should have what you want.
        Sorry but I am still trying and can't get to anywhere... I definitely can't give $MsgTxt to Attachment::stripper, because it needs an Email::Mime object. The parsing does not go well with Email::Mime : it is all messed up. Somehow I can't get IMAP::Talk to give me something in the proper format, but this is beyond my understanding... I am really exhausted, I have been 2 weeks on that :(