#!/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 = 'XXX'; my $imap_port = '143'; my $login = 'YYY'; my $password = 'ZZZ'; 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 # ... # 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}->{body}; my $i=0; # Give the Email MIME content to Attachment::Stripper for extraction my $stripper =Email::MIME::Attachment::Stripper->new($MsgTxt ); my @attachments = $stripper->attachments; print Dumper( @attachments ); exit; # Display the resulting attachments hash #... # Close the IMAP connection $imap->logout(); # Save the attachments on the local disk #... } else { print "No new message in the mailbox\n" }