For interfacing with Windows programs, check out the Win32::OLE module on CPAN, and as suggested above, search nodes on this site, as others may have done parts of your task already.
| [reply] |
It always helps to have a working sample, to use as a starting point - so here is some:
#Read Outlook Folder...
use strict;
use Win32::OLE qw(in);
use Constant olFolderCalendar=>9;
my $Outlook = Win32::OLE->GetObject("Outlook.Application")
|| Win32::OLE->new("Outlook.Application");
my $Session = $Outlook->Session;
#my $NameSpace = $Session->GetNameSpace("MAPI");
#my $ContactsFolder = $NameSpace->GetDefaultFolder(olFolderContacts);
ShowSubFolders ($Session->Folders);
#################################################
sub ShowSubFolders{
my $FolderCollection = shift;
my $level = shift || 0;
for my $Folder (in $FolderCollection) {
print " " x $level . "Folder$level: $Folder->{Name}\t(" . $Folder
+->Items->{Count}. ")\n";
#if ($Folder->{Name} =~ /Personal Folders/) {
if ($Folder->{Name} =~ /^Mailbox|^Issues/) {
ShowSubFolders($Folder->Folders, $level + 1);
}
if ($Folder->{Name} =~ /Inbox/) {
my $msgCount=1;
print "*** Please OK the msgbox in outlook that requests externa
+l program access**\n";
$Folder->Items->Sort ("CreationTime",1); # Descending by date
for my $Item (in $Folder->Items) {
print "$msgCount: $Item->{SenderName} : $Item->{Subject}\n";
#print "\t $Item->{Body} \n\n";
last if $msgCount++ > 9;
}
}
}
}
You're just jealous cause the voices are only talking to me.
No trees were killed in the sending of this message. However, a large number of electrons were terribly inconvenienced.
| [reply] [d/l] |
| [reply] |
You need to specify what exactly you mean by "read my outlook account". I think you are referring to already downloaded messages on your computer, not your email account itself, but maybe not. If you connect using IMAP you can bypass outlook altogether, because IMAP saves all your mail server-side. POP3 does not. | [reply] |
Hey Win,
I did almost exactly this recently...this script takes a folder that you want to search, a folder you want to move processed mail items to, and a regex to search on the subject line.
The datestamping could use work, I was just lazy b/c it was just for me.
#! perl -w
use strict;
use Win32::OLE qw(in valof with OVERLOAD);
my $infolder;
my $tstamp = &getDateString;
my ($in, $to, $submatch)=@ARGV;
my $mail = new Win32::OLE('Outlook.Application');
my $ns = $mail->GetNamespace("MAPI");
my $inbox = $ns->GetDefaultFolder(6);
if ($in !~ /inbox/i){
$infolder = $inbox->Folders($in);
}
else {
$infolder = $inbox;
}
my $tofolder = $inbox->Folders($to);
my $count = $infolder->Items->Count;
print "There are $count messages in the $in folder\n";
my $i=0;
my $result = &saveAttachments($submatch);
sub saveAttachments(){
my ($sub) = @_;
foreach my $item(in $infolder->Items){
#my $bdy = $item->Body;
my $subject = $item->Subject;
#print "$subject\n";
#print "$bdy\n";
if ($subject =~ /$sub/i){
foreach my $atm(in $item->Attachments){
my $atmname = $atm->FileName;
$tstamp = &getDateString;
print "\nSaving $tstamp.$atmname...";
$atm->SaveAsFile("H:\\erepts\\$tstamp.$atmname");
(-e "H:\\erepts\\$tstamp.$atmname") or print "Could not sa
+ve $tstamp.$atmname\n";
}
$item->Move($tofolder);
}
}
}
sub getDateString(){
my @fields=localtime();
$fields[5] += 1900;
$fields[4]++;
for (@fields){
$_ = sprintf ("%01d", $_) if length($_) gt 2;
}
my $time_stamp = join "-", reverse @fields[0..5];
return $time_stamp;
}
#$mail->Quit();
| [reply] [d/l] |
Win,
What have you tried so far?
Have you looked on Cpan for pop3/imap modules which would help you with this? If you did a Super Search you may find that someone else has done something similar in the past.
Hope this helps.
Martin | [reply] |