in reply to Re^8: How to parse outlook type attachment from inbox
in thread How to parse outlook type attachment from inbox
#!/usr/bin/perl use strict; use Mail::POP3Client; use MIME::Parser; use MIME::Head; use Mail::Header; #usage() unless scalar @ARGV == 3; my $pop = new Mail::POP3Client( HOST => 'server', USER => 'my', PASSWORD => 'pass' ); my $tmp_directory = "/tmp/attach"; my $parser = new MIME::Parser; $parser->output_dir($tmp_directory); $parser->output_prefix("attachment"); $parser->output_to_core(); open (FH,">>/tmp/msgtxt1"); for (my $i = 1; $i <= $pop->Count(); $i++){ my $head = $pop->Head($i); if ($head =~ /X-MS-Has-Attach: yes/i){ foreach ( $pop->Head( $i ) ) { if( /^(Subject):\s+/i ) { if ( /complaint about message from/) { my $msg = $pop->HeadAndBody($i); ### Automatically attempt to RFC-1522- +decode the MIME headers? $parser->decode_headers(1); + ### default is false ### Parse contained "message/rfc822" o +bjects as nested MIME streams? $parser->extract_nested_messages(1); + ### default is true ### Look for uuencode in "text" messag +es, and extract it? $parser->extract_uuencode(1); + ### default is false ### Should we forgive normally-fatal e +rrors? $parser->ignore_errors(0); + ### default is true my $entity = $parser->parse_data($msg) +; #$entity->print_header() my $head = $entity->head; my $msg_summary = " Message Summar +y for message \n" . " From : " . $head->get('From') +. " To : " . $head->get('To') . " Subject: " . $head->get('Subject +') . " Date : " . $head->get('Date') +. "\n"; print " -- $msg_summary \n"; my @parts = $entity->parts; my $body; my @attachments; # If this is a single part message, we + should just append it # (To be handled later) unless( $entity->parts ){ # Single part. Just add it as a comment $body = $entity->stringify_bod +y; #print " -- $body \n"; } else { # Multi-part. # Assume the first part is the + comment body and any additional # parts are the attachments @attachments = $entity->parts; $body = $attachments[0]->strin +gify_body; } for my $attachment ( @attachments ) { my $headers = $attachment->hea +d; print " - $attachment->bodyhan +dle->as_string()\n"; #print " -- $attachment --- +\n"; } #$entity->dump_skeleton; ### Get the head, a MIME::Head: } } } # print "\n"; } } close(FH); $pop->Close(); my $dirtoget="/tmp/attach/"; opendir(IMD, $dirtoget) || die("Cannot open directory"); my @thefiles= readdir(IMD); foreach my $fl (@thefiles) { if ($fl =~ m/msg/){ &readFile($fl); } } sub readFile { my ($filename) = @_; $filename = $dirtoget.$filename; open(MSG,"$filename") || die ("Cannot Open File for Reading\n" +); while (<MSG>) { my $ln = $_; if ($ln =~ m/^To: /){ print $ln; } } close(MSG); }
|
|---|