Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111817
  • 博文数量: 25
  • 博客积分: 1094
  • 博客等级: 少尉
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 16:36
文章分类

全部博文(25)

文章存档

2011年(14)

2010年(11)

分类: LINUX

2011-06-10 15:16:21

功能获取最后一封邮件:然后如果邮件正文匹配op:restart 就执行某些动作.
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use MIME::Parser;
  4. use Data::Dumper;
  5. use Mail::POP3Client;
  6. use Time::HiRes qw(gettimeofday tv_interval);

  7. my $pop = new Mail::POP3Client(
  8.     USER => "user",
  9.     PASSWORD => "passwd",
  10.     HOST => "smtp.xxx.com"
  11. ) || die "Can not conect:$!";

  12. ### get mail count;
  13. my $count = $pop->Count();

  14. ### Get run time
  15. my $start_time = [gettimeofday];

  16. ### Create a new parser object:
  17. my $parser = new MIME::Parser;

  18. ### Tell it where to put things:
  19. $parser->output_dir("Mail");

  20. mkdir('Mail') if (!-d "Mail");

  21. ### Mail prefix
  22. $parser->output_prefix("part");

  23. ### get mail sealing
  24. my $headandbody = $pop->HeadAndBody($count);

  25. my $entity = $parser->parse_data($headandbody);

  26. ### Settings head decode
  27. $entity->head->decode;

  28. $entity->head->mime_encoding;
  29. ### get Mail From
  30. print "From = ", $entity->head->get('From');
  31. print "To = ", $entity->head->get('To');
  32. print "Cc = ", $entity->head->get('Cc');
  33. print "Subject = ", $entity->head->get('Subject');

  34. #print "MIME type = ",$entity->mime_type,"\n";

  35. my $num_parts = $entity->parts;

  36. ### get multipart
  37. my $boundary = $entity->head->multipart_boundary;

  38. my ( $theBody, $part1 );

  39. ### If File ne multipart
  40. if ( ( $boundary eq "" ) or ( $num_parts == 0 ) ) {
  41.     $theBody = $entity->bodyhandle();
  42. }
  43. else {
  44.     $part1 = $entity->parts(0);
  45.     $theBody = $part1->bodyhandle();
  46. }

  47. ### File path
  48. #print $theBody->path;

  49. my $Text_path = $theBody->path;
  50. open( FD, "<$Text_path" ) || die "$!\n";

  51. print "#-=-=-=-=-=-= [ 邮件正文 ] =-=-=-=-=-=# \n";
  52. while (<FD>) {
  53.     print $_,"\n";
  54.     if ( $_ =~ /op:restart/i ) {
  55.         print "重启服务器\n";
  56.         $pop->Delete($count);
  57.         last;
  58.     }
  59. }

  60. print "=========================================================\n";

  61. exit if ( ( scalar $entity->parts ) == 1 );

  62. my $interval = tv_interval( $start_time, [gettimeofday] );
  63. print "it take the time : $interval seconds\n";

阅读(1659) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~