Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Win32::OLE Outlook help requested

by bassplayer (Monsignor)
on Nov 28, 2002 at 00:40 UTC ( [id://216212]=perlquestion: print w/replies, xml ) Need Help??

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

Good Afternoon Fine Monks,

I am forced to use Oulook 2000 at work. I suddenly started receiving a TON of SPAM and my administrator told me to send him the message headers from the spam so that he could get the Return-Path out of it and use it to bounce the offending email at the server level.

Not wanting to waste a bunch of time, I decided to write a short program to interact with my Outlook and extract the info I needed. I have it done (see below), but I am not able to get the return path, because I don't have the right constant. The closest thing I have found is ReplyRecipientNames. I have checked out the M$ Outlook 9.0 Object Library which is on my machine from the ActiveState install at D:\Perl\html\OLE-Browser\Browser.html, but I haven't found what I need.

I need the Return-Path or the entire message header so I can parse it out myself. Any insight out there?

#!C:/perl/bin/perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Outlook'; $|=1; $Win32::OLE::Warn = 3; my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application') or Wi +n32::OLE->new('Outlook. +Application', 'Quit'); my $ol = Win32::OLE::Const->Load($Outlook); my $namespace = $Outlook->GetNameSpace("MAPI") or die "Can't open MAPI + namespace: $!"; my $Folder1 = $namespace->GetDefaultFolder(olFolderInbox); my $Folder2 = $Folder1->Folders("TEST"); foreach my $item (in $Folder2->{Items}){ print $item->{'ReplyRecipientNames'} . "\n"; } exit 0;

Any help is MUCH appeciated.

Update: Changed $|++ to $|=1.

bassplayer

Replies are listed 'Best First'.
Re: Win32::OLE Outlook help requested
by davis (Vicar) on Nov 28, 2002 at 09:41 UTC
    Caveat: I am not by any means an OLE expert, I've never used visual basic, and I try not to use Windows at all

    After looking at the Object Browser from the Visual Basic Editor in Outlook, (Tools->Macro->Visual Basic Editor), I've found an object/property/thingummy named ReplyRecipients in the Mailitem class. This apparently returns a collection of Recipients for the mail message.

    I've not tried it, so I've got no idea if that works. If it does, then all well and good, if it doesn't, then I'd suggest looking in the Object Browser, as it has a reasonable search option.

    cheers
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

    Update: I forgot to mention that there doesn't seem to be any way to extract the mail header. But you may be able to save the message as plain text (I don't know if Outlook can do this), then munge it using something like Mail::Header.

      I've used Outlook Spy to dig into the available properties and collections (of the actual MAPI objects with your data--a BIG help!) A 30-day trial version is available for download. It installs as an Outlook add-in.

      --
      May the Source be with you.

      You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Re: Win32::OLE Outlook help requested
by bassplayer (Monsignor) on Dec 13, 2002 at 21:25 UTC
    Well, I could not find any way to extract the message headers, so the solution ended up being a variation on davis' idea about saving to a text file. Unfortunately, olTXT format does not contain the message header either, but the olMSG format does. This format is binary, but the message header is pretty much in tact, so I used the following to get the job done:
    #!C:/perl/bin/perl use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Outlook'; $|=1; $Win32::OLE::Warn = 3; my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application') or Wi +n32::OLE->new('Outlook. +Application', 'Quit'); my $ol = Win32::OLE::Const->Load($Outlook); my $namespace = $Outlook->GetNameSpace("MAPI") or die "Can't open MAPI + namespace: $!"; my $Folder1 = $namespace->GetDefaultFolder(olFolderInbox); my $Folder2 = $Folder1->Folders("SPAM"); my $temp_file_name = 'D:\\exchange\\temp_spam.msg'; my @return_paths; foreach my $item (in $Folder2->{Items}){ unlink $temp_file_name if -f $temp_file_name; $item->saveAs( $temp_file_name, olMSG ); if ( -f $temp_file_name ) { my $previous_return_path = ''; open( SPAMFILE, $temp_file_name ) || die "Couldn't ope +n the $temp_file_name"; binmode( SPAMFILE ); while ( <SPAMFILE> ) { $_ =~ m/Return-Path: <(.*)>/; push ( @return_paths, $1 ) if $1 && $1 ne '' & +& $1 ne $previous_return_path; $previous_return_path = $1; } close( SPAMFILE ); } } my @deduped_return_paths = remove_duplicates( @return_paths ); my $deduped_return_paths = "deduped_return_paths.txt"; open( DEDUPEDSPAMFILE, ">>$deduped_return_paths" ) || die "Couldn't op +en the $deduped_return_paths"; foreach ( @deduped_return_paths ) { print DEDUPEDSPAMFILE "$_\n"; } close( DEDUPEDSPAMFILE ); exit 0; ####################### sub remove_duplicates { ####################### my @dirty_array = @_; my @clean_array = (); my %hash = (); foreach my $item ( @dirty_array ) { push @clean_array, $item unless $hash{$item}++; } return @clean_array; }
    Thank you both for your help.

    Update: Changed $|++ to $|=1.

    bassplayer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found