in reply to Re^6: mail attachment extraction
in thread mail attachment extraction

Well... Here is the code that works for me, I've just changed the auth data for me to work and have now commented out everything I think is irrelevant:

#!/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}->{bodystruct +ure}; 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}; my $i=0; # Give the Email MIME content to Attachment::Stripper for extracti +on 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" }
As said, this works for me. Might be that we have different version of Email::MIME::Attachment::Stripper; what I have is version 1.313, Perl 5.8.8.

Replies are listed 'Best First'.
Re^8: mail attachment extraction
by phocean (Novice) on May 06, 2007 at 20:31 UTC
    Thank you for giving this code, but there is something definitely weired... I pasted your code as it is and it always complain with "Need a message..." I use the same version of perl and Attachment::stripper as you...
      Hmmmm... Are you sure you actually get a message out of your Inbox, so that $MsgTxt isn't empty ?

      Just insert
      print Dumper( $MsgTxt );
      where that assignment is and you should know. I've noticed you're always looking fo the newest unseen message - maybe you just have run out of 'amunition' and there is no more unseen message to cope with?

      Maybe it's better to search an message that you know for sure that exists and retrieve it during testing via uid, like
      my $MsgId = $imap->search('uid', '257283')->[0];


        What I get when I execute the script (the attachment is a text file, so it looks alright in $MsgTxt...):
        ****************** * MAIL EXTRACT * ****************** Connecting to IMAP server at 192.168.0.7:143... Use of uninitialized value in concatenation (.) or string at /home/jc/ +.eclipse/attachement_extract/mail_extract5.pl line 49. The size of the message is kB Continue ? Use of uninitialized value in concatenation (.) or string at /home/jc/ +.eclipse/attachement_extract/mail_extract5.pl line 58. $VAR1 = 'Return-Path: <test@mcm> X-Original-To: jc@mcm Delivered-To: jc@mcm Received: from [10.8.0.6] (unknown [10.8.0.6]) by debian (Postfix) with ESMTP id DD7F3E6B02 for <jc@mcm>; Wed, 2 May 2007 12:11:30 +0200 (CEST) Resent-From: test <test@localdomain> Resent-To: jc@mcm Resent-Date: Sun, 6 May 2007 17:01:45 +0200 Resent-Message-Id: <463DEDD9.2040003@localdomain> Resent-User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10 +) Gecko/20060911 SUSE/1.5.0.10-1.1 Thunderbird/1.5.0.10 Resent-From: test <test@localdomain> Resent-To: jc@mcm Resent-Date: Sun, 6 May 2007 16:02:36 +0200 Resent-Message-Id: <463DDFFC.5050401@localdomain> Resent-User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10 +) Gecko/20060911 SUSE/1.5.0.10-1.1 Thunderbird/1.5.0.10 Resent-From: test <test@localdomain> Resent-To: jc@mcm Resent-Date: Sun, 6 May 2007 15:21:56 +0200 Resent-Message-Id: <463DD674.4050109@localdomain> Resent-User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10 +) Gecko/20060911 SUSE/1.5.0.10-1.1 Thunderbird/1.5.0.10 Message-ID: <463DD5F1.6080302@localdomain> Date: Sun, 06 May 2007 15:19:45 +0200 From: test <test@localdomain> User-Agent: Thunderbird 1.5.0.10 (X11/20060911) MIME-Version: 1.0 To: jc@mcm Subject: test X-Enigmail-Version: 0.94.2.0 Content-Type: multipart/mixed; boundary="------------060502080004010203030303" This is a multi-part message in MIME format. --------------060502080004010203030303 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit --------------060502080004010203030303 Content-Type: text/plain; name="text" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="text" texte fichier --------------060502080004010203030303-- '; Need a message at /home/jc/.eclipse/attachement_extract/mail_extract5. +pl line 64