Skip to content

Instantly share code, notes, and snippets.

@dingsdax
Created November 30, 2011 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingsdax/1408565 to your computer and use it in GitHub Desktop.
Save dingsdax/1408565 to your computer and use it in GitHub Desktop.
get & store all attachments in a mailbox directory
#!/usr/bin/perl -w
use strict;
use Mail::MboxParser;
use constant MAILBOX_DIR => "/var/mail/mymailboxdir";
use constant STORAGE_DIR => "/data";
if ( -z MAILBOX_DIR ) {
print STDERR "no mail.\n";
exit;
}
my $mb = Mail::MboxParser->new( MAILBOX_DIR, decode => 'ALL' );
print `date`;
my $msgnum = 1;
for my $msg ($mb->get_messages) {
print "\nmessage: $msgnum ";
$msgnum++;
print $msg->header->{subject} if defined $msg->header->{subject};
print "\n";
my $mapping = $msg->get_attachments;
for my $filename (keys %$mapping) {
print " * storing $filename ...\n";
$msg->store_attachment( $mapping->{$filename},
path => STORAGE_DIR )
}
}
print "\n-----------------------------\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment