MIME::Explode(3) Perl extension for explode MIME messages


use MIME::Explode;
my $explode = MIME::Explode->new(
output_dir => "tmp",
mkdir => 0755,
decode_subject => 1,
check_content_type => 1,
content_types => ["image/gif", "image/jpeg", "image/bmp"],
types_action => "exclude"
print "Number of messages: ", $explode->nmsgs, "\n";
open(MAIL, "<file.mbox") or
die("Couldn't open file.mbox for reading: $!\n");
open(OUTPUT, ">file.tmp")
or die("Couldn't open file.tmp for writing: $!\n");
my $headers = $explode->parse(\*MAIL, \*OUTPUT);
for my $part (sort{ $a cmp $b } keys(%{$headers})) {
for my $k (keys(%{$headers->{$part}})) {
if(ref($headers->{$part}->{$k}) eq "ARRAY") {
for my $i (0 .. $#{$headers->{$part}->{$k}}) {
print "$part => $k => $i => ", $headers->{$part}->{$k}->[$i], "\n";
} elsif(ref($headers->{$part}->{$k}) eq "HASH") {
for my $ks (keys(%{$headers->{$part}->{$k}})) {
if(ref($headers->{$part}->{$k}->{$ks}) eq "ARRAY") {
print "$part => $k => $ks => ", join(($ks eq "charset") ? " " : "", @{$headers->{$part}->{$k}->{$ks}}), "\n";
} else {
print "$part => $k => $ks => ", $headers->{$part}->{$k}->{$ks}, "\n";
print "$part => $k => $ks => ", $headers->{$part}->{$k}->{$ks}, "\n";
} else {
print "$part => $k => ", $headers->{$part}->{$k}, "\n";
if(my $e = $explode->clean_all()) {
print "Error: $e\n";


MIME::Explode is perl module for parsing and decoding single or multipart MIME messages, and outputting its decoded components to a given directory ie, this module is designed to allows users to extract the attached files out of a MIME encoded email messages or mailboxes.


new([, OPTION ...])

This method create a new MIME::Explode object. The following keys are available:
Directory where the decoded files are placed
mkdir => octal_number
If the value is set to octal number then make the output_dir directory (example: mkdir => 0755).
check_content_type => 0 or 1
If the value is set to 1 the content-type of file is checked
decode_subject => 0 or 1
If the value is set to 1 then the subject is decoded into a list.

  $header->{'0.0'}->{subject}->{value} = [ARRAYREF];
  $header->{'0.0'}->{subject}->{charset} = [ARRAYREF];
  $subject = join("", @{$header->{'0.0'}->{subject}->{value}});
exclude_types => [ARRAYREF]
Not save files with specified content types (deprecated in next versions)
content_types => [ARRAYREF]
Array reference with content types for ``include'' or ``exclude''
types_action => "include" or "exclude"
If the action is a ``include'', all attached files with specified content types are saved but if the action is a ``exclude'', no files are saved except if its in the array of content types. If no array is specified, but the action is a ``include'', all attached files are saved, otherwise all files are removed if action is a ``exclude''. The default action is ``include''.


This method parse the stream and splits it into its component entities. This method return a hash reference with all parts. The FILEHANDLE should be a reference to a GLOB. The second argument is optional.


Returns the number of parsed messages.


Cleans all files from the ``output_dir'' directory and then removes the directory. If an error happens returns it.


Henrique Dias <[email protected]>


Thanks to Rui Castro for the revision.