public
Created

get & store all attachments in a mailbox directory

  • Download Gist
get_attachments.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/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";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.