Sort [SPAM] mail into a Spam folder (Postfix, Dovecot & procmail)

Quickly wanted to stop mail tagged by spamassasin as [SPAM] being delivered into my inbox. I did this using procmail and the following was configured on a Centos 6 server.

Install procmail

yum install procmail

Next create a procmailrc file in the users home dir along (this can be done globally by adding this to /etc/procmailrc rather than a user home). We also create a procmail directory to keep logs in and change the owner and permissions.

 mkdir /home/myuser/.procmail
 touch /home/myuser/.procmailrc
 chmod 600 /home/myuser/.procmailrc
 chown -R myuser:myuser /home/myuser/procmail*

Now add the following to /home/myuser/.procmailrc

# Some custom procmail stuff :D
# Change to on for logging
VERBOSE=off
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/procmail.log
# Location of things
SHELL="/bin/bash"
DELIVER="/usr/libexec/dovecot/deliver"
# Use the following if you get "destination user parameter (-d user) not given":
DROPPRIVS="YES"
# the directory that holds the mail!
DEFAULT="$HOME/mail/"
MAILDIR="$HOME/mail/"
:0 w
* ^X-Spam-Status: Yes
| $DELIVER -m Spam
:0 w
| $DELIVER

Next we change the mailbox_command in /etc/postfix/main.cf to the following

mailbox_command = /usr/bin/procmail -a $DOMAIN

Restart postfix

service postfix restart

Now we finally test by sending some spam using the gtube template to our mailbox.

Subject: Test spam mail (GTUBE)
Message-ID: <GTUBE1.1010101@example.net>
Date: Wed, 23 Jul 2003 23:30:00 +0200
From: Sender <sender@example.net>
To: Recipient <recipient@example.net>
Precedence: junk
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is the GTUBE, the
	Generic
	Test for
	Unsolicited
	Bulk
	Email

If your spam filter supports it, the GTUBE provides a test by which you
can verify that the filter is installed correctly and is detecting incoming
spam. You can send yourself a test mail containing the following string of
characters (in upper case and with no white spaces and line breaks):

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

You should send this test mail from an account outside of your network.

If you tail the maillog file you should see something similar to the following :)

Nov 30 06:54:16 m00nie.com dovecot: lda(m00nie): msgid=<blahblahblah@mail.gmail.com>: saved mail to Spam

m00nie :D