Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

E.mail interface

by Win (Novice)
on Dec 16, 2005 at 17:24 UTC ( [id://517305]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I would like to write a Perl program that will read my Microsoft Outlook email account and then search through subject headings and pick out emails with particular headings. When the emails of interest are found I want them to detach an attached file and save it to a local folder. Please can people suggest modules that I should be looking at?

Replies are listed 'Best First'.
Re: E.mail interface
by bassplayer (Monsignor) on Dec 16, 2005 at 17:45 UTC
    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.

    bassplayer

Re: E.mail interface
by NetWallah (Canon) on Dec 16, 2005 at 19:39 UTC
    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.

Re: E.mail interface
by Popcorn Dave (Abbot) on Dec 16, 2005 at 17:42 UTC
    Start by looking at Mail::Pop3Client. That should do what your'e after as far as being able to access your e-mail. If however you want to look at your e-mail already in your Outlook Express, that's quite a different story.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: E.mail interface
by eweaver (Sexton) on Dec 16, 2005 at 19:13 UTC
    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.
Re: E.mail interface
by SamCG (Hermit) on Dec 16, 2005 at 20:48 UTC
    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();
Re: E.mail interface
by marto (Cardinal) on Dec 16, 2005 at 17:28 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found