Implementing a second level Spam folder with Kerio Connect


2012/03/29

Kerio Connect mail server assigns a "spam score" to messages. In the Administration panels, you set "Tag" and "Block" limits for Spam. For example, I have "Tag" set at 3 and block at 8 on my server.


Kerio Spam settings

As configured, anything with a score of 3 or greater gets marked as "**SPAM**" and a default rule tucks those messages into my "Junk E-mail" folder. If the score reaches 8, the message gets blocked outright and I never see it.

That's fine, but there are an awful lot of messages with positive spam scores that may or may not be spam, but are probably less important to me. I don't necessarily want them to go to the Junk E-Mail folder, but I don't want to look at them before I look at the important ones. It would be nice if I could sort by the spam score, but none of my email clients can do that, so I came up with another way.

This is a Perl script I call "spammish.pl". It uses the "Mail::IMAPClient" module from CPAN to move selected messages from INBOX (or any other folder you specify) to another folder. As presented here, it selects messages with a spam score greater than 1 and moves them to a "Spammish" folder (which must already exist).

Effectively, that's a second level spam folder. It's messages that are likely far less important to me than messages with lesser (or negative) spam scores. I can look at them in "Spammish" after I look at the more important messages in INBOX.

Note that you might want to turn this upside down: create a "Important" folder and move messages to that if their score is UNDER a certain level.

If you want to play with this, I suggest creating "Testing" and "Spammish" folders, editing the script for your credentials, and testing it by manually moving messages into "Testing" before running the script.

After it runs, you can examine "Testing" and 'Spammish" to see what it did. Using your normal mail client, you could then drag messages from Spamish back to Testing, edit the code, and run it again until you are satisfied with its behavior.

Caution

I recommend extreme caution. I just dashed this off today and have not tested it extensively. It's meant to be an example of the kind of thing you can do using the Mail::IMAPClient module and not necessarily a ready to use script. A production version would take arguments and do much more error checking.

Because I did NOT do much in the way of checking, this code will tend to spit out "uninitialized variable" messages if you point it at folders that don't exist. Production code should be more forgiving.

This script uses plaintext login; the Mail::IMAPClient will support other methods; see the CPAN docs.

If you are going to get ambitious with this sort of programming, you should read RFC 2683 and RFC 3501 at a minimum.

That all noted, feel free to take the ideas here and use them.

Here is the script:

#!/usr/bin/perl -w

use strict;
use Mail::IMAPClient;

my $inbox="Testing";
my $spammish="Spammish";
my $host="yourmailserver.com";
my $user="your_login";
my $passw="your_password";
my $moveit=0;



my $imap = Mail::IMAPClient->new(
				 Server   => $host,
				 User     => $user,
				 Port     => 143,
				 Password => $passw,
				 Peek     => 1,
				) or die "Cannot connect: $@";

 $imap->select($inbox);
 my @my_messages = $imap->search('UNDELETED');
 foreach my $message (@my_messages)
 {
  my $data = $imap->parse_headers($message,"Subject","From","X-Spam-Status");
  my $address = $data->{From}->[0];
  my $spam=$data->{'X-Spam-Status'}->[0];
  if (defined $spam) {
    $spam=~ s/.*TOTAL_SCORE://;
    $spam=~ s/,.*//;
    $moveit=1 if $spam > 1;
   }
  next if not $moveit;
  
  printf "%s %s\n", ((defined $data->{Subject}->[0]) ? $data->{Subject}->[0] : ''),
((defined $data->{'X-Spam-Status'}->[0]) ? $data->{'X-Spam-Status'}->[0] : '') ;
   print "Moving message $message to $spammish\n";
   print "Wanted to but could not move message $message: $!" unless $imap->move($spammish, $message);
   $moveit=0;
 }

$imap->expunge();
$imap->close();
$imap->logout();

Sample output:

This report might affect your future decisions No, hits=1.2
required=3.0       tests=BAYES_40: -0.276,HTML_MESSAGE:
0.001,MIME_HTML_ONLY: 0.001,        RDNS_NONE:
0,URIBL_JP_SURBL: 1.501,TOTAL_SCORE: 1.227,autolearn=no
Moving message 220 to Spammish
Are you eligible for back surgery compensation? No,
hits=1.4 required=3.0      tests=BAYES_00:
-1.665,FB_TO_STOP_DISTRO: 3.096,HTML_MESSAGE: 0.001,
MIME_HTML_ONLY: 0.001,RDNS_NONE: 0,TOTAL_SCORE:
1.433,autolearn=no
Moving message 221 to Spammish
And you say you're smart? Can you speak a foreign
language? No, hits=1.3 required=3.0   tests=BAYES_00:
-1.665,HTML_MESSAGE: 0.001,MIME_HTML_ONLY: 0.001,
RDNS_NONE: 0,URIBL_JP_SURBL: 1.501,URIBL_WS_SURBL: 1.5,
TOTAL_SCORE: 1.338,autolearn=no
Moving message 222 to Spammish
Did you or anyone you know have back/neck
surgery? No, hits=2.9 required=3.0   tests=BAYES_00:
-1.665,FB_TO_STOP_DISTRO: 3.096,HTML_MESSAGE: 0.001,
MIME_HTML_ONLY: 0.001,RDNS_NONE: 0,URIBL_JP_SURBL: 1.501,
TOTAL_SCORE: 2.934,autolearn=no
Moving message 223 to Spammish
Man-High Within Months... No, hits=1.2 required=3.0
tests=BAYES_40: -0.276,RDNS_NONE: 0,URIBL_WS_SURBL: 1.5,
TOTAL_SCORE: 1.224,autolearn=no
Moving message 224 to Spammish
 

Also see Implementing a second level Spam folder with Kerio Connect



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Implementing a second level Spam folder with Kerio Connect




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





If you have any trouble sounding condescending, find a Unix user to show you how it's done. (Scott Adams)

A learning experience is one of those things that say, "You know that thing you just did? Don't do that." (Douglas Adams)








This post tagged: