I used Redemption together with Win32::OLE module with strawberry perl to parse emails out of a pst file and save attachements and body to disk. Here is some rough code to do this #!perl
use strict;
use Win32::OLE;
use Win32::OLE::Const;
use Win32::OLE::Const 'Microsoft Outlook';
use Data::Dumper;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use IO::File;
use Win32::OLE::Variant;
my $outlook;
eval {
$outlook = Win32::OLE->GetActiveObject('Outlook.Applic
+ation')
};
if ($@ || !defined($outlook)) {
$outlook = Win32::OLE->new('Outlook.Application', sub
+{$_[0]->Quit;})
or die("Cannot create outlook\n");
}
my $namespace = $outlook->GetNamespace('MAPI');
# get the proper pst file
my $folder = $namespace->{'Folders'}{'somepst'};
# now get the folder within the pst file
my $folder2 = $folder->{'Folders'}{"dailyfolder"};
# get all the messages
my $msgs = $folder2->{Items};
my $ncon = $msgs->{Count};
print "number of contacts - $ncon\n\n"; # this works
my $app = new Win32::OLE('Redemption.SafeMailItem');
my $con;
my $at;
my $fn;
my $obase = 'C:\strawberry\perl\daily\\';
my $out;
my $po = {};
foreach my $ii (1 .. $ncon) {
$con = $msgs->Item($ii);
$app->{'Item'} = $con;
#print_body($app->{Body});
#find_data($app->{Body},$ii,$t);
$at = $app->{'Attachments'};
print "\n\n Attachment count\n";
print $at->{Count} . "\n";
$fn = $at->Item(1)->{FileName};
print "filename is $fn\n" if $fn;
$out = $obase . $fn;
if(!-e $out) {
$at->Item(1)->SaveAsFile($out);
print "SAVING ATTACHMENT TO $out\n";
}
$po->{$out}++;
}
my $z = Archive::Zip->new();
my $i = 1;
my $fh;
my $fl;
my $exf = {};
my $he = 0;
foreach my $zf (keys %$po) {
warn("processing $zf\n");
unless( $z->read($zf) == AZ_OK) {
die("ERROR READING $zf\n");
}
foreach my $m ($z->memberNames()) {
warn("processing member $m\n");
if(! -e "$obase$m") {
$fh = IO::File->new("$obase$m",">") or
+ die($!);
$fl = $z->memberNamed($m);
$fl->extractToFileHandle($fh);
$fh->close() if $fh;
if($m =~ /\.xls$/) {
$exf->{"$obase$m"}++;
$he++;
}
next;
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|