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: tom@my-server.com
Delivered-To: tom@my-server.com
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: tom@my-server.com
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.

  • Looks like it might be necessary to use something like maildrop. I am still looking for a way to do this with Postfix's default MDA so I can keep aliases and other postfix features with minimal trouble. I will post my answer when I come up with it. Mar 15, 2013 at 6:02

3 Answers 3


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/):

* ^X-Spam-Flag: YES

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.

  • 1
    Please note that in some cases procmail lives at /usr/bin/procmail, to figure out where it is on your system run which procmail
    – Malfist
    Sep 15, 2014 at 13:27

What is your mail delivery agent (see main.cf mailbox_command)? If the MDA is dovecot, it supports the Sieve IETF standard (rfc5228), which can do a lot more than procmail without exposing possible security holes. DON'T bolt on procmail to postfix/dovecot, sieve is fully integrated. If you're using Courier, then procmail is an okay solution. However, you will find the sieve/sievec programs from Pigeonhole to be faster and cleaner.

The following is a simple sieve script to recognize your spam header and put the message in the Trash file.

require "fileinto";

if header :comparator "i;ascii-casemap" :contains "Subject" "**SPAM**"  
  fileinto "Trash";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.