Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Extracting email messages and attachments from a microsoft outlook .pst file

by pankaj_it09 (Scribe)
on Apr 08, 2011 at 12:15 UTC ( [id://898333]=perlquestion: print w/replies, xml ) Need Help??

pankaj_it09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

My goal is to find a specific information(using regular expressions) from a Microsoft Outlook .pst file.

Is there a way to do that ?

Replies are listed 'Best First'.
Re: Extracting email messages and attachments from a microsoft outlook .pst file
by marto (Cardinal) on Apr 08, 2011 at 12:34 UTC
Re: Extracting email messages and attachments from a microsoft outlook .pst file
by wfsp (Abbot) on Apr 08, 2011 at 13:49 UTC
    If Outlook is available you may be better off using that.

    This is a skeleton of something I've used. You'd need to adjust the mailbox name.

    #!/usr/bin/perl use v5.12.2; use warnings; use strict; use Win32::OLE; my $mail = parse_inbox(); sub parse_inbox{ # use existing instance if Outlook is already running, or launce a n +ew one my $ol; eval {$ol = Win32::OLE->GetActiveObject('Outlook.Application')}; die "Outlook not installed" if $@; unless (defined $ol) { $ol = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Outlook"; } my $mailbox = seekFolder( $ol->Session, 'Mailbox - John Sharpe' ); # adjust to suit my $folder = seekFolder( $mailbox, 'Inbox' ); my (%table); for (my $i = 1; $i <= $folder->Items->Count; $i++) { my $sender_name = $folder->Items->Item($i)->SenderName; my $subject = $folder->Items->Item($i)->Subject; my $body = $folder->Items->Item($i)->Body; say $subject; # process # store something in table } return \%table; } sub seekFolder { my $obj = shift; my $target = shift; for (my $i = 1; $i <= $obj->Folders->Count; $i++) { if ($obj->Folders->Item($i)->Name eq $target) { return $obj->Folders->Item($i); } } return; }
    Tested with Outlook 2003. There is a snag in that after the script is started Outlook complains that something is trying to look at the contact list. You need to click a checkbox and OK.
      The code runs in Outlook 2007 but the count it calculates is 0 even though the "Inbox" has a large amount of messages.

      Hence the below loop is not executed :

      for (my $i = 1; $i <= $folder->Items->Count; $i++) {
      Is perl version 5.12.2 required ?

      I commented the perl version line and then ran the code which showed the below error:

      Can't call method "Folders" on an undefined value at outlook.pl line 57.
        Have you got the correct mailbox name? It should be exactly as Outlook displays it. You could add a debug print statement in the seekFolder subs for loop to see which folders are found. Something like
        print "Folder:", $obj->Folders->Item($i)->Name;
        You don't need 5.12.2, when I first used it I was on 5.8.8.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://898333]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-03-28 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found