全部博文(1144)
分类: LINUX
2014-11-13 22:47:26
先说说环境:
Cygwin+Perl5.8+Mail::POP3Client+MIME::Parser+MIME::Entity+Time::HiRes
Mail::POP3Client 封装了基本的Net::POP3,使用起来更简单
MIME::Parser是MIME::Entity的子类,就是用来解析格式的
Time::HiRes用来计算消耗时间
看看代码:
===========================================================================
#!/usr/bin/perl -w
use strict;
use Mail::POP3Client;
use Time::HiRes qw(gettimeofday tv_interval);
use MIME::Parser;
use MIME::Entity;
my $start_time = [gettimeofday];
my $host = 'pop3.163.com';
my $user = ;
my $passwd = '******';
my $client = new Mail::POP3Client( $user, $passwd, $host );
my $parser = MIME::Parser->new;
my $mgrnum = $client->Count;
print "当前共有邮件[$mgrnum]封!\n";
for ( my $i = 1 ; $i <= $mgrnum ; $i++ ) {
my $headandbody = $client->HeadAndBody($i);
my $entity = $parser->parse_data($headandbody);
$parser->decode_headers(1);
print "From = ",$entity->head->get('From');
print "To = ",$entity->head->get('To');
print "Cc = ",$entity->head->get('Cc');
print "Subject = ",$entity->head->get('Subject');
print "MIME type = ",$entity->mime_type,"\n";
print "Parts = ",scalar $entity->parts,"\n";
my $part_num = scalar $entity->parts;
for my $part ($entity->parts){
print "\t",$part->mime_type,"\t",$part->bodyhandle,"\n";
}
print "=========================================================\n";
exit if ( (scalar $entity->parts) == 1 );
#exit if ($i >= 3);
}
my $interval = tv_interval( $start_time, [gettimeofday] );
print "it take the time : $interval seconds\n";
==============================================================================
前面始终没有闹明白为什么邮件头里面Subjects中文总是乱码不能解析,翻了翻RFC的文档,看看MIMR::Parser才知道有一个 decode函数
### Automatically attempt to RFC 2047-decode the MIME headers?
$parser->decode_headers(1); ### default is false
解析还是很方便的,呵呵
在$parser->parse_data($headandbody)的时候会将邮件body保留在本地msg-PID-#.txt文件 中,附件也保留在相应目录下!