0

How can I have spamassasin check a mail folder for spam and move it into a different one.

I've been getting a lot of spam recently and started implementing bayesian classifiers. I moved a bunch into a separate folder and trained it with 'sa-learn'.

Now I want spamassasin to use what it just learned and check all email in a folder for spam and move it into the spam folder if it considers it spam. (e.g. the Spam-Level is higher than 5.0 in my configuration.)

EDIT: I am storing my emails with the maildir format on the server.

Any idea?

Thanks in Advance

1
  • IMAP folder or maildir folder?
    – AnFi
    Aug 17, 2014 at 19:39

1 Answer 1

1

Below please find combined sample code from man spamassassin and man Mail::Box.

Add $message->moveTo($destinationFolder) in appropriate places after initial tests. The current version merely identifies oversized/spam/ham.
(see man Mail::Box::Message for moveTo details)

usage: ./scriptname maildir:/home/username/Maildir/.Folder/

#!/usr/bin/perl
use strict;
use warnings;
use utf8;

use Mail::SpamAssassin;
use Mail::Box::Manager;

my $mgr    = Mail::Box::Manager->new;
my $spamtest = Mail::SpamAssassin->new();

for my $foldername (@ARGV) {
  my $folder = $mgr->open(folder => $foldername, access => 'rw' ) || die;

  foreach my $message ($folder->messages) {
    # Print Subject: and Date: headers as progress indicators
    print "Subject: ",  $message->subject(),"\n";
    print "Date: ",  $message->head->get('Date'),"\n";

    if( $message->size >= 250*1024 ) {
       print "Oversized Message\n";
       next;
    }
    my $mail = $spamtest->parse($message->string());
    my $status = $spamtest->check($mail);

    if ($status->is_spam()) {
      # $message = $status->rewrite_mail();
      print "Spam Message\n";
    } else {
      print "Ham Message\n";
    }
    $status->finish();
    $mail->finish();
  }
  $folder->close();
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .