Create a gist now

Instantly share code, notes, and snippets.

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";
my $mb = Mail::MboxParser->new( MAILBOX_DIR, decode => 'ALL' );
print `date`;
my $msgnum = 1;
for my $msg ($mb->get_messages) {
print "\nmessage: $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