PERL   52

extract mail

Guest on 24th November 2022 03:19:32 PM

  1. #!/usr/local/bin/perl -w
  2.  
  3. use strict;
  4. use MIME::Parser;
  5.  
  6. use Sys::Syslog qw(:DEFAULT setlogsock);
  7. setlogsock 'unix';
  8.  
  9. sub logmsg($$);
  10. my $syslog_opened = 0;
  11. my $syslog_facility = 'mail';
  12. my $tag = $0; $tag =~ s,^.*/,,; $tag =~ s/\.pl//;
  13.  
  14. my $p = new MIME::Parser;
  15. $p->output_to_core(1);                  # do not write to fs
  16. $p->extract_nested_messages(0);         # do not parse nested message/rfc822
  17. $p->extract_uuencode(1);                # parse uuencoded too (just in case)
  18. $p->decode_bodies(0);                   # avoid to change entity bodies
  19. $p->tmp_dir("/var/tmp");                # place temp files here if needed
  20.  
  21. # read from STDIN
  22. my $pos = tell STDIN;
  23. my $from = <STDIN>;
  24. exit unless $from;
  25. logmsg('notice', "from: $from");
  26. seek STDIN, $pos, 0;
  27.  
  28. my $e = $p->parse(\*STDIN) or exit;
  29. my $j = 1;
  30. foreach ($e->parts()) {
  31.   if ($_->mime_type eq 'message/rfc822') {
  32.       logmsg('notice', "printing part $j"); $j++;
  33.       print "From - Thu Jan  1 07:00:05 1970\n";
  34.       $_->print_body(\*STDOUT);           # print out forwarded messages
  35.       print "\n";
  36.   }
  37. }
  38. # that's all, folks!
  39.  
  40. sub logmsg($$) {
  41.   my $i = 1;
  42.   while ($i <= 3) {
  43.     unless($syslog_opened) {
  44.       openlog($tag, 'cons,pid', $syslog_facility) and $syslog_opened = 1;
  45.     }
  46.     return if syslog($_[0], $_[1]);
  47.     closelog();
  48.     $syslog_opened = 0;
  49.     $i++;
  50.     sleep(1);
  51.   }
  52. }

Raw Paste


Login or Register to edit or fork this paste. It's free.