#!/usr/bin/perl # Print the types of messages in the folder. Multi-part messages will # be shown with all their parts. # # This code can be used and modified without restriction. # Mark Overmeer, <********@overmeer.net>, 9 nov 2001 use warnings; use strict; use lib '..', '.'; use Mail::Box::Manager 2.00; use Email::Address; use File::Slurp qw( write_file ); my $outfile = 'multipart4_out.txt'; sub show_type($;$); # # Get the command line arguments. # die "Usage: $0 folderfile\n" unless @ARGV==1; my $filename = shift @ARGV; # # Open the folder # my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open ( $filename , extract => 'LAZY' # never take the body unless needed ); # which saves memory and time. die "Cannot open $filename: $!\n" unless defined $folder; # # List all messages in this folder. # my @messages = $folder->messages; print "Mail folder $filename contains ", scalar @messages, " messages:\n"; my $counter = 1; foreach my $message (@messages) { printf "%3d. ", $counter++; print $message->get('Subject') || '', "\n"; show_type $message; get_header_values($message); } sub get_header_values($) { my $msg = shift; # return all Mail::Message objects my @from = $msg->from; my $sender = $msg->sender; my $subject = $msg->subject; my $msgid = $msg->messageId; my @to = $msg->to; my @cc = $msg->cc; my @bcc = $msg->bcc; my @dest = $msg->destinations; my $replyto = $msg->get('Reply-To'); # need code here to use @from, @to, @cc , @bcc and $replyto, and push out those emails (name/email address) to an array, # then output to $outfile, like below #File::Slurp::write_file( $outfile, {append => 1 }, join("\n", @emails) ); } sub show_type($;$) { my $msg = shift; my $indent = (shift || '') . ' '; # increase indentation print $indent, " type=", $msg->get('Content-Type'), ', ' , $msg->size, " bytes\n"; if($msg->isMultipart) { foreach my $part ($msg->parts) { my $content_type = $part->contentType; # the only parts we are interested in are text and html - bypass all others if (defined($content_type) && $content_type eq 'text/plain') or (defined($content_type) && $content_type eq 'text/html') { my @emails = Email::Address->parse($part->body); #this isn't working properly #File::Slurp::write_file( $outfile, {append => 1 }, join("\n", @emails) ); } show_type $part, $indent; } } } # # Finish # $folder->close;