Perl的:的Win32 :: OLE和Microsoft Outlook - 通过电子邮件附件迭代效率 [英] Perl: Win32::OLE and Microsoft Outlook - Iterating through email attachments efficiently

查看:213
本文介绍了Perl的:的Win32 :: OLE和Microsoft Outlook - 通过电子邮件附件迭代效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名实习生,很新的这...

我的老板找来了两个附件每星期一,他已经变成维基code,把它放在我们的内部网站的电子邮件。该过程需要约20分钟,每周一因的信息传送量。我一直在问到流线这个过程。

我有code,这将解析该文件并把它分解成组件,并且我有code抓住所有的附件了他的收件箱中。

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

 #!的/ usr / bin中/ perl的
使用CWD;
使用的Win32 :: OLE QW(与);
使用的Win32 :: OLE ::常量的Microsoft Outlook;
使用的Win32 :: OLE ::变体;我的$ OL =的Win32 :: OLE-> GetActiveObject(Outlook.Application')||的Win32 :: OLE->新建(Outlook.Application','退出');
我的$ namespace = $ OL-GT&; GetNameSpace(MAPI);
我的$文件夹= $ NameSpace-> GetDefaultFolder(olFolderInbox);
我的$ DIR = CWD。 \\\\​​;
$ DIR =〜S / \\ // \\\\ /克;
我的$ atch1,$文件1,$ atch2,$文件2;打印REF($文件夹 - > {}项)。 \\ n;我的foreach $味精(在$文件夹 - > {}项){
    #PRINT $ msg-> {} CREATIONTIME。 \\ n;
    我的foreach $ ATCH(在$ msg-> {}附件){
        如果($ atch-> {文件名} =〜M / .xls的$ / I){
            如果($ atch->附件1的{文件名} =〜/姓名/ I){
                $ atch1 = $ ATCH;
                $文件1 = $目录。 file1.xls;
            }
            如果($ atch-> attachment2的{文件名} =〜/姓名/ I){
                $ atch2 = $ ATCH;
                $文件2 = $目录。 file2.xls;
            }
       }
   }
}如果($ atch1&放大器;&安培; $ atch2){
    打印文件1 $。 \\ n。 $ file2的。 \\ n;
    $ atch1-> SaveAsFile($文件1);
    $ atch2-> SaveAsFile($文件2);
}

这是树立正确的现在,因为它是最旧到最新的方式,目的是找到的文件,然后只需更换他们,如果它发现一个较新的(虽然我删除了该功能)。现实我可以找到最新的人,并停止。

我不知道如何扭转$文件夹 - > {}项目。我甚至不知道它是什么。当我做裁判($文件夹 - > {}项目它说,这是一个Win32 :: OLE,它为Win32文件很多,因为并没有帮助我:: OLE似乎只显示它可以是任何一些事情<。 / p>

任何想法我怎么能到新邮件第一? (倒车$文件夹 - > {项目}比Foreach源以外的东西倾销$文件夹 - ????> {项目}成可逆转另一个对象只是跳过成千上万的电子邮件,直到日期是在过去2周内(我不t等,一个虽然))

感谢。


解决方案

您导入子程序从包装的Win32 :: OLE 。这可能是一些可怕的语法糖。深深的 unperlish 的。我想它会返回一些从的Win32 :: OLE 对象 $文件夹名单。所以,试试这个:

 我的foreach $味精(反向$文件夹 - &GT; {}项目 - &gt;在)

 我的foreach $味精(逆转($文件夹 - &GT; {}项))

另外,一定要使用严格使用在每个脚本中警告以确保高品质code 。如果你可以肯定的是一个现代的Perl将被使用,也可以使用V5.10 并享受功能 - 它像打印,可自动追加一个换行符到您的输出

I'm an intern and very new to this...

My boss gets an email with two attachments every Monday which he has to turn into wiki code and put it on our internal website. The process takes roughly 20 minutes every Monday due to the amount of information to transfer. I've been asked to stream line this process.

I have code which will parse the file and break it up into components, and I have code to grab all the attachments out of his inbox.

The issue I am facing is that my script starts at the oldest email. This isn't a huge issue, but it causes the script to run much longer than needed.

#!/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);
}

The way this is set up right now, since it was oldest to newest, is intended to find the files and then just replace them if it finds a newer one (Though I removed that functionality). Realistically I could just find the newest ones and stop.

I have no idea how to reverse $Folder->{Items}. I don't even understand what it is. When I do ref($Folder->{Items} it says it is a Win32::OLE, which hasn't helped me much since the documentation for Win32::OLE seems to just show it could be any number of things.

Any ideas how I can get to the newer emails first? (Reversing $Folder->{Items}? Something other than Foreach? Dumping $folder->{Items} into another object that can be reversed? Just skip thousands of emails until the date is within the last 2 weeks? (I don't like that one though))

Thanks.

解决方案

You imported the in subroutine from the package Win32::OLE. It is probably some awful "syntactical sugar". And deeply unperlish. I suppose it returns some sort of list from the Win32::OLE object $Folder. So try this:

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

or

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

Also, be sure to use strict and use warnings in every script to ensure high quality code. If you can be sure that a modern perl will be used, you can also use v5.10 and enjoy the say function – it behaves like print, but automatically appends a newline to your output.

这篇关于Perl的:的Win32 :: OLE和Microsoft Outlook - 通过电子邮件附件迭代效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆