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

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

Replies are listed 'Best First'.
Re^11: mail attachment extraction
by Krambambuli (Curate) on May 07, 2007 at 08:33 UTC
    Well:
    ... 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 ?
    Seems you have an empty message there, since size is undef ? Check all the warnings you get, and correct the underlying problems before continuing. One step at time: check that you're message is ok, check that what you get is ok, insert print statements to assist you in debugging. Don't step forward to the next part of your program till you make sure that till that point everything is fine.
      hi there ;) I just found the script (I want to use it for some automatic email-stuff) and I still have (after trying debugging & prints for 100 times ^_~) the problem mentioned above... the mail I use for testing is nothing special... a rtf as an attachment and a test subject a "test" as a message together with the normal stuff like name,telephonenumber etc... Can somebody please tell me where the mistake is :( ? OS is WindowsXP in this case (German version below ... the Path just links to my projectfolder but the script only creates HASH-files xP no .txt or something like that :P) oh did ya notice that my "how do I name this project" sucks,too :>?

      edit says: I just added the print Dumper( $MsgTxt ); which results in
      $VAR1 = 'Received: by mymailserver id <01C81A05.60F5A3EC@mymailserver>; Mon, 29 Oct 2007 09:26:18 +01 +00
      and there ARE unread messages in the mailbox xP if there aren't any the script tells me that I should get an unread one ;)
      here are the erros I get:
      MsgBS -->HASH(0x1e6bdb8)<-- Use of uninitialized value in concatenation (.) or string at C:\Dokume +nte und Einstellungen\holzner.s\Eigene Dateien\perlmail\perlguard_gu_ +version.pl line 55. at C:\Dokumente und Einstellungen\holzner.s\Eigene Dateien\perlmail\p +erlguard_gu_version.pl line 55 The size of the message is kB Continue ? Use of uninitialized value in concatenation (.) or string at C:\Dokume +nte und Einstellungen\holzner.s\Eigene Dateien\perlmail\perlguard_gu_ +version.pl line 64. at C:\Dokumente und Einstellungen\holzner.s\Eigene Dateien\perlmail\p +erlguard_gu_version.pl line 64 Full message : <emailtext & info about attachment xP> Number of email parts : 2 hash: $VAR1 = undef; + hash: $VAR1 = undef; + cou cou cou cou
      here is the code I use at the moment (atm only with one "extra-print" I delete the other prints again because I think the main problem is focused at the MailBS-point):
      #!/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 => 'C:\\Dokumente und Einstellungen\\holzner\.s\\Eig +ene Dateien\\perlmail\\'; my $server = 'mymailserver'; my $imap_port = '143'; my $login = 'myacc'; my $password = 'mypwd'; print "******************\n"; print "* MAIL EXTRACT *\n"; print "******************\n\n"; my $FolderName = 'THO'; 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}->{body +structure}; print "MsgBS -->". $MsgBS. "\<--\n"; 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 Dumper( $MsgTxt ); 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" }


      ######
      I hope someone can help me :( I'm stuck and I have tried to solve this problem by myself many (and I mean many ._.") times =( ), so please ;), lend me your power ;)

      Bye for now,
      Shinama
      <Sebastian>