IMAP and Outlook Spam training

From Zimbra :: Wiki

(Redirected from Spam training)
Jump to: navigation, search
   Article-alert.png  - This article is a community contribution and may include unsupported customizations for network edition customers.


Admin Article

Article Information

This article applies to the following ZCS versions.
  ZCS 5.0 Article  ZCS 5.0
  ZCS 4.5 Article  ZCS 4.5

ZCS 5.0.11+

ZCS 5.0.11+ Junk/Spam folders are 'item-move-aware' per bug 9532. In other words, when you move mail into your Junk/Spam folder it will be "queued for processing by the spam filter/trainer". Previously, this was NOT the case.


Prior to ZCS 5.0.11

IMAP/Outlook move to junk doesn't train anti-spam - It seems that when you move a mail to junk folder on IMAP client (like Thunderbird) anti-spam filter on MTA does not use that mail as a spam example for training... See bug 9532

Here is a bash script created from the comments against this bug. I have tested it on our system and it appears to work OK. Feel free to contribute and enhance! Full credit for this script is due to bobby@zimbra.com and dmangot@terracottatech.com who wrote the bits which I just pasted together!

#!/bin/bash

wikiuser=`zmprov getConfig zimbraNotebookAccount | cut -d ' ' -f 2`;
hamuser=`zmprov getConfig zimbraSpamIsNotSpamAccount | cut -d ' ' -f 2`;
spamuser=`zmprov getConfig zimbraSpamIsSpamAccount | cut -d ' ' -f 2`;

users=`zmprov getAllAccounts | grep -v -e $wikiuser -e $hamuser -e $spamuser`;

for zuser in $users
do
echo "Train spam for $zuser"
/opt/zimbra/bin/zmtrainsa $zuser spam junk
done

echo "Zimbra spam training complete"

To install, save the above script as 'learnmorespam.sh', copy to /opt/zimbra/learnmorespam.sh on your Zimbra server. Set ownership and make executable:

[root@mail ~]# chmod +x /opt/zimbra/learnmorespam.sh
[root@mail ~]# chown zimbra:zimbra /opt/zimbra/learnmorespam.sh

And then create this crontab entry, /etc/crontab

#Learn more spam 6am daily
0 6 * * * zimbra /opt/zimbra/learnmorespam.sh > /dev/null 2>&1

That should be it :)

Another:

#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
my $host="<edit>";
my $username="<edit>";
my $password="<edit>";
my @real_users=`/opt/scalix/bin/omshowu -m all -i`;     # get all real user names.
foreach my $punter (@real_users)                        # Loop over them all.
{
        chomp $punter;                                  # Remove trailing carriage return.
        if ("$punter" ne "postmaster")
        {
        print "$punter\n";                              # Some output. Feel free to remove.
        #$punter =~s/@/\\@/;
        my $user="mboxadmin:$username:$punter";         # Set up superuser login.
        my $imap  = new Mail::IMAPClient( 'Server' => $host , 'User' => $user , 'Password' => $password  ) or next;  # connect to server.
        my @folders=$imap->folders;                     # list folders.
        foreach my $i ( @folders ) { print $i; }
        foreach  my $folder (@folders)                  # Look through each of them.
        {
                print lc($folder),"\n";
                if (lc($folder) eq "junk e-mail")                                                                    # "junk email" folder.
                {
                        print "Found a spam folder: $folder\n";
                       $imap->select($folder) or next;                                                                  # Select the folder.
                        print "Folder $folder selected.\n";
                        my @list=$imap->messages or next;                                                              # List all messages in folder.
                        print scalar(@list)." messages in folder.\n";
                        foreach my $msg (reverse(@list))                                                                # Loop over them all.
                        {
                                my @email=$imap->fetch($msg,'RFC822');                                                  # Fetch message.
                                open (SALEARN,"|/usr/bin/spamassassin -d | /usr/bin/sa-learn --spam") or print "$!\n";  # Feed to sa-learn.
                                print SALEARN "$email[1]";
                                close SALEARN;
                                open (REPORT,"|/usr/bin/spamassassin -d | /usr/bin/spamassassin -r") or print "$!\n";   # Report it. (SpamCop and Pyzor).
                                print REPORT "$email[1]";
                                close REPORT;
                                $imap->delete_message($msg) or next;                                                    # Delete it.
                        }
                        $imap->expunge($folder) or next;                                                                #Expunge folder.
                }
                elsif(lc($folder) eq "not spam")
                {
                       $imap->select($folder) or next;                                                                  # Select the folder.
                        print "Folder $folder selected.\n";
                        my @list=$imap->messages or next;                                                              # List all messages in folder.
                        print scalar(@list)." messages in folder.\n";
                        foreach my $msg (reverse(@list))                                                                # Loop over them all.
                        {
                                my @email=$imap->fetch($msg,'RFC822');                                                  # Fetch message.
                                open (SALEARN,"|/usr/bin/spamassassin -d | /usr/bin/sa-learn --forget") or print "$!\n";# Sa-learn forget this message if already seen.
                                print SALEARN "$email[1]";
                                close SALEARN or print "$!\n";
                                open (SALEARN,"|/usr/bin/spamassassin -d | /usr/bin/sa-learn --ham") or next;          # Feed to sa-learn as ham.
                                print SALEARN "$email[1]";
                                close SALEARN;
                                $imap->delete_message($msg) or next;
                        }
                        $imap->expunge($folder) or next;

                }
        }
        }
}
Verified Against: unknown Date Created: 3/5/2008
Article ID: http://wiki.zimbra.com/index.php?title=IMAP_and_Outlook_Spam_training Date Modified: 04/15/2010
Personal tools