Programmer
Programmer

Reputation: 475

download attachments from ms outlook using perl

I am assigned to download attachments from ms outlook using perl with the mail subject as Net file. Since i am new to perl i am confused how to proceed. This is the code with which i have to proceed with

use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
my $outlook;
eval {$outlook = Win32::OLE->GetActiveObject('Outlook.Application')};
die "Outlook not installed" if $@;
unless (defined $outlook) {
    $outlook = Win32::OLE->new('Outlook.Application','Quit') 
               or die "Unable to start Outlook";
}

$outlook->{visible} = 0;

my $dir = "F:\\OL\\";      #destination  directory
$dir =~ s/\//\\/g;

#get the Inbox folder
my $namespace = $outlook->GetNamespace("MAPI");

Can some one please help me with the remaining part of the code.

Upvotes: 1

Views: 1436

Answers (1)

user1126070
user1126070

Reputation: 5069

You could get a lot of info form this: Perl: Win32::OLE and Microsoft Outlook - Iterating through email attachments efficiently or from http://www.perlmonks.org/?node_id=700307

use strict;
use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Outlook';
use Win32::OLE::Variant;

my $Folder = $NameSpace->GetDefaultFolder(olFolderInbox);

foreach my $msg (reverse in($Folder->{items}))
    print "Subject: ",$msg->{'Subject'},"\n";
    next if $msg->{'Subject'} !~ m!Net file!i;
    foreach my $atch (reverse in($msg->{Attachments}))
        if($atch->{FileName} =~ m/.xls$/i){
            if($atch->{FileName} =~ /Name of attachment1/i){
                print "found ".$atch->{FileName}."\n";
            }

       }
   }
}

or you could use Mail::Outlook

Upvotes: 1

Related Questions