AntiSPAM with SpamAssassin : Move mails to Junk folder

We have made a way to flag messages giving the user the responsibility to remove the messages from his/her inbox. But some user don’t want to see them, don’t want to create filters on the mail client (or can’t do it), so, there is a way for them. We will create a Junk folder for this mail. If our user is friend@mylittlecloud.com we must (remember, our cyrus administration user was cloud):

$ cyradm -u cloud localhost
localhost> cm user/friend/Junk@mylittlecloud.com
localhost> mboxcfg user/friend/Junk@mylittlecloud.com expire 20
localhost> info user/friend/Junk@mylittlecloud.com
{user/friend/Junk@mylittlecloud.com}:
  duplicatedeliver: false
  expire: 30
  lastpop:  
  lastupdate: 23-Jun-2013 00:32:40 +0000
  partition: default
  pop3newuidl: true
  sharedseen: false
  size: 0

We are automatically expiring Junk mail messages when they are 20 days old, so the user can periodically see the spam messages and notice if there are any false positives.

We have to move all Spam to that folder, to do so, we will create a sieve script watching the subject of all messages and wherever it detects *****SPAM*****, it will move that message to the Junk folder.

First, create the script, it will be valid for all users with a Junk folder:

require ["fileinto", "reject"];

if header :matches "Subject" ["*****SPAM*****"] {
fileinto "INBOX/Junk";
}
else {
keep;
}

Now, we must log into sieveshell as the user we want to execute the scripts, but from our administration account (normally, we wouldn’t know users passwords), there, we install the script and activate it:

$ sieveshell --user=friend@mylittlecloud.com --realm cloud localhost
> put moveToJunk.sieve
> activate moveToJunk.sieve
> ls    
moveToJunk.sieve 
> quit

And we’re done. Now, let’s do it with all our users, it may help:

USERS=`cat /etc/postfix/vmailbox | cut -d' ' -f1`
for user in $USERS
do
     NAME=`echo $user | cut -d'@' -f1`
     DOMAIN=`echo $user | cut -d'@' -f2`
     echo "NAME: $NAME ; DOMAIN: $DOMAIN"
done

It gets a list of users from /etc/postfix/vmailbox file and extracts its name and domain, so the creation of the Junk mailbox would be:

user/$NAME/Junk@DOMAIN

And the installation of the sieve script will be the same for all users… so, let’s complete the script (it’s not optimized and maybe slow

CYRADM_PASSWORD="XXXXX"
USERS=`cat /etc/postfix/vmailbox | cut -d' ' -f1`
for user in $USERS
do
     NAME=`echo $user | cut -d'@' -f1`
     DOMAIN=`echo $user | cut -d'@' -f2`
     echo "cm user/$NAME/Junk@DOMAIN" | cyradm -u cloud -w $CYRADM_PASSWORD localhost
     echo -e "put moveToJunk.sieve\nactivate moveToJunk.sieve" | sieveshell --user=$user --realm cloud --password=$CYRADM_PASSWORD localhost
done

In a while, all our users will have a Junk folder and the Spam will be redirected.

Leave a Reply

Your email address will not be published. Required fields are marked *