Base64 email MIME decoder

Finally, I have (simultaneously) the need and the opportunity to find a perl script to decode the base64 MIME attachments in a set of email.

The script is used for GNATS (the GNU bugtracking system) to handle attachments and can be found here. It takes an email file from standard in and writes the message’s decoded components to an output_path subdirectory ($output_path has to be set, of course).

The necessary Perl modules aren’t all found on a standard Red Hat distribution; the one that has to be built is MIME::Parser. Luckily, someone has made a source RPM for perl-MIME-tools (which contains MIME::Parser as well as other things) available over at www.rpmfind.net. Note that perl-IO-stringy is required to build perl-MIME-tools.

Note that for relatively simple, one-shot decoding, the default Red Hat distro comes with MIME::Base64. With some file editing to get the base64 parts out of an email message, one can run a script like this without installing extra modules:

#!/usr/bin/perl

use MIME::Base64;

$infile = ‘test.b64’;
$outfile = ‘test.out’;

open(FH, “< $infile"); undef $/; $input = ;

close FH;

#$encoded = encode_base64($input);
$decoded = decode_base64($input);

open(FH, “>$outfile”);
print FH $decoded;

Comments are closed.