Send spam mail to a special folder using postfix

I have a postfix server running amavis and SpamAssassin to help filter spam. Messages that are detected as spam look like this when delivered into the user's Maildir:

From tom@tom-mint Fri Mar 15 01:46:20 2013
Return-Path: <tom@tom-mint>
X-Original-To: [email protected]
Delivered-To: [email protected]
X-Virus-Scanned: Debian amavisd-new at my-server.com
X-Spam-Flag: YES
X-Spam-Score: 6.463
X-Spam-Level: ******
X-Spam-Status: Yes, score=6.463 tagged_above=2 required=6.31
    tests=[DRUGS_ERECTILE=2.221, FH_FROMEML_NOTLD=0.18,
    FSL_HELO_NON_FQDN_1=0.001, HELO_NO_DOMAIN=0.001, RCVD_IN_PBL=3.558,
    RDNS_DYNAMIC=0.363, TO_NO_BRKTS_DYNIP=0.139] autolearn=no
Date: Fri, 15 Mar 2013 01:46:19 -0400
To: [email protected]
Subject: ***SPAM*** hello
User-Agent: Heirloom mailx 12.5 6/20/10
Content-Type: text/plain; charset=us-ascii
From: tom@tom-mint (Tom)
Status: RO

buy some viagra!

Note specifically the subject, which is prepended with ***SPAM***, and the X-Spam-Flag: YES header.

What's the best way to configure postfix to store these messages in a special junk folder?

From my research, it seems like I will need to use a different mail delivery tool than postfix to accomplish this, but I am not sure what approach is the best. There seems to be a lot of dated information, and I am wondering whether postfix is capable of placing messages into a folder or not.

The mail directories are all Maildir.


Cursory perusal of postfix's local(8) local delivery agent man page shows no hint of this type of capability - as expected. This kind of tasks is usually offloaded to procmail (probably through the mailbox_command directive) which can handle the task you describe while managing your kitchen sink on the side. The downside of procmail is the config file format, the upside the flexibility and the tons of examples that are easily found. If anything better than procmail has recently emerged, I know not.

For my money, though, even if I found the way to shoehorn the MTA in doing what you want, I would not follow that road, because this kind of mail sorting things have a way of balooning and no MTA (that I know of: perhaps exchange does,who knows) can do a good job in also being a flexible message handler/delivery agent.

This is a procmail recipe that woud do what you want based on the sole header content (where DEFAULT is the delivery directory, often something like $HOME/Mail/):

:0
* ^X-Spam-Flag: YES
$DEFAULT/.Spam/

Edit: (This assumes maildir mailbox format, as noted in a comment below. Omit final slash if using mbox)


As Alien Life Form said, this is a job for procmail. That's not a replacement for the mail server's local delivery agent; it's a separate process which will be called after the MDA has done it's work.

Each user that wants to have procmail sort their mail will need to create a file called .forward in their home directory. That file should contain the following:

"|exec /usr/local/bin/procmail || exit 75"

complete with the " sign and all.

ALF has already posted a basic .procmail file that will sort the mail tagged with spam into a separate folder.