Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create an online spam trap counter Internet
For those obsessed with trapping spam, or just looking for another shiny-object to include on your blog/homepage, it's fairly simple to use many of the free web-counter services to count your spam messages instead using either procmail on unix, or the OS X Mail client and a short chunk of AppleScript.

I've written up the whole thing in an entry on my weblog.

[robg adds: With the author's permission, I've recreated the content of the weblog here in the database -- read the rest of the hint to read it here, or use the link above to view the original.]

I just whipped this up, and didn't know who to tell about it, so I figured I'd blog it, and then maybe when it comes up in conversation, I can just say "look on my blog for that."

For quite awhile now, I've had a couple of neat "personal odometers" over on the righthand side of my blog, including my friendster counter, and the number of spam messages I've blocked since May, 2003. That's all done server-side on my systems, generating the number as a little image I then include in my blog template. Cute, and a little elaborate.

It occurred to me that anyone out there with a templatable blog, regardless of how simple, could do the same thing with any of the free web-counters that are available out there, if they're using procmail or mac-mail to filter their mail:
  1. sign up for a free counter at a web-counter service. For my example, I used, whom I found using Google. They're pretty easy to set up. Once you've signed up with them, you'll get a chunk of html code that you'd normally put on your web page... don't do that just yet.

  2. the chunk of html you're given by xcounters (or any of the other free web-counter services that work in the same way) give you a <img src=""> tag (where "userid" is your accountname) that you're supposed to include on your homepage/blog/whateverpage. When you fetch that image, it increments a counter in a database someplace, and then spits back to the requestor a GIF image of that number, which is displayed on your page.

    What we want to do is fetch that image, and thereby increment our counter, every time a spam message is caught.

    Note that most web counters have a "don't increment" image you can include for "admin" pages or whatever... with, that url is constructed by putting in a "new/" before the question mark, like "" (notice that's pulled from a different servername, too, just "", not "") ... THIS is the image we want to include on our page. It's also nice to not be a total sponge, and include the links back to xcounters they ask us for, to support their website.

    This is what the little number towards the bottom of the page is.

  3. Now that you have a non-incrementing counter on your web page, we want to increment it by requesting that first url, the one that looks like "" , every time we receive some spam.

    if you're on unix, using procmail, you're golden... in your .procmailrc file, where you'd normally have a rule like:
         * ^X-Spam-Status: Yes
    ...change that to:
         * ^X-Spam-Status: Yes
         * ^X-Spam-Status: Yes
         | /usr/bin/curl "" 2>&1 > /dev/null
    ....if you can't find "curl" installed on your unix system, try replacing that with "wget -q -O - "

    ...this should result in a copy of each spam message (that's flagged by spamassassin with the "X-Spam-Status: Yes" header) getting piped into the command-line URL-getter (either curl or wget)... they ignore the input of your message, but they hit that URL, which increments the counter image, and bingo, your spam count goes up by one.

    if you're not on unix and procmail, but you use something like Mac Mail, you're still in luck. If (on your mac, running osX) you fire up "Script Editor", and enter:
    do shell script "curl -si \"\" 2>&1 > /dev/null
    ...into the window, and click "run", you should notice your counter increment by one in your browser (upon reloading the page). Save this AppleScript as a script, and name it something like "spam-increment.scpt", in your homedir.

    In Mac Mail, under "Preferences...", under the "Rules" tab, add a rule. Name the rule "spam-counter increment." Under the conditions, you can select "Message is Junk Mail." Under "Perform the following actions:", select "Run Applescript", and then use the "choose..." button to select the small applescript you just saved.
That's it... every time that script is called now, your mac (or procmail system) should go out and hit that url, and increment your spam counter, greatly improving your blog's blinkenlights quotient.
  • Currently 3.25 / 5
  You rated: 3 / 5 (4 votes cast)

Create an online spam trap counter | 9 comments | Create New Account
Click here to return to the 'Create an online spam trap counter' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create an online spam trap counter
Authored by: ryanmathus on Jan 12, '04 12:55:00PM
For the Mac OS X Mail, I had to put a " at the end of the line in script editor. It gave me a syntax error -- I'm sure it was just an oversight. The line looked like this for it to work properly:
do shell script "curl -si \"\" 2>&1 > /dev/null" 
with the trailing quote.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: soulrider2k on Jan 12, '04 04:18:50PM

What about an option to change your ichat status to mention how many spams you have recieved?


[ Reply to This | # ]
Create an online spam trap counter
Authored by: restiffbard on Jan 12, '04 08:15:58PM

Regarding using There's no need to make a new rule for Junk. You can just add to the existing rule. If you click Junk in the preferences window then click advanced you can add the applescript function there.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: kirkmc on Jan 13, '04 04:23:12AM

I don't see the point in using bandwidth for something like this. Just create a file, say, spamcount.txt and put it in your Documents folder. Then create an AppleScript as follows:

do shell script "echo >> 1 ~/Documents/spamcount.txt"

This adds a 1 on a new line.

Add this script to a junk mail rule in Mail or Entourage, then, to find the total number of spams, just run this command:

wc -l ~/Documents/spamcount.txt

in the Terminal.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: iordonez on Jan 13, '04 03:48:39PM

Now the great question would be... How can this be done using spamassassin, amavis and Postfix on a server? I'd love to be able to add a counter on my webmail page that shows our users just how many messages we have trapped that are spam. I can't find anywhere for either to run a script when mail is received and considered spam. Anyone got any great ideas out there?

[ Reply to This | # ]
Create an online spam trap counter
Authored by: wsdr on Jan 14, '04 10:06:29AM
iordonez, I use the Exim mail server instead of Postfix, so I don't know if this directly relates. But Exim has a conf file that you have to edit to have Exim call Spamassassin. In my instance we use a MySQL database to store email accout info, and so Exim is also making calls to that. In the rule that Exim uses to decide if an email is spam, I added another call to MySQL that increments a column by one (if it's spam). It looks like this:
deny message = Spam score too high ($spam_score)
     condition = ${if >{$acl_m1}{0}{1}{0}}
     condition = ${if {$spam_score_int}{$acl_m1}{1}{0}}
     set acl_m3 = ${lookup mysql{UPDATE spam set spamcount=spamcount+1}}
The last line is the relevant one; this statement appears in the acl_check_content section of the config file. If you're using Exim, don't copy my code verbatim because much of it relates to our customizations of Exim and Spamassassin-- you're on your own there. If you want to set up Exim & Spamassassin, visit for articles on this (not affiliated, just found their articles very helpful).

To do this I had to add a new table to the MySQL email user database, but it's a table with only one column. I guess I could write to text file, but this was easier to do.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: waked1 on Jan 14, '04 03:00:55PM

Can anyone recommend an alternative to xcounters. I can access their server at work but not from home. (I'm investigating with my ISP but I don't hold much hope!)
I can find lots of counter providers but can't see any who do the admin access one for actual display.
Alternatively, can I get the scripts and I'll do it on my own server.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: ryanmathus on Mar 03, '04 05:40:52PM

Yeah, I'd like to know an alternative too. This hint no longer works with xcounters because the site was changed to an only hit counter.

The no longer works because the directory /new/ now just forwards you to the homepage.

[ Reply to This | # ]
Create an online spam trap counter
Authored by: mikemacfr on Aug 24, '06 08:01:10AM


I was looking for something else when I saw this thread.

What about amavis-stats? It works real neat but its for the whole server not
individule users...

[ Reply to This | # ]