mail_summary (1539B)
1 #!/usr/bin/perl 2 # http://louis.jachiet.com/blog/?p=228 3 # by Louis Jachiet, public domain 4 # requires liburi-perl libmime-tools-perl 5 6 # read mail on stdin, produce summary on stdout 7 8 use MIME::Parser; 9 use URI::Escape; 10 use Encode qw(decode); 11 12 # http://stackoverflow.com/a/4597964/414272 13 sub trim { 14 (my $s = $_[0]) =~ s/^\s+|\s+$//g; 15 return $s; 16 } 17 18 my $parser = new MIME::Parser; 19 $parser->output_under("/tmp"); 20 $entity = $parser->parse(\*STDIN) or die "parse failed\n"; 21 $subject = decode("MIME-Header",$entity->get('Subject')); 22 $from = decode("MIME-Header",$entity->get('From')); 23 $body = ""; 24 25 sub explore_mail 26 { 27 my $mpart = $_[0] ; 28 if( defined($mpart->parts()) && $mpart->parts() > 0) 29 { 30 foreach $int ($mpart->parts()) 31 { 32 if($int->head->mime_type eq"text/plain") 33 { 34 $path = $int->bodyhandle->path ; 35 open RUTF8, ("<".$path) or die $! ; 36 while (my $line = <RUTF8>) 37 { 38 $body = $body." ".$line ; 39 } 40 if($int->head->mime_type eq"multipart/alternative" ) 41 { 42 explore_mail ($int); 43 } 44 } 45 } 46 } 47 else 48 { 49 $body = $body.(join " ", @{$mpart->body}); 50 } 51 } 52 53 explore_mail $entity ; 54 55 if(!($body eq "")) 56 { 57 $sms = "[". (trim($from)) ."] ". (trim($subject)) ." >>> ".$body ; 58 $sms =~ s/\n/ /g; 59 $sms =~ s/ / /g; 60 $sms = substr($sms,0,400); 61 print ($sms); 62 } 63 $entity->purge;