2014-04-15 41 views
0

我自動通過perl從Exchange 2010服務器下載郵件。到目前爲止,我已經設法通過Exchange Web服務(EWS)和解析頭來訪問消息。現在我想知道如何將消息的附件下載到本地臨時文件夾。使用Perl從Exchange郵件下載附件

我是Perl新手,無法找到消息數據結構的源代碼或文檔。任何幫助表示讚賞。

use Email::Folder::Exchange; 
use Email::Simple; 

# some more code here.... 

my $folder = Email::Folder::Exchange->new($url, $user, $pass); 

for my $message ($folder->messages) { 
    if ($message->header('Subject') =~ /Downloadable Message/) { 
     // How to access message's attachments? 
    } 
} 

回答

1

所以基本上,關鍵是要轉換的電子郵件::簡單了Email :: MIME,並使用了Email :: MIME ::附件::剝通過每個附件進行解析。容易;-)

!我只複製了相關部分......所以您可能需要稍微擴展一下才能重用。

use Email::Folder::Exchange; 
use Email::Simple; 
use Email::MIME::Attachment::Stripper; 

# some more code here.... 

my $folder = Email::Folder::Exchange->new($url, $user, $pass); 

for my $message ($folder->messages) { 
    my $tmpMsg = Email::MIME->new($message->as_string); 
    my $stripper = Email::MIME::Attachment::Stripper->new($tmpMsg); 

    for my $a ($stripper->attachments()) { 
     next if $a->{'filename'} !~ /csv/i; #only csv attachments 
     my $tempdir = "C:\\temp\\"; 
     my $tmpPath = $tmpdir . $a->{'filename'}; 


     # Save file to temporary path 
     my $f = new IO::File $tmpPath, "w" or die "Cannot create file " . $tmpPath; 
     print $f $a->{'payload'}; 
    } 
}