SirCharles has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to decode an email that was pop'd. After writing the body to a disk file using the perl Mail::POP3Client routine

$pop->BodyToFile( $fh, 1);

It looks all like text. So, I run following command to translate body of mime message back to pdf for reading.

perl -MMIME::Base64 -ne 'print decode_base64($_)' <test.txt >test.pdf

No error messages, but file size is different from original file poped from email attachment. When pdf file is brought up in browser, there is no data.

Edited by planetscape - added code tags and rudimentary formatting

Replies are listed 'Best First'.
Re: Mail::POP3Client and pdf decode
by jdtoronto (Prior) on Apr 06, 2006 at 19:30 UTC
    That method will give you the entire body, not just the PDF. I am in a rush, but have a look at MIM::Parser, I use it with Net::POP3 like this (which I think came from Lincoln Steins "Network Programming with Perl"):
    package Email::PopParser; use strict; use Net::POP3; use MIME::Parser; use vars '@ISA'; @ISA = qw(Net::POP3); # over-ride Net::POP3 new() method sub new { my $pack = shift; return unless my $self = $pack->SUPER::new(@_); my $parser = MIME::Parser->new(); $parser->output_dir( $ENV{TEMP} || '/tmp'); $self->parser($parser); $self; } # accessor for parser sub parser { my $self = shift; ${*$self}{'pp_parser'} = shift if @_; return ${*$self}{'pp_parser'} } # over-ride get() sub get { my $self = shift; my $msgnum = shift; my $fh = $self->getfh($msgnum) or die "Can't get message: ",$self->m +essage,"\n"; return $self->parser->parse($fh); } 1;
    Then using the parser methods you can see if the email is a MIME multipart, what MIME-Type each part is then you can get the body of the part and handle it. I use this where I am receviing voicemail files as wav's by email and want to save them to disk to be linked in an application.

    jdtoronto