
MIME::Tools decoding of a MIME::Entity
Hello there! I'm currently trying to write a
(hopefully) full-featured mail program in perl. The MIME::Tools modules
are quite, *quite* handy, but I have one question: I've currently
figured out how to send MIME encoded messages using MIME::Tools but I
can't quite figure out exactly how to decode a multipart message once
it's received. Here's the code:
#!/usr/bin/perl -w
use Mail::POP3Client;
use IO::File;
use MIME::Parser;
use MIME::Decoder;
use strict;
my( $tmp_dir ) = "/tmp/POP-$ENV{'USER'}";
unless ( -d $tmp_dir ) {
mkdir( $tmp_dir );
Quote:
}
my( $pop ) = new Mail::POP3Client( USER => 'foo',
PASSWORD => 'bar',
HOST => "mailhost",
PORT => 110,
TIMEOUT => 60 );
$pop->Connect( ) || die $pop->Message( );
my( $parser ) = new MIME::Parser( );
$parser->output_under( "$tmp_dir" );
my( $msg_count ) = $pop->Count( );
print( "You have $msg_count new messages.\n" );
for ( my $i = 1; $i <= $msg_count; $i++ ) {
print( "Message #$i\n" );
my( $tmp_file ) = "mail$msg_count";
my( $fh ) = new IO::File( );
$fh->open( "> $tmp_dir/$tmp_file" );
unless ( defined $fh ) {
print( STDERR "Could not open $tmp_dir/$tmp_file for
writing.\n" );
exit( -1 );
}
$pop->RetrieveToFile( $fh, $i );
$fh->close( );
$fh->open( "< $tmp_dir/$tmp_file" );
my( $message ) = $parser->parse( $fh );
$fh->close( );
my( $head ) = $message->head( );
my( $type ) = $message->mime_type( );
my( $efftype ) = $message->effective_type( );
my( $subject ) = $head->get( 'Subject', 0 );
##
# Here is the multipart section
#
if ( $message->is_multipart( ) ) {
my( $preamble ) = $message->preamble( );
my( $epilogue ) = $message->epilogue( );
print( "Subject: $subject\n" );
##
# Here's where I try to decode each part
#
my( $type ) = $part->effective_type( );
my( $encoding ) = $part->get( 'Content-Transfer-Encoding',
0 );
print( "Type: $type\n" );
print( "Encoding: $encoding\n" ) if $encoding;
my( $decoder ) = new MIME::Decode( $encoding );
##
# Here's where I don't know what to put
#
}
print( "\n" );
} else {
print( "Subject: $subject\n" );
print( "Type: $type\n" );
print( "Effective: $efftype\n" );
# $message->bodyhandle( )->print( );
print( "\n" );
}
Quote:
}
$pop->Close( );
exit( 0 );
So I guess my question is really this, how do I take an already existing
MIME::Entity and decode it properly? What's the best way to determine
how to decode (I currently use "my( $encoding ) = $part->get(
'Content-Transfer-Encoding', 0 );" ). Any other suggestions would be
greatly appreciated! Thanks in advance!
--
Ti Leggett
Nortel Networks
SPM Strategic Development