Filtering Spam with Spamassassin on Redhat 9

Keywords: Redhat Spamassassin Procmail Sendmail Fetchmail Spam Hotwayd


SPAM had finally gotten to the point in my life that I could stand it no more.

Before I was just a little annoyed and sometimes mildly amused by various offers for septic tanks and promises of an enlarged male organ. I have several email accounts, each of which receive a few unsolicited messages per day. I end up with about 100 spams a week and growing, so decided it was time to find a way to filter those bastards to /dev/null once and for all.

After a little bit of reading, I found out that Spamassassin is included with the RH9 distribution (if you're doing a fresh install, it's located in the mail server package section), so all I had to do was figure out how to use it. What I've ended up with grabs all of my email from various accounts and filters out 99% of my spams so far. It took me several hours of reading and testing to get this going, as I ran into several different articles that approached the problem differently. This article is targeted at a single home user that just wants to get rid of spam and doesn't want to be bothered with the details.  I assume you have root priveledge and can install and configure packages on your own.



Here's a brief rundown of what I'm doing followed by the configuration
.

1. Mail is grabbed from various accounts using Fetchmail. I also use Hotwayd to POP my hotmail as I phase out this godforsaken service.

2. Procmail forwards messages to Spamassasin for analysis.

3. The subject line gets labelled with [SPAM] if it is considered to be spam.

3. Procmail gets back the message and drops it where it's been told to be put (mine goes to /dev/null).

4. From there I can use the mail reader of my choice to read my virtually spam free messages.

                                                                                

Getting Spamassassin to work.


Sendmail, Procmail, Spamassassin, and Fetchmail are all included in the RH9 distribution, but you may need to install some of them manually. If you prefer Postfix over Sendmail, it's up to you. Make sure they are all installed before getting started. You should also make sure your sendmail is working by sending yourself a test message. From the command line you can type:


Verify your mail is working first.
mail -s TEST username (where username is the username you're logged in as)
This is a test
<ctrl + d>


If you then type "mail" and hit enter, you should see your message. If not, you've got to get your mail working which is beyond the scope of this article.


Procmail

By default, Sendmail should be configured to send all mail through Procmail, so there is no need to set up a ~/.forward file.  To verify, you can check your /etc/mail/sendmail.rf.  You should see an entry like: FEATURE(local_procmail,`',`procmail -t -Y -a $h -d $u')dnl.


Next, create /etc/procmailrc if it does not exist (it shouldn't if you are working with a new install of RH9).  Make sure you've got the following lines.

DROPPRIVS=yes
#Uncomment the following lines to allow for logging
#
#LOGFILE=/var/log/procmail
#VERBOSE=ON

:0fw
# The following line tells Procmail to send messages to Spamassassin only if they are less thatn 256000 bytes. Most spam falls well below this size and a larger size could seriously affect performance.)
* < 256000

| /usr/bin/spamassassin -P

:0e
{
EXITCODE=$?
}

That is all you need to do to get started.  All mail delivered to your local mailbox will have [spam] added to the subject.  You can use your favorite mail reader to filter as you wish, or if you want Procmail to move it somewhere, create or edit your ~/.procmailrc file and add the following lines.

:0:

* ^X-Spam-Status: Yes

/home/username/mail/spam

You can configure the last line to put marked messages wherever you want. If you change the line to /dev/null you will immediately get rid of spam. Make sure to test out Spamassassin for a while before deciding to do so, though.




Fetchmail


If you are like me, you have multiple email accounts and want to download them all into one mailbox when you're online.  I use fetchmail to get my messages and they all end up in my /var/spool/mail/username box (username being my login name).  Mail readers such as Ximian Evolution can be configured to pick up mail from the local spool and from there I'm all done.  Fetchmail is also installed by default on RH9, so all you need to do is create the ~/.fetchmailrc file.  Here's an example that polls  IMAP and POP3 accounts.

set daemon 600 (this tells Fetchmail to run as a daemon and check for mail every 600 seconds (i.e. 10 minutes)).

poll  imap.servername.com

protocol imap
username 'your namehere' (leave single quotes or take them out if there's no weird characters)
password 'yourpasswordhere' (leave this line out if you want to be prompted for a password each time)
fetchall
expunge 5
pass8bits
stripcr

poll 127.0.0
protocol pop3
username 'yourusername@hotmail.com'  (this account is set up for hotwayd)
password 'yourpasswordhere'
fetchall
expunge 5
pass8bits
stripcr


 I also added the command, "fetchmail", to my ~/.bash_profile so Fetchmail will start up when I log in.


Enjoy your virtually spam free mail.

-Ryan Turan

ryan at wanderlush dot com