Submitted by stompro on Tue, 07/03/2007 - 9:23am.
#!/usr/bin/perl
#This script uses mail::Box to forward all email in a mailbox to spamcop, or anywhere else
#you want, and then moves the mail to a different directory.
#
#Settings
my $inbox = '/var/mail/inbox';
my $trash = '/var/mail/inbox/.Junk2';
my $forwardaddr = 'spamcop@microsoft.org';
use Mail::Box::Manager;
my $mgr = Mail::Box::Manager->new;
#open folder where spam is trapped
my $folder = $mgr->open(folder => $inbox,
access=>'rw',accept_new=>1,
remove_when_empty=>0
) || die "Cannot open inbox\n";;
#open folder where spam should be stored after it is forwarded
my $folder1 = $mgr->open(folder => $trash,
access=>'rw',
remove_when_empty=>0,
extract=> 'LAZY'
) || die "cannot open trash\n";
#Go through each message, for each one, print the sender and the first received header,
#then create a forward as attachment, the send the foward.
#last thing is to move the message to the storage folder.
foreach ($folder->messages) {
print "Sender->".$_->sender->format."\n";
print "Received Header 1->".$_->head->get('Received',0)."\n";
my $body = Mail::Message::Body::Lines->new(data => "Forward spam from stompro-spam");
my Mail::Message $fwd = $_->forwardEncapsulate(To => $forwardaddr, preamble => $body) || die "could not create forward";
$fwd->send || die "Could not send message, aborting";
my $copy = $_->moveTo($folder1) || die "could not move message to junk folder, aborting";
}