How to receive a daily spam digest

Chalmers has a spam-filter you can enable; it filters your mail and puts anything it thinks is spam in a special mailbox. The claim is made that it is 99.9% correct at guessing spam; but in practice I routinely find important mail in my spam box. Perhaps it is the same for you.

I set up a simple script to email me, once a day, the from and subject lines of everything that has been placed in my spam box. This is what it looks like:

From: Cron Todd 
Subject: Spam o' the day (4)
Date: Tue, 10 Aug 2004 07:30:20 +0200 (MEST)

chr@active.ch                             Re: Returned mail: Data format error
inrwjry@yahoo.com                         Fw: hadamard
info@capitalraisingstrategies.com         Go Public: how any company can go public including yours: receive 2 reports
swisslotto12@swissmail.net                CONGRATULATIONS! YOU WON!!!

How to set it up

I did this under SunOS, but it should work under any reasonable unix. You'll need fetchmail (a standard package; try `which fetchmail`).

  1. Put this in your ~/.fetchmailrc:
    poll mail.medic.chalmers.se proto IMAP
    folders INBOX.spam
    keep
    password "mypassword"
    mda "( echo '#%F ' ; grep '^Subject:' | head -1 ) | spam.digest"
    
    Replace "mail.medic.chalmers.se" with your mail server, change protocol from IMAP if necessary, and replace "mypassword" with your mail password. Obviously chmod go-rwx this sucker. On our mail server spam is put in a mailbox called "INBOX.spam"; you may need to adjust this for your situation.
  2. Put the following script in your path, and call it "spam.digest":
    sed 's/Subject:/%/g' \
    | tr -d '\n' \
    | tr -d '\r' \
    | tr -s '#' '\n' \
    | awk -F% '{ printf "%-40s %s\n", $1, $2 }' \
    | grep -v "^                  "
    
    Ugly, yes, thank you.
  3. Test with "fetchmail --verbose"; if all goes well you should see a list of new mail messages in your spambox. You won't see anything if there are no unread messages. Try sending yourself some spam first; a subject line like "nigeria viagra cheap!" should do the trick...
  4. Assuming that worked, put the following script in your path and call it "cron.spam":
    #!/usr/ed-pkg/sup.phc/b/binh/bash
    . ~/.bashrc
    TMPFILE=/tmp/$USER-spam
    RECIPIENT="myusername@cs.chalmers.se"
    fetchmail -s --ssl >$TMPFILE
    
    NSPAM=`wc -l $TMPFILE | awk '{ print $1 }'`
    
    if test $NSPAM -eq 0; then
      exit 0
    fi
    echo "From: Cron Todd " >$TMPFILE.mail
    echo "Subject: Spam o' the day ($NSPAM)" >>$TMPFILE.mail
    echo "" >>$TMPFILE.mail
    cat $TMPFILE >>$TMPFILE.mail
    mail $RECIPIENT <$TMPFILE.mail
    rm -f $TMPFILE.mail $TMPFILE
    
    This is a bash script; you'll want to replace the path to bash on the first line with `which bash`; change "myusername@cs.chalmers.se" to your email address; change the From and Subject line to taste. Note the --ssl argument to fetchmail, if your mailserver is not running over SSL then omit this. You may need to supply a full path for the fetchmail command. Try running the script; you should get an email.
  5. Now it's just a matter of setting up a cron job. Do "crontab -e" and add the following line:
    30 7 * * * /users/cs/tveldhui/bin/cron.spam
    
    This will send you an email every morning at 7:30 am. If you want twice daily you can do (for example) "30 7,14 * * * ..." which will also send you a note at 14:30pm. Edit the path /users/cs/tveldhui/bin/cron.spam to point to wherever you've put the "cron.spam" script. Note cron is finicky about whitespace and will send you a prissy email if there is a trailing blank line.
Good luck with all that.