16.10. Filters

Filters enable automatic processing of incoming messages and manual processing of selected messages in a folder. To filter selected messages to a specific folder, highlight the messages and enter Ctrl-J to activate existing filter rules or select Message+Create Filter to create a new one. In doing so, all existing filter rules are applied to this message. There is no way to use only certain filters.

16.10.1. Filter Rules

A filter consists of one or several filter rules and filter actions. A filter rule can select messages according to criteria, such as the sender, content, or recipient. The respective filter action can perform tasks like moving, deleting, marking, or forwarding these messages.

16.10.2. Examples of Filters

Assume, for instance, to be subscribed to the KDE user list (kde-user@kde.org). Whenever a message from the mailing list arrives, the message is moved into another folder. The following steps are necessary for creating this filter.

  1. Add a new folder as described in Section 16.7. “Message Folders”. A name like kde-user can be assigned.

  2. First, find a way of identifying messages to filter. The messages of the KDE user list described in our example can be identified by the kde-user@kde.org address, found in the To: or Cc: field.

  3. Click Settings+Configure Filters. The left side of the window that opens displays the existing filters. The right side is composed of two panels: filter rules and filter actions.

  4. Click the New icon to create an empty filter. It appears as unknown.

  5. Select To: or Cc: from the first drop-down menu and contains from the second drop-down menu. Enter kde-user@kde.org in the text field.

  6. In Filter actions, select Move to Folder from the first drop-down menu. A second drop-down menu with the list of folders appears. Choose the folder to which the filtered messages should be moved if they meet the desired criteria. In this case, choose kde-user from the drop-down menu.

  7. Confirm the changes with OK.

More complex filters are also possible. For example, you may want to save only the messages written by Fred Johnson to the KDE user list (fj@anywhere.com). Here, the remaining filter criteria come into play:

  1. Click Settings+Configure Filters and mark the newly created filter.

  2. To filter all messages containing kde-user@kde.org in the To: or Cc: fields and originating from Fred, define a second filter under Filter Criteria using the second row of drop-down menus. Select From in the first menu and contains in the second. Then enter the e-mail address to filter. Activate Applies to all.

  3. Select the action to execute (move, delete, etc.).

You have now created a filter that transfers all mails from Fred Johnson on the KDE user list as specified.

16.10.3. Spam Filters

While electronic mail has many advantages, it has a significant disadvantage: spam. Unsolicited mass mailings, also known as spam, sent by the advertisement industry, clog your electronic inbox and cause long transfer times. Filtering tools can separate the annoying ballast from important messages. SpamAssassin is such a tool that can be embedded into KMail. It relies on various built-in rules and has the ability to learn from its experience with spam. The configuration of this useful application for KMail is described here.

16.10.3.1. Preparations

The actual software needs to be present before spam messages can be filtered. Run YaST as user root and install the package spamassassin with the package manager (Software->Software).

Then configure the system to activate the SpamAssassin service on system start-up by opening the RunLevel editor in YaST (located under System). Search the list of services for spamd and select activate. Confirm the choice with Finish. This completes the SpamAssassin installation process.

Create an additional folder in KMail (naming it spam, for instance) in which to store the messages classified as spam. If you already have collected some spam messages, move as many of them as possible into this folder. This folder can be later used as a training stock and allows the tool to decide which of your incoming messages should be classified as spam.

16.10.3.2. Spam Filtering

KMail requires two filters for spam processing:

  1. The first filter examines the size of a message. Select Settings->Configur filters…. In the dialog that appears, create a new filter entry and name it SpamAssassin (modify the name, if desired). Select <Size in bytes> as the filtering property and is less than as the qualifier. Set the threshold to 300000.

    Select pipe through application for filtering actions and enter spamc in the field next to it. The option abort processing here upon matching filter condition must be deactivated. Confirm the first filter with Apply.

    This filtering rule specifies that KMail passes only messages to spamc whose size is less than 300 KB. It is possible to adjust other threshold values. The processing time for messages rises, however, with an increasing size threshold. SpamAssassin then tests all messages for characteristics of spam and alters the message header by adding the line X-Spam-Status: YES. The second filter relies on this.

  2. Add a second filter to the list and name it SPAMFilter. This filter moves the messages marked by SpamAssassin to the spam folder.

    Select <header field> as the property and contains as the qualifier. Enter X-Spam-Status: YES in the corresponding text entry field. Set filtering action to file in folder and select spam. Confirm with Apply.

These two filters should be placed in the described order at the top of the list. Any other filters can be placed below them.

16.10.3.3. Distinguishing Wanted and Unwanted Messages

SpamAssassin ships with predefined rules that control the recognition of spam messages. It is possible for the tool to miss a message occasionally. This is where the rate of false negatives can be lowered by training SpamAssassin.

The utility sa-learn is included for this purpose. If your messages are stored in the MailDir format (refer to Section 16.2. “Mail Formats”) run the following command:

sa-learn --spam --dir ~/Mail/spam/cur/

If your messages are stored in the mbox format, run the following command:

sa-learn --mbox --spam ~/Mail/.spam.directory/*

SpamAssassin returns the following message upon a successful spam recognition event:

Learned from 15 message(s)

Visit the site http://www.spamassassin.org or read the corresponding man page to discover more about this tool.