MIME::Tools decoding of a MIME::Entity 
Author Message
 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



Wed, 10 Sep 2003 01:21:01 GMT  
 MIME::Tools decoding of a MIME::Entity

<snip snip>

Quote:
>         ##
>         # 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
>             #
>          }

<snip snip>

So I guess I'll answer my own question :)


        my( $type ) = $part->effective_type( );
        if ( $type =~ /^text/ ) {
            $part->bodyhandle->print( );
        } else {
            my( $encoding ) = $part->head->mime_attr(
'content-transfer-encoding' );
            my( $file ) = $part->head->mime_attr(
'content-disposition.filename' );
            if ( $file ) {
                my( $io ) = new IO::File( );
                $io->open( "> $ENV{'HOME'}/$file" );
                $part->bodyhandle->print( $io );
                $io->close( );
            }
        }
        print( "\n" );
    }

Quote:

> 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


--
Ti Leggett
Nortel Networks
SPM Strategic Development



Wed, 10 Sep 2003 03:48:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. MIME-Tools - MIME::Entity - extra newline problem

2. MIME::Parse and MIME::Entity

3. Please help with MIME::Parser and MIME::Entity

4. MIME::Tools and extracting entities via a pipe

5. Problem with MIME::Lite and MIME-tools

6. MIME::Tools - Getting All MIME Header Info for a Message

7. problems with MIME::Lite and MIME-tools

8. Problem with MIME::Lite and MIME-tools

9. MIME-tools/Mail-tools incompatible?

10. MIME:Entity - why so many modules wanted?

11. Problems w/ MIME/Entity.pm

12. perl MIME::Entity modules

 

 
Powered by phpBB® Forum Software