Barbie,

I haven't done much Perl with Outlook, but from the experience I have had, it seems like a wrapper module could be written to make it simpler and more Perl-like. In fact, there's at least two ways to interface with Outlook. The first is doing a Win32::OLE->new('Outlook.Application'), which your module uses. The other way is to use CDO via Win32::OLE->new('MAPI.Session').

Just for an example of where your module could be used, I once wanted to extract the SMTP headers out of all messages in a given public folder. I started using Outlook.Application:

==========================
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
use Data::Dumper;

my $outlook = Win32::OLE->new('Outlook.Application')
    or die "Failed Opening Outlook.";

my $namespace = $outlook->GetNamespace("MAPI");
my $folder = $namespace->Folders("Public Folders")->Folders(
    "All Public Folders")->Folders("Junk Mail")->Folders("Bad");
my $items = $folder->Items;

for my $itemIndex (1..$items->Count)
{
    my $message = $items->item($itemIndex);
    next if not defined $message;
    #print Dumper($message);
    print $message->{SenderName}."\n";
    print $message->{To}."\n";
    print $message->{Subject}."\n";
    print $message->{Body}."\n";
}
==========================

This isn't that bad, but the folder access could be simplified. The real problem was that I couldn't figure out how to access the SMTP headers from the $message object. Data::Dumper didn't show them, and some references on the Internet suggested that this must instead be done with CDO. Here was my new code:

==========================
# Processes e-mail in Exchange folder using MS CDO.
# David Manura, created 2003-12-27.
#
# Apparently, the SMTP headers are available only via CDO (not MAPI)
#
# Useful references:
#   HOWTO: Access SMTP Headers of a Message Using CDO (1.x)
#   http://support.microsoft.com/?kbid=194870
#
#   HOW TO: Create public folders in Exchange with .NET
#   http://www.dotnet247.com/247reference/msgs/23/115872.aspx
#
#   HOWTO: Access SMTP Headers of a Message Using CDO (1.x)
#   http://support.microsoft.com/?kbid=194870
#
#   (RhetTbull) Re: Win32 - M$ Outlook and Perl.   (not using CDO)
#   http://perlmonks.thepen.com/(RhetTbull)%20Re:%20Win32%20-%20M$
#       %20Outlook%20and%20Perl..html

use strict;
use Win32::OLE;
use Win32::OLE::Variant;
use Win32::OLE::Const;
#use Data::Dumper;

Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);

my $cdo_const = Win32::OLE::Const->Load('Microsoft CDO.*Library')
    or die;

my $session = Win32::OLE->new('MAPI.Session') or die;

$session->Logon();

# get "Public Folders/All Public Folders/Junk Mail/Bad"

my $store = $session->infostores->item("Public Folders");

my $fld = $store->Fields->item(0x66310102, undef);
my $id = $store->ID;
my $pfid = $fld->Value;
my $folder = $session->GetFolder($pfid, $id);

$folder = $folder->Folders->Item("Junk Mail")->Folders->Item("Bad");

my $msgcol = $folder->Messages();

for my $idx (1..$msgcol->Count()) {
    my $message = $msgcol->item($idx);
    #my $subj = $message->Subject();
    my $header = $message->Fields(
        $$cdo_const{CdoPR_TRANSPORT_MESSAGE_HEADERS})->Value();

    print $header;
}

$session->Logoff();
==========================

The difficult part was finding out how to obtain the folder object. It's ugly.

Here's how I would have liked to write that code:

===========================
use strict;
use MyOutlookModule;
#use Data::Dumper;

my $outlook = new MyOutlookModule();

my $folder = $outlook->get_folder(
    "Public Folders/All Public Folders/Junk Mail/Bad");

foreach my $message (@{$folder->messages()}) {
    #my $subj = $message->subject();
    my $header = $message->header();

    print $header;
}
===========================


-davidm



Barbie wrote:


-----Original Message-----
From: Ed Summers [mailto:[EMAIL PROTECTED]

On Wed, Jan 21, 2004 at 02:00:40PM +0000, Barbie wrote:

1) Is this a distribution worth uploading to CPAN?

Yes! There was a recent discussion at chicago.pm [1] at how to get at .pst files.


The module uses an active Outlook, as in my situation the Exchange Server is remotely located.


2) Is Mail::Outlook a suitable namespace or are there

better suggestions?


In an ideal world your module would plug into the Mail::Box framework somehow. But I'm not even sure if Outlook Express files are functionally different from vanilla Outlook files...exit stage left.


My first thought was to integrate it into Mail::Box, but because of the OLE side of things, that doesn't work. Plus Outlook and Outlook Express are VERY different beasts. The API to both are different. I had hoped my module would be able to talk to an active OE, but alas no :( I may look into adding that functionality, but seeing as there are already modules to read .dbx files, it's not something I think is needed.

In most situations I've come across Outlook files are generally located on a remote server, whereas Outlook Express files are located locally.


[1]

http://mail.pm.org/pipermail/chicago-talk/2004-January/000800.html


Interesting reading. I have not come across a .pst file, so maybe that's an extension for the future ... or someone else :)

Barbie.




Reply via email to