功能获取最后一封邮件:然后如果邮件正文匹配op:restart 就执行某些动作.
- #!/usr/bin/perl -w
-
use strict;
-
use MIME::Parser;
-
use Data::Dumper;
-
use Mail::POP3Client;
-
use Time::HiRes qw(gettimeofday tv_interval);
-
-
my $pop = new Mail::POP3Client(
-
USER => "user",
-
PASSWORD => "passwd",
-
HOST => "smtp.xxx.com"
-
) || die "Can not conect:$!";
-
-
### get mail count;
-
my $count = $pop->Count();
-
-
### Get run time
-
my $start_time = [gettimeofday];
-
-
### Create a new parser object:
-
my $parser = new MIME::Parser;
-
-
### Tell it where to put things:
-
$parser->output_dir("Mail");
-
-
mkdir('Mail') if (!-d "Mail");
-
-
### Mail prefix
-
$parser->output_prefix("part");
-
-
### get mail sealing
-
my $headandbody = $pop->HeadAndBody($count);
-
-
my $entity = $parser->parse_data($headandbody);
-
-
### Settings head decode
-
$entity->head->decode;
-
-
$entity->head->mime_encoding;
-
### get Mail From
-
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";
-
-
my $num_parts = $entity->parts;
-
-
### get multipart
-
my $boundary = $entity->head->multipart_boundary;
-
-
my ( $theBody, $part1 );
-
-
### If File ne multipart
-
if ( ( $boundary eq "" ) or ( $num_parts == 0 ) ) {
-
$theBody = $entity->bodyhandle();
-
}
-
else {
-
$part1 = $entity->parts(0);
-
$theBody = $part1->bodyhandle();
-
}
-
-
### File path
-
#print $theBody->path;
-
-
my $Text_path = $theBody->path;
-
open( FD, "<$Text_path" ) || die "$!\n";
-
-
print "#-=-=-=-=-=-= [ 邮件正文 ] =-=-=-=-=-=# \n";
-
while (<FD>) {
-
print $_,"\n";
-
if ( $_ =~ /op:restart/i ) {
-
print "重启服务器\n";
-
$pop->Delete($count);
-
last;
-
}
-
}
-
-
print "=========================================================\n";
-
-
exit if ( ( scalar $entity->parts ) == 1 );
-
-
my $interval = tv_interval( $start_time, [gettimeofday] );
-
print "it take the time : $interval seconds\n";
阅读(1659) | 评论(0) | 转发(0) |