Perl:Win32::OLE 和 Microsoft Outlook - 有效地遍历电子邮件附件 [英] Perl: Win32::OLE and Microsoft Outlook - Iterating through email attachments efficiently

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

问题描述

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

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

我的老板每周一都会收到一封带有两个附件的电子邮件,他必须将这些附件转换为 wiki 代码并将其放在我们的内部网站上.由于要传输的信息量很大,该过程每周一大约需要 20 分钟.我被要求简化这个过程.

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}) . "
";

foreach my $msg (in $Folder->{Items}){
    #print $msg->{CreationTime} . "
";
    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 . "
" . $file2 . "
";
    $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.

我不知道如何反转 $Folder->{Items}.我什至不明白它是什么.当我执行 ref($Folder->{Items} 时,它说它是一个 Win32::OLE,这对我没有多大帮助,因为 Win32::OLE 的文档似乎只是表明它可以是任意数量的东西.

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.

有什么想法可以让我先获取较新的电子邮件吗?(反转 $Folder->{Items}?Foreach 以外的东西?将 $folder->{Items} 转储到另一个可以反转的对象中?只需跳过数千封电子邮件,直到日期在过去 2 周内?(我不虽然不喜欢那个))

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))

谢谢.

推荐答案

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

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)

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

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

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天全站免登陆