我使用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:错误的参数ref类型:mailextract.pl第x行中的Net :: IMAP :: Simple :: _ message
答案 0 :(得分: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 :: _消息而不是:
parse_data DATA
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))
;
答案 1 :(得分: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();