#/usr/bin/perl -w use MIME::Parser; my $parser = new MIME::Parser; $parser->decode_bodies(0); $parser->decode_headers(0); $parser->output_to_core(1); $parser->tmp_to_core(1); local($/) = undef; # slurp open(EMAIL, "63602021-5.eml"); binmode EMAIL; $entity = $parser->parse_data(<EMAIL>); close(EMAIL); dump_entity($entity) if $entity; sub dump_entity{ my $ent = shift; my @parts = $ent->parts; if (@parts) { map { dump_entity($_) } @parts; } else { if(scalar($ent->head->mime_type) eq "application/pdf"){ print "Part: " . $ent->head->recommended_filename . " (" . scalar($ent->head->mime_type) . ")\n"; my $data_org = $ent->bodyhandle->as_string(); print length($data_org) . "\n"; $data_org = fixbase64($data_org); print length($data_org) . "\n"; writefile("/tmp/test.b64",$data_org); $data_bin = decode_base64($data_org); print length($data_bin) . "\n"; } } } sub writefile{ my $file = shift; my $data = shift; open(HANDLE,">$file"); local($/) = undef; binmode HANDLE; print HANDLE $data; close(HANDLE); } sub fixbase64{ my $str = shift; my $res = ""; $str =~ tr|A-Za-z0-9+=/||cd; while ($str =~ /(.{1,76})/gs) { $res .= $1 . "\n"; } return $res; } sub decode_base64{ local($^W) = 0; my $str = shift; my $res = ""; $str =~ tr|A-Za-z0-9+=/||cd; if (length($str) % 4) { warn("Length of base64 data [" . length($str) . "] not a multiple of 4"); } $str =~ s/=+$//; $str =~ tr|A-Za-z0-9+/| -_|; while ($str =~ /(.{1,60})/gs) { my $len = chr(32 + length($1)*3/4); $res .= unpack("u", $len . $1 ); } return $res; }
In reply to MIME::Parser odd PDF error. by cyberdoc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |