Mail-Outlook

 view release on metacpan or  search on metacpan

lib/Mail/Outlook/Folder.pm  view on Meta::CPAN

#----------------------------------------------------------------------------

=head1 NAME

Mail::Outlook::Folder - extension to handle Microsoft (R) Outlook (R) mail folders.

=head1 SYNOPSIS

See Mail::Outlook, as this is not meant to be used as a standalone module.

=head1 DESCRIPTION

Handles the Folder interaction with the Outlook API.

=cut

#----------------------------------------------------------------------------

#############################################################################
#Library Modules                                                            #
#############################################################################

use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';

use Mail::Outlook::Message;

#############################################################################
#Variables
#############################################################################

my %foldernames = (
    'Inbox'         => olFolderInbox,
    'Outbox'        => olFolderOutbox,
    'Sent Items'    => olFolderSentMail,
    'Drafts'        => olFolderDrafts,
    'Deleted Items' => olFolderDeletedItems,
);

#----------------------------------------------------------------------------

#############################################################################
#Interface Functions                                                        #
#############################################################################

=head1 METHODS

=over 4

=item new()

Create a new Outlook mail object. Returns the object on success or undef on
failure. To see the last error use 'Win32::OLE->LastError();'.

=cut

sub new {
    my ($self, $outlook, $foldername) = @_;
    my ($mailbox,$folder,$path);

    # split mailbox and path
    ($foldername,$path) = ($foldername =~ m!(.*?)/(.*)!)
        if ($foldername =~ m!/!);

    # mailbox name
    if($foldernames{$foldername}) {
        eval { $mailbox = $outlook->{namespace}->GetDefaultFolder($foldernames{$foldername}) };
        return undef    if($@);

    # mailbox constant only
    } elsif($foldername =~ /^\d+$/) {
        eval { $mailbox = $outlook->{namespace}->GetDefaultFolder($foldername) };
        return undef    if($@);

    # well if you don't know, neither do i!!!
    } else {
        return undef;
    }

    if($path) {
        # This is a bit of a hack to stop the OLE complaining when the path
        # doesn't exist in the folder tree
        my $hash;
        eval { $hash = $mailbox->Folders(); };
        my %keys = map {$_ => 1} keys %$hash;
        return undef    if($@);

        $folder = $self->_folders($mailbox, $path) || return undef; 

    }

    # create an attributes hash
    my $atts = {
        'outlook'       => $outlook,
        'foldername'    => $foldername,
        'objfolder'     => $folder || $mailbox || undef,
        'items'         => undef,
    };

    # prime the mail items collection
    $atts->{items} = $atts->{objfolder}->Items()    or return undef;

    # create the object
    bless $atts, $self;
    return $atts;
}

# Split the path (eg A/B/C) into segments call call the Folder method for each. 
# Unix style separators (/).
sub _folders {
  my ($self,$mailbox, $path)=@_;
  my @segs = split('/', $path );
  my $f;
  foreach my $s ( @segs ) {
    eval { $f = $f ? $f->Folders($s) : $mailbox->Folders($s);1;}||return undef;
    return undef if($@); 
    return undef unless( defined($f) );    
  }
  return $f;
}



( run in 0.641 second using v1.01-cache-2.11-cpan-71847e10f99 )