Last quarter I graded labs for a course known as Bioinformatics Computing I. The students mailed me their labs on a daily basis, and I saved the labs and all of the associated e-mails in an mbox file on my home directory. The professor for the course wanted all of the labs so that he could read see where students were confused and improve the course, so I wrote this Perl script in about an hour save all attachments from everyone who sent me a lab into a reasonably-named directory.
#!/usr/bin/perl -w use strict; use Mail::MboxParser; my @folders = glob '*'; @folders = grep /^lab\d+$/, @folders; foreach ( @folders ) { &save_bic_messages( $_ ); } sub save_bic_messages { my $mail_folder = shift; # create the mailbox parser my $mailbox = Mail::MboxParser->new( $mail_folder, decode => 'ALL' +); # iterate through the messages, save all attachments in a folder ba +sed on # each person's name for my $msg ($mailbox->get_messages) { my %header = %{ $msg->header }; my $from = $header{ 'from' }; # do some magic to get a reasonable name from the addresses my $name; if ( $from =~ /(.*)\s+\<.*\>/ ) { $name = $1; } elsif ( $from =~ /^([A-Z0-9a-z]+)\@/ ) { $name = $1; } else { $name = $from; } $name =~ s/\"//g; # remove quotation marks from names # make sure that the directory for this folder exists my $save_dir = "bic1_$mail_folder"; if ( ! -d $save_dir ) { mkdir $save_dir; } # create a directory for each of these names $save_dir = "$save_dir/$name"; if ( ! -d "$save_dir" ) { mkdir "$save_dir" or warn "Error creating directory $save_dir +"; } # save all of the attachments in the directory $msg->store_all_attachments(path => "$save_dir") ; } }

In reply to Parsing an mbox File for Labs by biosysadmin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.