Explode(3pm)

πŸ’– NAME πŸ’–

okay so bestie, gather round β€” this is MIME::Explode, a Perl extension for explode MIME messages. no cap, that's literally the whole vibe. πŸŽ€


🌸 SYNOPSIS 🌸

here's how you actually use it, copy-paste girlies rise:

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);
close(OUTPUT);
close(MAIL);

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";
}

✨ DESCRIPTION ✨

MIME::Explode is a perl module for parsing and decoding single or multipart MIME messages, and outputting its decoded components to a given directory. basically?? it's giving "extract the attached files out of a MIME encoded email messages or mailboxes" energy β€” that's the whole point of it, and the module is designed to allow users to do exactly that. πŸ’…


🫢 METHODS 🫢

new([, OPTION ...])

this method create a new MIME::Explode object. slay. here are the keys you can pass, all of them, no skips:

  • output_dir β€” 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). like it literally goes and makes the folder for you, iconic.
  • 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. so you get:
$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) ⚠️ yeah bestie, deprecated, so don't get too attached.
  • 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". πŸ’–

parse(FILEHANDLE, FILEHANDLE)

this method parse the stream and splits it into its component entities. it return a hash reference with all parts. the FILEHANDLE should be a reference to a GLOB. the second argument is optional. yes queen, optional, you can just not. 🌈

nmsgs

Returns the number of parsed messages. that's it, that's the whole method, we love a one-liner. πŸ’€

clean_all

Cleans all files from the "output_dir" directory and then removes the directory. If an error happens returns it. so it's not just cleaning, it's deleting β€” she takes the whole folder with her. 🧚


πŸ‘‘ AUTHOR πŸ‘‘

Henrique Dias <henrique.ribeiro.dias@gmail.com>

πŸŽ€ CREDITS πŸŽ€

Thanks to Rui Castro for the revision. shoutout to the bestie who proofread, fr fr. 🫢

πŸ¦‹ SEE ALSO πŸ¦‹

MIME::Tools, perl(1).


and that's the whole page, bestie!! this manual was brought to you by perl v5.42.3, dated 2026-08-19 β€” go forth and explode those MIME messages, you absolute main character πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from