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.

mbox-datefilter

#!/usr/bin/perl -w use strict; use Getopt::Std; use Date::Manip qw( ParseDate Date_Cmp); use Mail::Box::Manager; my %opts = ( "i" => undef, "o" => undef, "f" => undef, "t" => undef ); getopts("i:o:f:t:", \%opts); die "Usage: $0 -i <input mailbox> -o <output mailbox> -f <from date (i +nclusive)> -t <to date (exclusive)>\n" unless ($opts{i} and $opts{o} and ( $opts{f} or $opts{t} ) ); my ($from_date, $to_date); $opts{f} and ( $from_date = ParseDate($opts{f}) or die "Bad from date: + $opts{f}\n" ); $opts{t} and ( $to_date = ParseDate($opts{t}) or die "Bad to date: $op +ts{t}\n" ); my $mbm = Mail::Box::Manager->new(); my $inbox = $mbm->open($opts{i}, access => 'r') or die "Can't open $opts{i}\n"; my $outbox = $mbm->open($opts{o}, access => 'rw', create => 1) or die "Can't open $opts{o}\n"; my $i = 0; for my $msg ($inbox->messages) { my $msg_date = ParseDate($msg->date->body); if ( ( ( $from_date and Date_Cmp($from_date,$msg_date) <= 0 ) or ( + ! $from_date ) ) and ( ( $to_date and Date_Cmp($msg_date,$to_date) < 0 ) or ( ! $t +o_date ) ) ) { $mbm->copyMessage($outbox, $msg); ++$i; } } $outbox->write(); print "Copied $i messages from $opts{i} to $opts{o}\n"; print "$opts{i} contains " . $inbox->messages . " messages.\n"; print "$opts{o} contains " . $outbox->messages . " messages.\n"; $inbox->close(); $outbox->close(); exit;

mbox-movemessages

#!/usr/bin/perl -w use strict; use Getopt::Std; use Mail::Box::Manager; my %opts = ( "i" => undef, "o" => undef, "c" => undef ); getopts("i:o:c:", \%opts); die "Usage: $0 -i <input mailbox> -o <output mailbox> -c <# messages t +o move>\n" unless ($opts{i} and $opts{o} and $opts{c}); my $mbm = Mail::Box::Manager->new(); my $inbox = $mbm->open($opts{i}, access => 'rw', remove_when_empty => +0) or die "Can't open $opts{i}\n"; my $outbox = $mbm->open($opts{o}, access => 'rw', create => 1) or die "Can't open $opts{o}\n"; my $num = $opts{c}; my $i = 0; for my $msg ($inbox->messages) { $mbm->moveMessage($outbox, $msg); last if ++$i >= $num; } $inbox->write(); $outbox->write(); print "Moved $i messages from $opts{i} to $opts{o}\n"; print "$opts{i} contains " . $inbox->messages . " messages.\n"; print "$opts{o} contains " . $outbox->messages . " messages.\n"; $inbox->close(); $outbox->close(); exit;

-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.


In reply to Re: using Mail::Box::Manager; by xdg
in thread using Mail::Box::Manager; by ahakim

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.