Saturday, January 14, 2012

Server Side Spam Filtering with Postfix Spamassasin and Maildrop


Server Side Filtering spam in Postfix to move all spam tagged mails to a user spam / junk folder is pretty simple.
This solution helps in moving all ***** SPAM ***** subject tagged by spamassasin to be moved to junk folder there by the spam tagged mails doesnt appear in INBOX.
Install Maildrop and configure as given below to automatically move those files to a Junk folder.
Steps to set this with Postfix and Spamassassin :-
First, setup your /etc/maildroprc file:
# commands and variables for making the mail directories
maildirmake=/usr/bin/maildirmake
mkdir=/bin/mkdir
rmdir=/bin/rmdir
MAILDIR=$DEFAULT
# make the user's mail directory if it doesn't exist
`test -e $MAILDIR`
if ($RETURNCODE != 0)
{
`$mkdir -p $MAILDIR`
`$rmdir $MAILDIR`
`$maildirmake $MAILDIR`
}
# make the .Junk folder if it doesn't exist
JUNK_FOLDER=.Junk
_JUNK_DEST=$MAILDIR/$JUNK_FOLDER/
`test -d $_JUNK_DEST`
if ($RETURNCODE != 0 )
{
`$maildirmake $_JUNK_DEST`
#auto subscribe. the following works for courier-imap
`echo INBOX.Junk >> $MAILDIR/courierimapsubscribed`
}
# If the Spam-Flag is set, move the mail to the Junk folder
if (/^X-Spam-Flag:.*YES/)
{
exception {
to $DEFAULT/.Junk/
}
}
The comments clearly state what’s going on there.
Once that’s setup, check /etc/postfix/master.cf and make sure the
maildrop unix - n n - - pipe
flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
is not commented out.
Next set the /usr/bin/maildrop file setuid root. This is so maildrop can interact with authdaemon and the mail folders.
#chmod +s /usr/bin/maildrop
Then add this to /etc/postfix/main.cf file:
virtual_transport = maildrop
maildrop_destination_recipient_limit = 1
If there is another virtual_transport line, be sure to comment that out first.
Last, set the permissions on the authdaemon so that maildrop can access it.
chown vmail /var/run/courier/authdaemon


2 comments:

  1. Thanks for the very useful post. I was able to get Maildrop working on my virtual domain based EHCP server.
    I modified master.cp using this sites recommendation for user + extension@domain.com support - http://www.postfix.org/MAILDROP_README.html

    Here is the version that I am using on my server:

    # Global maildrop filter file

    # Uncomment this line to make maildrop default to ~/Maildir for
    # delivery- this is where courier-imap (amongst others) will look. default "HOME/maildir"
    DEFAULT="/home/vmail/$4/$3"

    # Commands and variables for making the mail directories
    maildirmake="/usr/bin/maildirmake"
    mkdir="/bin/mkdir"
    rmdir="/bin/rmdir"
    MAILDIR="$DEFAULT"
    #make the user's mail directory it it doesn't exist
    `test -e $MAILDIR`
    if ( $RETURNCODE !=0 )
    {
    `$mkdir -p $MAILDIR`
    `$rmdir $MAILDIR`
    `$maildirmake $MAILDIR`
    }
    # make the .Junk folder if it doesn't exits
    JUNK_FOLDER=".Trash"
    _JUNK_DEST="$MAILDIR/$JUNK_FOLDER/"
    `test -d $_JUNK_DEST`
    if ( $RETURNCODE !=0 )
    {
    `$maildirmake $_JUNK_DEST`
    }
    #auto-subscribe. The following works for courier-imap
    `echo INBOX.SPAM >> $MAILDIR/courierimapsubscribed`
    # If the Spam-Flag is set, move the mail to the Junk folder
    if (/^X-Spam-Flag: YES/)
    {
    to "$_JUNK_DEST/"
    }
    else
    {
    to "$MAILDIR/."
    }


    ReplyDelete
  2. Hi, thanks for the guide. It almost seems to work for me. For whatever reason all mails regardless of domain get thrown into one file named /home/vmail/Maildir where Maildir is that file where I find all emails.

    Do you have an idea why that happens and how to solve it?

    Thank you!

    ReplyDelete