12.10. Filters

Filters enable the automatic processing of incoming messages. However, filters can also be applied to messages manually. To do so, highlight the messages then press Ctrl-J or select Message->Apply filters. The filters are applied to all selected messages by matching rules. All existing filter rules are applied. There is no way to exclude certain filters from the action.

12.10.1. Filter Rules

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

12.10.2. Examples of Filters

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

  1. Add a new folder as described in 12.7. “Message Folders”. Provide a suitable name for it, such as kde-user.

  2. First, think about the best way to clearly identify the messages to filter. The messages from 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 (bottom left) to create an empty filter. It appears as <unknown>.

  5. In the Filter Criteria panel, 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 the Filter actions panel, 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. Here, the remaining filter criteria come into play:

  1. Click Settings+Configure Filters and select 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 Fred's e-mail address () in the input field. Make sure to enable 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.

12.10.3. Spam Filters

While electronic mail has many advantages, it also 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.

12.10.3.1. Preparations

The actual software must be present before spam messages can be filtered. Run YaST as user root and install the package spamassassin with the package manager. 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 used later as a training stock and allows the tool to decide which of your incoming messages should be classified as spam.

12.10.3.2. Spam Filtering

KMail requires two filters for spam processing:

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

Under Filter actions, select pipe through application and enter spamc in the field next to it. Note that the option abort processing here upon matching filter condition must be deactivated. Confirm the first filter with Apply.

The above filtering rule specifies that KMail passes only those messages to spamc that have a size less than 300 KB. You may try other threshold values, but keep in mind that, with a higher threshold, the processing time increases. 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.

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.

Under Filter actions, select file in folder with spam as the destination folder. Confirm with Apply.

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

12.10.3.3. Distinguishing Wanted and Unwanted Messages

SpamAssassin ships with predefined rules that control the recognition of spam messages. It is possible for SpamAssassin to miss a message occasionally, but the program can be trained to lower the number of false negatives. The utility sa-learn is included for this purpose. If your messages are stored in the MailDir format (refer to 12.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)

To learn more about this tool, visit the site http://www.spamassassin.org or read the corresponding man page.