Stopping Spam with SpamAssassin

Stopping Spam with SpamAssassin

Despite the fact that it is rude and hateful, there are people who send out thousands of unsolicited e-mail messages (referred to as spam), hoping to get a few responses. Using a tool called SpamAssassin, you can configure your incoming mail service to tag messages it believes to be spam so you and your users can deal with those messages as you choose.

There are several methods that SpamAssassin uses to identify spam:

  • Checking mail headers — Examining the headers of your incoming mail to look for well-known tricks used to make the e-mail look valid.

  • Checking mail text — Looking for text style, content, and disclaimers in message bodies that are commonly used in spam.

  • Checking blacklists — Checking,, and other blacklists to find e-mail sent from sites known before to send spam.

  • Checking spam signatures — Comparing e-mail signatures. Since spam often consists of the exact same message sent thousands of times, taking signatures of spam messages lets SpamAssassin compare your message to a database of known spam messages. SpamAssassin uses Vipul's Razor (see

Although there are many different ways to deal with spam (or rather, e-mail that might be spam), most of the experts I have consulted like to configure SpamAssassin to simply tag incoming e-mail messages that appear to be spam. Then they encourage each user of the e-mail server to create his own rules for filtering the spam.

Setting up SpamAssassin on your mail server

Here's a quick procedure for enabling SpamAssassin and having your users choose what to do with spam messages that are encountered:

  1. Configure your mail transport agent (sendmail or postfix) to use the procmail command as its mailer. For sendmail, it is already configured as the default mailer, based on the following line in the /etc/mail/ file:

    FEATURE(local_procmail,`',`procmail -t -Y -a $h -d $u')dnl
  2. Make sure that the SpamAssassin spamd daemon is running (it should already be on for run levels 2-5), and if it isn't, start it by typing the following (as root user):

    # chkconfig --list spamassassin
    spamassassin   0:off   1: off   2:on   3:on   4:on   5:on   6:off
    # chkconfig spamassassin on
  3. Create an /etc/procmailrc file (using any text editor, as root user). This procmailrc file example pipes all mail messages received by procmail through spamc (which is the client side of the spamd daemon turned on in the previous step):

    | /usr/bin/spamc

    If you like, you can do a lot more in the procmailrc file to deal with spam on a system-wide basis. You could, for example, create procmail recipes that take reported spam e-mail messages and sorts them into a system-wide spam folder or deletes them completely. Likewise, each user can create an individual $HOME/.procmailrc file to create personal procmail recipes. (Type man procmailex for examples of rules in a procmailrc file.)

  4. Check the /etc/mail/spamassassin/ file. This file contains rules that are used system-wide by SpamAssassin, unless they are overridden by a user's individual $HOME/.spamassassin/user_prefs file. Here are the contents of the file:

    required_hits 5
    rewrite_subject 1
    subject_tag [SPAM]
    report_safe 0

    The required_hits line shows that a score of 5 is needed to flag the message as spam. You should set that higher for a public mail server (such as 8 or 10). Setting rewrite_subject to 1 allows SpamAssassin to change the Subject line of a message. The subject tag [SPAM] line has SpamAssassin add the text "[SPAM]" to the Subject line of spam it finds. The report_safe 0 line ensures that only the message header is changed, while the content is intact. (Type man Mail::SpamAssassin::Conf to see other settings you can use in the file.)

Because there will almost certainly be some false-positives, you risk preventing your users from seeing an e-mail they need if you do system-wide filtering. To avoid this problem, the approach shown here lets the user decide what to do with e-mail tagged as spam.

Next, you should have the users of that mail server set up their own user preferences in their home directories. The preferences set in each user's $HOME/.spamassassin/user_prefs file help tell SpamAssassin how to behave for that user's e-mail. Here are examples of lines a user might want to have in that file:

required_hits          3
blacklist_from        *

The required_hits line (which is on by default) sets the number of hits needed to consider the message to be spam. Hits are based on scores for matching or not matching criteria in the tests SpamAssassin performs. (See

The whitelist_from and blacklist_from lines let you set addresses for people, individual hosts, or entire domains that should not be considered as spam (whitelist_from) or should always be considered as spam (blacklist_from). For other ways to modify SpamAssassin behavior, type the following command:

man Mail::SpamAssassin::Conf.

At this point, SpamAssassin should be running and identifying spam based on input from you and the people using your e-mail server. Next, each user needs to decide what to do with the messages that are marked as spam, as described in the following section.


Techniques you can use along with SpamAssassin include services like RealTime Blackhole Lists (RBL) and SpamCop. With RBL (, you can block spam messages before they even reach your server. SpamCop ( provides a service that allows you to enter spam messages you receive into a database that helps others block the same spam messages.

Setting e-mail readers to filter spam

Each user can turn on filtering in his e-mail reader to decide what to do with each message tagged as spam from SpamAssassin. A common practice is to direct e-mail marked as spam to a separate folder. Because some real mail can occasionally be mistakenly marked as spam, you could check the spam folder every week or two, just to make sure you didn't miss anything.

Here's an example of how to add a filter rule from Evolution Email:

  1. Create a folder labeled SPAM under your incoming mailbox.

  2. Click Tools ? Filters.

  3. From the Filters window, click Add. An Add Rule window appears.

  4. Identify a rule name (such as Spam) that adds a criterion that looks for a specific header (X-Spam-Flag) containing specific text (YES). Then under Add Action, select an action (Move to Folder) and identify the folder to contain the spam messages (SPAM).

When you ask to receive mail from your mail server, all messages with the X-Spam-Flag set to yes will be sorted into your SPAM folder. As an alternative, you could check for the text [SPAM] to appear in the subject line as the criterion for sorting the spam messages.

Other mail readers (Mozilla mail, pine, Netscape mail, and others) also include features for filtering and sorting e-mail based on criteria you enter.

Part IV: Red Hat Linux Network and Server Setup