Perl:Win32 :: OLE和Microsoft Outlook - 有效地通过电子邮件附件进行迭代

时间:2012-07-25 17:04:17

标签: perl email automation ole attachment

我是一名实习生,对此非常陌生......

我的老板每周一收到一封带有两个附件的电子邮件,他必须将其变成维基代码并将其放在我们的内部网站上。由于要转移的信息量,该过程每周一大约需要20分钟。我被要求简化这个过程。

我有代码可以解析文件并将其分解为组件,我有代码可以从收件箱中抓取所有附件。

我面临的问题是我的脚本从最早的电子邮件开始。这不是一个大问题,但它导致脚本运行的时间比需要的时间长得多。

#!/usr/bin/perl
use Cwd;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Outlook';
use Win32::OLE::Variant;

my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application', 'Quit');
my $NameSpace = $OL->GetNameSpace("MAPI");
my $Folder = $NameSpace->GetDefaultFolder(olFolderInbox);
my $dir = cwd . "\\";
$dir =~ s/\//\\/g;
my $atch1, $file1, $atch2, $file2;

print ref($Folder->{Items}) . "\n";

foreach my $msg (in $Folder->{Items}){
    #print $msg->{CreationTime} . "\n";
    foreach my $atch (in $msg->{Attachments}){
        if($atch->{FileName} =~ m/.xls$/i){
            if($atch->{FileName} =~ /Name of attachment1/i){
                $atch1 = $atch;
                $file1 = $dir . "file1.xls";
            }
            if($atch->{FileName} =~ /Name of attachment2/i){
                $atch2 = $atch;
                $file2 = $dir . "file2.xls";
            }
       }
   }
}

if($atch1 && $atch2){
    print $file1 . "\n" . $file2 . "\n";
    $atch1->SaveAsFile($file1);
    $atch2->SaveAsFile($file2);
}

现在设置它的方式,因为它是最旧的,最新的,旨在找到文件,然后只要找到更新的文件(虽然我删除了该功能)。实际上,我可以找到最新的并停止。

我不知道如何撤销$ Folder-> {Items}。我甚至不明白它是什么。当我做ref($ Folder-> {Items}它说它是一个Win32 :: OLE,它没有帮助我很多,因为Win32 :: OLE的文档似乎只是显示它可能是任何数量的的东西。

我是否可以先了解更新的电子邮件? (撤消$ Folder-> {Items}?Foreach以外的东西?将$ folder-> {Items}转储到另一个可以撤销的对象中?只需跳过数千封电子邮件,直到日期在过去2周内?(I虽然不喜欢那个))

感谢。

2 个答案:

答案 0 :(得分:4)

您从包in导入了Win32::OLE子例程。这可能是一些可怕的“语法糖”。并深深地 unperlish 。我想它会从Win32::OLE对象$Folder返回某种列表。所以试试这个:

foreach my $msg (reverse $Folder->{items}->in)

foreach my $msg (reverse in($Folder->{items}))

此外,请务必在每个脚本中use strictuse warnings以确保高质量的代码。如果您确定将使用现代perl,您还可以use v5.10并享受say功能 - 它的行为类似print,但会自动为您的输出添加换行符。< / p>

答案 1 :(得分:0)

尝试这样的事情

foreach my $msg (reverse @{$Folder->{items}})

不要在perl中使用“in”