john.tm has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a script that reads thru an email folder and if the subject line starts with 'test' it extracts the mail body to a txt file. I would like it to then move the all emails to another folder. but while the script extracts from all the emails in the folder, i can only get it to move one email, not all of them. i get warning Use of uninitialized value in pattern match (m//) on ~ /^test /) ..... line
#!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $filename = 'c:\\test.txt' ; open(FH,"> $filename") or die ("cannot open $filename"); my $outlook = Win32::OLE->new('Outlook.Application') or die "Failed Opening Outlook."; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->Folders("testmail")->Folders("test");#->Folde +rs; ("Junk Mail")->Folders("Bad"); my $tofolder = $namespace->Folders("testmail")->Folders("testout");#-> +Folders; ("Junk Mail")->Folders("Bad"); my $items = $folder->Items; for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); if( $message->{Subject} =~ /^test /){ print $message->{Subject}."\n"; print FH $message->{Body}; # $message->Move($tofolder); only moves one message } } close(FH);

Replies are listed 'Best First'.
Re: perl to move outlook emails to another folder
by stevieb (Canon) on Nov 26, 2014 at 01:32 UTC

    I can't test this, and I've never used that module, but could it be that you're not closing the file upon each iteration and using the same file throughout? Can you tell which message it is moving (ie. first or last)?

    Just a thought off the top of my head.

    -stevieb