簡體   English   中英

Perl:Win32 :: OLE和Microsoft Outlook - 有效地通過電子郵件附件進行迭代

[英]Perl: Win32::OLE and Microsoft Outlook - Iterating through email attachments efficiently

我是一個實習生,對此很新...

我的老板每周一收到一封帶有兩個附件的電子郵件,他必須將其變成維基代碼並將其放在我們的內部網站上。 由於要轉移的信息量,該過程每周一大約需要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周內?(我不知道)雖然像那個)))

謝謝。

您從包Win32::OLE導入in子例程。 這可能是一些可怕的“語法糖”。 而且非常不合時宜 我想它從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 ,但會自動為您的輸出添加換行符。

嘗試這樣的事情

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

不要在perl中使用“in”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM