This is a simple method that allows to search emails with certain criteria:
Criteria: AND or OR to say how to combine multiple criteria
Mailbox: Specify searching mailbox (path folder levels seperate by backslash (\); default: Inbox
Properties: can specify Field : Value pairs in an outlook mail item
Usage:
my $given_item = { 'Criteria' => 'OR', 'Mailbox' => 'Mailbox\Inbox\special', 'Subject' => 'Very important', 'To' => 'postmaster@perlmonk.com', }; my ($item, $future_use) = search_mail($given_item);
Input:
given_item: Pointer to the input criteria hash
namespace(optional): From where it's try to find Mailbox path
Output:
item: First mail met the given criteria
future_use: A collection of Items on which you can call FindNext() method to search recursively for the same criteria
Note:
Two sub-functions locate_folder & set_filter are used for each customerisation
You can find one another similar snippet here
Have fun - SB
use strict; use Win32::OLE; ############################################################ # # Usage: # my ($item, [$future_use]) = search_mail($given_item, [$namespace]); # # Input: # given_item: Pointer to the input criteria hash # # Ex: # my $given_item = { # 'Criteria' => 'OR', # 'Mailbox' => 'Mailbox - Gamage,S,Sumith,XVR4A C\Inbox\bt', # 'Subject' => 'Thank you!', # 'To' => 'BT-Students@messenger.hermes.bt.co.uk', # }; # # namespace(optional): From where it's try to find Mailbox path # # Output: # item: First mail met the given criteria # future_use: A collection of Items on which you can call FindNext() # method to search recursively for the same criteria # ############################################################ sub search_mail { my ($given_item, $namespace) = @_; my $folder = locate_folder($given_item, $namespace); if ($folder) { my $items = $folder->Items(); my $filter = set_filter($given_item); my $item = $items->Find( $filter ); return ($item, $items); } } sub locate_folder { my ($given_item, $namespace) = @_; my $path = $given_item->{'Mailbox'}; unless ($namespace) { my $outlook = Win32::OLE->new('Outlook.Application'); $namespace = $outlook->GetNamespace("MAPI"); } my $folders = $namespace->Folders(); my $folder = undef; my @path = split(/\\/, $path); while (@path) { my $crnt_folder = shift @path; if ($folders->{'Count'}) { $folder = $folders->GetFirst(); my $last = 0; do { if (lc($folder->{'Name'}) eq lc($crnt_folder)) { if (@path) { $folders = $folder->Folders(); } $last = 1; } $folder = $folders->GetNext() unless ($last); } while ((not $last) && $folder) } } return ($folder) ? $folder : $namespace->GetDefaultFolder(6); } sub set_filter { my ($given_item) = @_; my $filter = ""; my $criteria = ($given_item->{'Criteria'} =~ /AND|OR/i) ? $given_i +tem->{'Criteria'} : 'OR'; foreach my $field (keys %{$given_item}) { unless ($field =~ /^Mailbox|Criteria$/) { $filter .= "[$field] = $given_item->{$field} $criteria "; } } $filter =~ s/ $criteria $//; return $filter; }
|
|---|