【问题标题】:How to extract the zipped xls file from the email attachment?如何从电子邮件附件中提取压缩的 xls 文件?
【发布时间】:2015-04-21 07:27:45
【问题描述】:

我有一个模块应该提取电子邮件附件并将其放置在特定位置。该代码创建 POP3 客户端来获取邮件,并一直使用 EMAIL::MIME::Attachment::Stripper 模块来提取附件,如下所示。

     my $mail=$pop->HeadAndBody($i);
     my $parsed = Email::MIME->new($mail);
     my $stripper = Email::MIME::Attachment::Stripper->new($parsed);
     my @attachments = $stripper->attachments;

     foreach my $a(@attachments)
     {
        next if $a->{content_type} !~ /octet-stream/i;
        my $f = new IO::File "C:/MAIL_PARSING_DATA/" . "<filename>.<file-extension>", "w" or die "Can not create file!";
        print $f $a->{payload};
        goto EXITPOINT;
     }

该代码适用于 Perl 模块识别的标准文件,如电子表格等。但不适用于具有 压缩 Excel 文件作为附件的特定邮件。在提取文件时,该文件的 Perl 脚本识别的 content_typeapplication/octet-stream。使用上述代码提取文件时,文件似乎已损坏,因为:

  1. 文件未通过 WinZip、WinRAR 或 7-Zip 打开。
  2. 通过此脚本提取的文件的文件大小与使用 Outlook 提取的相同文件的大小略有不同。

请就这个问题提供一些意见。

【问题讨论】:

    标签: excel perl email zip email-attachments


    【解决方案1】:

    问题来了

    next if $a->{content_type} !~ /octet-stream/i;
    

    zip 的内容类型为 application/zip

    【讨论】:

      【解决方案2】:

      @NeoNox,感谢您的回复。但是 perl 系统仅将文件识别为八位字节流,而不是 application/zip。

      无论如何,我可以找到解决方案。

      我可以通过以二进制模式写入文件来解决问题(明确提及如下:

          binmode $fh;
      

      这里的文件正在以二进制模式写入,所以我没有将输出作为损坏的文件。

      【讨论】:

        【解决方案3】:

        我发现了同样的问题。

        XLSX 文件不被视为附件。 我正在使用 IMAP。

        它将下载电子邮件正文、任何 jpg 或 png 等,但即使在包含已下载附件的电子邮件中也找不到附加的 xlsx。

        【讨论】:

          猜你喜欢
          • 2016-07-26
          • 1970-01-01
          • 1970-01-01
          • 2017-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-03
          • 1970-01-01
          相关资源
          最近更新 更多