0

我正在使用 Perl 来阅读消息、查找和保存附件。附件将始终是二进制 pdf 文档,并且永远不会有多个附件。我需要阅读主题,检查附件(如果存在)并将复制消息保存到文件夹以进行临时存储。

阅读、打印、复印功能都可以正常工作。我用 MIME::Parser 尝试了很多不同的场景(我安装了 MIME::Tools),但要么得到一个空白文件,要么得到一个包含 1 个或 2 个字符的文件。我还想知道如何确定/设置文件扩展名,而不是盲目地重命名为 .pdf。

#!/usr/bin/perl


use Net::IMAP::Simple::SSL;
use Email::Simple;
use MIME::Parser;

print "Content-type: text/html\n\n";

$server = new Net::IMAP::Simple::SSL('xxx');
$server->login('xxx','xxx');

my $folder='inbox';

my ($unseen, $recent, $total) = $server->status($folder);
my $newm = $server->select('INBOX');

my $tmp=($total-9); #limit for testing

my $outputdir = "./temp";
my $parser = new MIME::Parser;
$parser->output_dir($outputdir);


for (my $i = $tmp; $i <= $total; $i++) {

        if ($server->seen($i)) {
        print "Message #$i has been seen before...<br />";
        } else {

        my $es=Email::Simple->new(join '', @{$server->top($i)});
        print $es->header('Subject')." on ";
        print $es->header('Date')."<br />";
        print "You've just seen message #$i<br />" if $server->see($i)."<br />";
        $msg = $server->get($i);
        $parser->parse_data($msg);
        $server->copy($i,'dump');

        }

    }

 $server->quit();

 exit;

错误

parse_data:错误的参数引用类型:Net::IMAP::Simple::_message at mailextract.pl line x

4

2 回答 2

1

不知道你为什么使用两个不同的解析器......

my $entity = $parser->parse_data($message);
my $from = $entity->head->get('From');
my $subject = $entity->head->get('Subject');
my $timestamp = $entity->head->get('Date');

for my $part ($entity->parts()) {
  if ( $part->mime_type eq 'application/pdf' ) { ### Few different types in use, see what your
                                                 ###  messages get sent with
    my $filename = $part->bodyhandle->path;
    ...
    ### Do whatever
  }
}

编辑:并且您的错误正在发生,因为您没有提供要解析的正确内容,即 Net::IMAP::Simple::_message 而不是:

解析数据数据

Instance method. Parse a MIME message that's already in core. This internally creates an "in memory" filehandle on a Perl scalar value

使用 PerlIO

You may supply the DATA in any of a number of ways...

    A scalar which holds the message. A reference to this scalar will be used internally.

    A ref to a scalar which holds the message. This reference will be used internally.

    DEPRECATED

    A ref to an array of scalars. The array is internally concatenated into a temporary string, and a reference to the new

字符串在内部使用。

    It is much more efficient to pass in a scalar reference, so please consider refactoring your code to use that interface instead.

如果你绝对必须传递一个数组,你最好在调用代码中使用 IO::ScalarArray 来生成一个文件句柄,然后将该文件句柄传递给 parse()

试试$parser->parse($server->getfh($i))

于 2012-10-11T20:45:48.583 回答
0
#!/usr/bin/perl

use Net::IMAP::Simple::SSL;
use MIME::Parser;

print "Content-type: text/html\n\n";

$server = new Net::IMAP::Simple::SSL('xxx');
$server->login('xxx','xxx');


my $newm=0;
   $newm = $server->select('INBOX');

if ($newm==0) {
  $server->quit();
  print "No New Messages.";
  exit;
  }

my $outputdir = "./temp";
my $parser = new MIME::Parser;
$parser->output_dir($outputdir);


for (my $i = 1; $i <= $newm; $i++) {

  my $entity = $parser->parse($server->getfh($i));
  my $from = $entity->head->get('From');
  my $subject = $entity->head->get('Subject');
  my $timestamp = $entity->head->get('Date');

  print "#$i $from / $subject / $timestamp<br />";

  for my $part ($entity->parts()) {
    print " / ".$part->mime_type;
    if ( $part->mime_type eq 'application/octet-stream' || $part->mime_type eq 'application/pdf' ) {
      my $filename = $part->bodyhandle->path;
      print " / $filename";
      }
    print "<br />";
    }
  $server->copy($i,'dump');
  $server->delete($i);
  }
$server->quit();
于 2012-10-12T11:40:21.193 回答