http://qs321.pair.com?node_id=392546

ahakim has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm having trouble with the Mail::Box::Manager modules in perl. What I'm trying to do is write a script which parses through an mbox mail file and pulls out certain messages based on the date they were written. The script should then add those messages to a new folder. I keep getting error messages which say that there are no methods for moving messages from one mbox to another. My code is below. Anybody have any ideas? Thanks.

Ali
#!/usr/local/bin/perl #usage: ./ml.pl alihakim 2003-05-01 2003-05-31 $filename = shift; #$filename2 = shift; use Date::Calc qw( Date_to_Days Time_to_Date ); use Date::Parse qw( str2time ); use Time::CTime qw( strftime asctime ); use lib "..."; use Mail::Box::Manager; my $start_time = shift; $start_time =~ m/^\d{4}-\d{2}-\d{2}$/ or die; $start_time = str2time( "$start_time 00:00:00" ); my $stop_time = shift; $stop_time =~ m/^\d{4}-\d{2}-\d{2}$/ or die; $stop_time = str2time( "$stop_time 00:00:00" ); #print "20: $start_time\n"; #print "21: $stop_time\n"; $mgr = Mail::Box::Manager->new(); my $ali = Mail::Box::Mbox->new(); $folder = $mgr->open ('mbox',$filename); my $new_folder;# = $ali->open('test','file','./',"create","1","access" +,"rw"); foreach $message ($folder->messages()) { $curr_msg++; my $time = $message->guessTimestamp(); my $atime = asctime (localtime($time)); if (($time >= $start_time) && ($time <= $stop_time)) { $ali->addMessage($message); } } $folder->close();

Replies are listed 'Best First'.
Re: using Mail::Box::Manager;
by xdg (Monsignor) on Sep 21, 2004 at 12:29 UTC

    You want to use moveMessage. RT(F)M for Mail::Box::Manager -- the synopsis shows both copy and move methods.

    Mail::Box gets a bad rap because it's incredibly complicated and thus has a pretty steep learning curve. It's benefit is kind of like that of Date::Parse -- it can pretty much handle anything thrown at it. Simon Cozen's new Email::Simple and family is precisely that -- simple email handling for when you don't need the extra robustness and don't violate any assumptions it's written under. Mail::Box has a very extensive test suite. While that's not necessarily a sign of quality just by its existence, it does give some confidence that it will operate under a wide range of inputs and can handle whatever junk format email the net sends your way.

    Anytime you're stuck on Mail::Box, you should check out the Mail::Box web documentation browser. It's part of that steep learning curve but helps tremendously at times in finding the right method in the right class to do what you want.

    For reference, I'll post my mbox-datefilter script that I use on my system. It copies rather than moves, but I'll also post mbox-movemessages which shows how to move messages (sets folders to read/write, removes them if empty, etc.) Between the two you should be able to figure out how to write what you want.

    -xdg

    Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Re: using Mail::Box::Manager;
by punkish (Priest) on Sep 21, 2004 at 11:13 UTC
    Ali,

    From the docs at CPAN, I don't even see a method called addMessage (maybe I missed it). The syntax for moving a message is

    $obj->moveMessage([FOLDER|FOLDERNAME,] MESSAGES, OPTIONS)
    which actually moves the message and marks it for deletion, so it is deleted from the source folder when the folder is closed.

    Hope this helps.

    Also, I've had mixed success with all the Mail-Box modules (otoh, I was working with Exchange server, which might have had its own flavor of voodooo). I read a while back on Perl.com about some new mail handling modules that Simon Cozens had started (because he too had mixed success with the existing Mail modules) which were supposed to be better. If your life depends on the above script, you would be well advised to utilize a lot of error-checking and success-failure checking, or use more reliable modules.

Re: using Mail::Box::Manager;
by PodMaster (Abbot) on Sep 21, 2004 at 10:31 UTC
    What's the exact error message you get? I think you need to create two Mail::Box::Manager's, or maybe use moveMessage?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.