Perl 解析来自 Outlook 收件箱的电子邮件和附件

Perl parse email and attachments from Outlook inbox

我正在使用 Mail::IMAPClient to connect to our Outlook mail server. I can get the mail just fine and print the text version of that mail to a file. But I'm having trouble using MIME::Parser 解析电子邮件。

我试过为解析器提供一个文件句柄,指向我向其发送电子邮件的文本文件。我试过只给解析器提供电子邮件的文本,但它不会像我期望的那样工作。实体部分始终等于 0。

当我转储实体骨架时,我得到

  Content-type: text/plain
  Effective-type: text/plain
  Body-file: NONE
  --

我可以在文件中看到电子邮件的所有部分。附加的两个 PDF 在那里,以 base64 编码,所以我知道脚本实际上是在检索电子邮件和附件。我也试过 parseparse_data.

my $msgCount = 0;    
$msgCount = $imap->message_count();    
#or abortMission("", "Could not get message count: ". $imap->LastError );

if ( $msgCount > 0 ) {     

    #get all the messages from the inbox folder    
    my @msgseqnos = $imap->messages
            or abortMission("", "Could not retreive messages:". $imap->LastError);

    my ($x, $bh, $attachment, $attachmentName);

    foreach my $seqno ( @msgseqnos ) {

        my $input_file;
        my $parser = new MIME::Parser;
        my $emailText = $imap->body_string($seqno)   # should be the entire email as text. 
                or abortMission("", "Could not get message string: " . $imap->LastError);

        $parser->ignore_errors(1);
        $parser->output_to_core(1);

        open my $emailFileHandle, ">", "invoiceText.txt";
        print $emailFileHandle $emailText;
        #$imap->message_to_file($emailFileHandle, $seqno);

        my $entity = $parser->parse_data($emailText);
        $entity->dump_skeleton;

        if ( $entity->parts > 0 ) {

            for ( my $i = 0; $i < $entity->parts; $i++ ) {

                my $subentity = $entity->parts($i);

                # grab attachment name and contents
                foreach $x ( @attypes ) {

                    if ( $subentity->mime_type =~ m/$x/i ) {

                        $bh = $subentity->bodyhandle;
                        $attachment = $bh->as_string;
                        $attachmentName = $subentity->head->mime_attr('content-disposition.filename');

                        open FH, ">$attachmentName";
                        print FH $attachment;
                        close FH;

                        #push @attachment, $attachment;
                        #push @attname, $subentity->head->mime_attr('content-disposition.filename');
                    }
                }
            }
        }
        else {
            stillAGo("eData VehicleInvoices problem", "Perl can't find an attachment in an email in the VehicleInvoices folder of eData email address");
        }

        close $emailFileHandle;

        # say $emailText;
        # next;

        #open OUT_FILE, ">invoiceText.txt";
        #print OUT_FILE $emailText;
        #print OUT_FILE $imap->bodypart_string($seqno,1);
        #close OUT_FILE;

        #print $emailText;
    }
}

我正在尝试自动从电子邮件中检索附件并将它们保存到磁盘以供其他作业处理。

我想包含 invoiceText.txt 文件,以便人们可以看到实际输出,但它有 1200 行长。我不知道在哪里上传文件到这里的 link。

body_string 方法不会 return 整封电子邮件。正如文档所述,顾名思义,它 return 是消息的 body,不包括 headers。这就是为什么 dump_skeleton 除了默认

之外没有显示 headers

虽然我还没有尝试过,但您可能想要的是 message_string return 整个电子邮件

我看到您使用了 message_to_file 但将其注释掉了。如果您 MIME::Parse 从文件

中读取,那可能会奏效