Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created June 6, 2013 18:27
Show Gist options
  • Save briandfoy/5723753 to your computer and use it in GitHub Desktop.
Save briandfoy/5723753 to your computer and use it in GitHub Desktop.
use v5.10;
use Email::MIME;
use File::Basename;
my $message = do { local $/; <> };
my $parsed = Email::MIME->new( $message );
my $count = 1;
foreach my $part ( $parsed->parts ) {
my $decoded = $part->body;
my $content_type = $part->content_type;
my $filename = sprintf "%04d.%s", $count++, (split /\//, $content_type)[1];
if( my $content_location = $part->header( 'Content-Location' ) ) {
$filename = basename( $content_location );
}
say "$content_type -> $filename";
open my $fh, '>', $filename;
print { $fh } $part->body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment