Procmail Programming – January 07, 2010

I've written a couple procmail scripts over the years, and I thought I should publish them since it might help someone.

The following script goes in /etc/procmailrc which allows users to disable the spam check for whatever reason - perhaps they use a different program or otherwise don't need the spamassassin check. (see next script, where my virtual users use a different spamd, since the user doesn't actually exist, so I need to change the username in spamc, and call a different spamd which is listening on a different port than the default spamd)

:0fw
* ? /usr/bin/test ! -e $HOME/.no-spam-check
| /usr/bin/spamc --headers

I use this next script for my "virtual" users - I now do all email via virtual users (except for the folks who were around before this switch) as it makes e-mail management easier, via PostgreSQL scripts, and a custom PHP customer script that allows them to make their own modifications easily.

This script also supports the username+extension@domain syntax, which is handy for folks who don't have whole domain names to use for email/spam tracking.  My users have an infinite number of email addresses which can be "created" by simply telling someone that their email address is john.doe+spammer@mydomain.com. If the user then creates a folder named "spammer", the mail will be delivered into that folder, and if the folder isn't created, this script will put the mail in the main inbox, and the user could filter it on the client-side using the X-Original-To: header.

##############################
## rename arguments into nice names
## strip out slashes and periods in the extension to avoid
## mis-writing stuff to other directories (like ../../../.procmailrc)
domain=$1
username=$2
extension=$3
extension=`echo "$extension" | sed -e 's/[\/.]/_/g'`

# strange formats, probably errors in the postfix database
:0
*$ ${domain:+!}
! vmail+$domain+$username+$extension+nodomain@xxxx.com
:0
*$ ${username:+!}
! vmail+$domain+$username+$extension+nousername@xxxx.com
# we disabled default spam checking with .no-spam-check
# so now we run it using the virtual spamd
#
# 02/04/2008, added --timeout=100, we are getting some cases
# where the procmail script is taking 1001 seconds to run,
# and postfix bounces the message...
:0fw
* ? /usr/bin/test -d $MAILDIR/$domain/$username
| /usr/bin/spamc --timeout=100 --port=784 --headers --username=$username@$domain

# chuck definitely known spam
:0 h
* ^X-Spam-Level: \*\*\*\*\*\*\*\*\*
/dev/null
# deliver to /home/vmail/mail/{domain}/{user}/mail/{extension}
# if they previously created the folder
:0:
*$ ! ${extension:+!}
* ? /usr/bin/test -e $MAILDIR/$domain/$username/mail/$extension
$domain/$username/mail/$extension

# deliver to /home/vmail/mail/{domain}/{user}/mail/mail
# if the directory exists.  if it doesn't, it is an error
:0:
* ? /usr/bin/test -d $MAILDIR/$domain/$username/mail
$domain/$username/mail/mail

# shouldn't ever get here unless there is a missing directory
:0
! vmail+$domain+$username+$extension+missing-dir@xxxx.com

Some spam blocking scripts:

# spammers like forging mail from @daley.snurgle.org
# and I no longer send mail using this address, so anyone
# who says they received mail from this address got spammed,
# and has a poor ISP which bounced the spam to a person who didn't send it.
:0
* ^X-Original-To: [^@]+@daley.snurgle.org
* ^Subject: .*(deliver|failure|returned|autoreply|challenge|blocked)
/dev/null

An old address that Eversave sold to spammers

:0
* ^TO_eversave@jondaley.cjb.net
/dev/null