Sunday, December 13, 2009

Spam daily report on servers using sendmail

SkyHi @ Sunday, December 13, 2009
I don't know the exact solution to your problem, but this is pretty similar and should help you getting what you want. Else search on Google for "daily spam digest". This should give you some more useful results.

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 <tveldhui@cs.chalmers.se>
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!!!
</tveldhui@cs.chalmers.se>

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<br />folders INBOX.spam<br />keep<br />password "mypassword"<br />mda "( echo '#%F ' ; grep '^Subject:' | head -1 ) | spam.digest"<br />
    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' \<br />| tr -d '\n' \<br />| tr -d '\r' \<br />| tr -s '#' '\n' \<br />| awk -F% '{ printf "%-40s %s\n", $1, $2 }' \<br />| grep -v "^                  "<br />
    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<br />. ~/.bashrc<br />TMPFILE=/tmp/$USER-spam<br />RECIPIENT="myusername@cs.chalmers.se"<br />fetchmail -s --ssl >$TMPFILE<br /><br />NSPAM=`wc -l $TMPFILE | awk '{ print $1 }'`<br /><br />if test $NSPAM -eq 0; then<br />  exit 0<br />fi<br />echo "From: Cron Todd <tveldhui@cs.chalmers.se>" >$TMPFILE.mail<br />echo "Subject: Spam o' the day ($NSPAM)" >>$TMPFILE.mail<br />echo "" >>$TMPFILE.mail<br />cat $TMPFILE >>$TMPFILE.mail<br />mail $RECIPIENT <$TMPFILE.mail<br />rm -f $TMPFILE.mail $TMPFILE<br /></tveldhui@cs.chalmers.se>
    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<br />
    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.

Reference: http://serverfault.com/questions/45513/spam-daily-report-on-servers-using-sendmail
http://www.cs.chalmers.se/~tveldhui/tools/spam/