Ledscripts.com
Main Menu
» Main Page
» Priority Support
» Mailing List
» Contract Work
» Contact Info
» Support Forums

Free PHP Scripts
» phpLedMailer
» phpLedAds
» LedFormMail

Free Perl Scripts
» LedNews
» LedAds 2.0
» FullImage

Resources
» Ledscripts.com Blog
» Technical Articles

Other Items
» Online Users (10)

 Spam Filter with SpamAssassin
Back to [ Technical Articles » Perl ]
Published Sep 08, 2005 @ 9:21:31

Script Installation/Usage

First, make sure SpamAssassin is installed. This is required so that we can load 'Mail::SpamAssassin' for spam checks.

Download and copy spam-filter.pl to your ~vpopmail/bin/ directory. (See attached files). Make sure this script can be executed by the vpopmail user (chown vpopmail spam-filter.pl && chmod +x spam-filter.pl)

Setup 2 vpopmail accounts, one for spam mail and one for real mail.
Note: You can just ignore spam mail if you want, but I don't recommend it,
as there may be false-positives. For this example, I'll use 'userclean@domain.ext' and 'userspam@domain.ext'

Now create a .qmail- (.qmail-username in this case) file for the user that the mail actually goes to
(cannot be either of the above users) in the ~vpopmail/domains/domain.ext/ directory.

In the file, put something to the effect of:
| /usr/local/vpopmail/bin/spam-filter.pl -C "/usr/local/vpopmail/bin/vdelivermail 1 user@domain.ext" -X "/usr/local/vpopmail/bin/vdelivermail 1 userspam@domain.ext" -x 100 -c 100 -S

(yes, all one line, and including the '|')

Here is a full list of the command line options (copied and pasted from the source of spam-filter.pl):
# opts (all optional):
# -d -- just delete anything marked spam (default)
# -S -- quietly discard message (no bounce) if spam
#	note: this still executes -X command, if specified
#		turn this off by using -K:
# -K -- if -S is specified, then also 'kill' the message all together (avoid exec of -X)
# -f /some/file.cf -- the spamassassin config file
# -r -- report the spam to any report databases
# 	(not yet implemented)
# -M 'eat me' -- the spam bounce message (Defaults to 'DIE SPAM')
# -p /path/to/file -- prefs file
# -C "/path/to/program arg1 arg2' -- Pipe to given program
#	Note: this happens on success (Still exists 0)
#	If exit from this program is not '0', then this will exit 111 (tmp fail)
# -c 111 -- exit with give code if -C program fails (defaults to 111)
# -X "/path/to/program arg1 arg2..." -- pipe to this program on spam
#	Note: give a value of 'C-' to use -C's value
# -x same as -c, only used with -X
#
# Added 8/26/2002 -- use spamc/spamd to check spam
# -s -- use spamc/spamd instead of loading the stuff yourselves
#	note: spamc doesn't allow reading of SA prefs file, this is a spamd option
#		(See spamd/spamc docs for details)
#	note: the prefs file things that check headers (like subject scans)
#		will be ignored, since we're not parsing this in-process
#
# Added 9/3/2002 -- allow rewrite of emails that are going to be sent to
#	a program via -X or -C
# -w -- reWrite email message to reflect that its spam
#
# Added 9/9/2002 -- use Mail::SpamAssassin if 
#	spamd appears to be dead (not running)
# -F -- fallback to Mail::SpamAssassin

See the source code of spam-filter.pl to see what the command line arguments are.

Another optional usage would be to use it as a 'stop processing .qmail file if spam' type of program. This was its original usage (and, of course, will still work as such). For this, you still have 2 vpopmail accounts (or however many you want), and set it up as so (this goes in the .qmail-realuser file):
# all mail will go to this account
| /usr/local/vpopmail/bin/vdelivermail 1 userspam@domain.ext

# this will 'stop' processing if its a spam message
| /usr/local/vpopmail/bin/spam-filter.pl -M 'die spam, die!'

# only 'clean' mail will be delivered this far
| /usr/local/vpopmail/bin/vdelivermail 1 userclean@domain.ext

This will allow *all* mail to be delivered to userspam@domain.ext (even non-spam e-mail), and only 'clean' e-mail will go to userclean@domain.ext. This will also bounce with the error of 'die spam, die!' to the mail server it came from. You can have it quietly ignore spam message (i.e., don't bounce) by using the -S flag.

Or... you can use as many combinations of -C and -X programs as your imagination can throw at you. I've tried to make this as flexible as possible.

Continued on the next page...
Page: 1 2 3
Attachments:
» spam-filter.pl [ 10,719 bytes ] [ 2154 download(s) ]

Download this article as a PDF document.
Right Click and Choose "Save As..."

Post comments on this article in the Forums.

Copyright Jon Coulter - Ledscripts.com 2000 - 2005