Filters

Filters

After using KMail for a while, you may find that you have trouble sorting out the new messages in your inbox when they arrive. Filters allow you to automatically perform certain actions on incoming messages and to manually perform actions on selected messages in a folder.

Filters consist of a search pattern, whose rules are used as criteria to determine whether this filter should be applied to a given message, and a list of filter actions, which describe what is being done with or to the message if the search pattern matches. Read more about search patterns and filter actions in the following subsections.

Caution

Filters are considered one after the other, starting with the first filter in the list. The first one whose pattern matches the given message gets executed. You can request that the remaining filters also be applied, but the default is to stop processing at the first matching filter.

Usually, filters are used on incoming messages, but they can also be applied to a message or a group of messages. To selectively filter messages, select the messages you want to filter and type Ctrl+J or select Message->Apply Filters. This will apply all your filters to those messages; there is no way to apply only certain filters to messages, except for incoming and outgoing messages.

Fast Filter Creation

There are two methods for creating a filter: The quick method is to use Message->Create Filter. This will call the filter dialog and present you with a new filter which has the first rule of the search pattern and the first action (as ‘move to folder’) preset. In most cases, all you have to do is select the folder where the message should be moved to. But you can, of course, edit the filter as you like.

The interesting thing about this method is that creating a filter on mailing lists will try really hard to find a header that uniquely identifies mails from that list. If it succeeds, the guessed name of the list is presented in the Message->Create Filter->Filter on Mailing-List... menu entry.

Tip

In most cases this works very well, yet sometimes you should delete parts of the header contents that are likely to change again. Usually, just keeping the list address works best.

The second method is to manually construct a filter from scratch by calling the filter dialog through Settings->Configure Filters.... The filter dialog is described in detail in the following subsection.

The Filter Dialog

This dialog allows you to manage and edit your list of filters.

You reach it either via Message->Create Filter or Settings->Filter Rules....

To exit this dialog without applying changes, click on Cancel or hit Esc.

The dialog is divided into four main sections:

Available Filters

This group contains the list of filters and action buttons to modify the filters, namely to create new filters, to move them up or down the list, or to delete and rename them. If you select a filter from the list, its properties are shown in the right-hand half of the dialog.

Filter Criteria

In this group you can edit the pattern that messages must match for the filter to be applied to them. You can select here whether all of the defined rules must match or whether it suffices that any one of them matches. See Search Patterns below for a detailed description of each search rule type.

You can click on More to get an additional (initially empty) rule if you want to define more complex patterns and on Fewer to remove the last rule. Clear clears the pattern, i.e. it removes all but two rules from screen and resets those two.

Invalid or empty rules are not evaluated. In fact, they are removed internally. Whether a given rule is considered invalid depends on the type of rule, but as a rule of thumb you can assume that rules are considered invalid if the domain (the first combobox in each row) or the search string (the line edit at the end of each row) is empty. Thus, to effectively delete a particular rule, you only need to empty one of those two edit fields.

Filter Actions

In this group you can edit the list of actions that is applied to all messages that match the defined search pattern. See Filter Actions below for a detailed description of each action type.

You can click on More to get a new, empty action if you want to define more than one action and on Fewer to remove the last action. Clear clears the list, i.e. it removes all but one action and resets that one.

Invalid or empty actions are not executed. In fact, they are removed internally. Whether a given action is considered invalid depends on the type of action, but as a rule of thumb you can assume that actions are considered empty if no type is selected or - in case of actions that need a textual parameter (i.e. one which must be entered into a line edit) - the parameter is empty. Thus, to effectively delete a particular action, you only need to unset its type, i.e. select the last (empty) item in the type combo box.

Advanced options

In this group you can define a few advanced options for filters, that allow you to refine your filtering.

Using the combo box, you can toggle the applicability of the corresponding filter: The incoming choice shows that it may be considered for incoming mails (i.e. on Check Mail), while the outgoing choice shows the same for outgoing mails (i.e. on Send).

Note

Note that it is currently not possible to define a filter that is not considered for manual filtering. This is a known limitation.

Note

Filters are automatically named unless you explicitely rename them using the Rename... button. The dialog assumes that it should continue auto-naming the filter as long as the filter name starts with “<”.

Click on the OK button to apply all changes and close the dialog, Apply to apply all changes and leave the dialog open, and Cancel to close the dialog without applying any changes.

Note

If you apply filter changes, i.e. via OK or Apply, only valid filters are actually copied to the internal filter manager.

A filter is considered invalid if

  1. the search pattern contains only empty rules or

  2. the action list is empty.

Similarly, empty rules and actions are removed from the pattern and action list respectively, before the filter is saved.

Search Patterns

The most common use of filters is to filter on the sender of messages. This can be done by choosing From. A good bet for a mailing list would be <To or Cc>. But there are other criteria a filter can search for:

<message>

Searches the whole message (i.e. headers, body and attachments, if any).

<body>

Searches the body of the message.

<any header>

Searches the headers of the message.

<To or Cc>

Searches the “To” and “Cc” header fields of the message.

<size>

Sets upper or lower bounds on the message size.

<age in days>

Sets upper or lower bounds on the message age.

Any other string

Searches the header field that is given by that string.

Filter Action

The most common use of filters is to sort incoming messages to certain folders. This can be done by choosing move to folder. Here's a list of all possible actions:

move to folder

This will move the message to another folder. You currently cannot use IMAP folders as a target.

mark as

This allows you to mark the message as read or important (flagged), but also as forwarded, replied, etc.

set identity to

This will set your identity if you reply to this message.

set transport to

This will set the method of transport (e.g. SMTP) if you reply to the message.

set Reply-To to

Will modify the Reply-To field of this message. This can be useful for mailing lists that automatically set a Reply-To which you don't like.

forward to

Will forward the message to another email address.

redirect to

Will redirect the message to another email address.

bounce

Will try to return the message to the sender as undeliverable.

Caution

This will only work if the senders email address is valid. That's not the case for most spam.

confirm delivery

Will try to return a message to the sender that indicates successful delivery of his or her message.

This action allows you to select who will get delivery receipts from you. Though you can enable the sending of delivery confirmations in the Configure KMail... dialog (section Security) we recommended to not send them to everyone, since this makes tracking of e.g. spam very easy for the sender.

remove header

Will remove all header fields with the given name from the message. This is useful mainly for removing bogus “Reply-To:” headers.

add header

If no such field is already present, will add a new header field with the given name and value to the message. If there is already a header field with that name, it is overwritten with the new (the given) value. If there are already multiple headers with the given name (e.g. “Received:” headers), an arbitrary one of them is overwritten and the others are left unchanged. This is a known limitation. You may want to combine this filter with the remove header filter above to make sure that there are no other headers with that name in the message.

rewrite header

Will scan the given header field, modify its contents and write it back. The search string is always interpreted as a case-sensitive regular expression. The replacement string is inserted literally except for occurrences of \n, $n and ${nn}, where n is a positive (single-digit, except for the third form) number or 0. These constructs are interpreted as backreferences to substrings captured with parentheses in the search string.

Analogous restrictions as in the add header action apply here, too.

execute command

This will execute a program, but will not modify the message. Specify the full path to the program you want to execute. KMail will block until the program returns. You can feed the program with the parts of the mail: %0, %1, etc. will stand for files representing the message parts. For common mails %0 is the text, %1 the first attachment and so on. Additionally, the whole message is fed into the program's stdin.

Caution

This currently only works if the message has at least one attachment. No, not even %0 will work in the general case!

Tip

You can enter arbitrarily complex shell commands here, since KMail uses a subshell to execute the command line. Therefore, even this command will work (within its limits): uudecode -o $(mktemp kmail-uudecoded.XXXXXX) && echo $'\a'

pipe through

This will feed the message to a program. If that program returns output, the entire message (including the headers) will be replaced with this output. If the program doesn't return output, the message won't change. Specify the full path to the program.

Caution

Be cautious with this action, as it will easily mess up your messages if the filter program does return garbage or extra lines.

Filter Examples

If I am subscribed to the (general) KDE List, I could create a folder for the list (I'll call it KDE-General) and use a filter to automatically transfer new messages from my inbox to my KDE-General folder if they are from the KDE List. Here is how to create this filter:

Procedure 3.0. Filtering a mailing list

  1. Try if Message->Create filter->Filter on Mailing-List... can identify the mailing list (the name of the list should then appear in the menu item). In this case, this works and I am presented a filter that has ‘X-Mailing-List equals <kde@lists.netcentral.net>’ preset. You select the desired destination folder from the folder pull-down menu in the Filter Action group and that's it!

    If that does not work, think of a unique way of identifying the messages you want to filter. The (almost) unique property of my KDE List messages is that they always contain ‘kde@lists.netcentral.net’ in the To: or Cc: field. It's only almost unique, because this fails for cross-posted messages.

  2. Select Settings->Filter Rules....

  3. Press the New button to create an empty filter. It will appear as <unnamed>.

  4. In the Filter Criteria area, select <To: or Cc:> from the first pull-down menu, contains from the second pull-down menu, and type kde@lists.netcentral.net in the text field.

  5. Skip down to the actions section. Select move to folder from the first pull-down menu. A new pull-down menu containing a list of folders will appear. Select the folder that you want the filtered messages to be transferred to if they match the criteria you selected. For this example, you would select KDE-General from the pull-down menu.

You may find that you need to use more powerful criteria to properly filter your messages. For example, you may only want to filter the KDE List messages that are written by your friend Fred Johnson <fj@anywhere.com>. This is where the rest of the matching criteria section comes into play:

Procedure 3.1. Extending the filter

  1. Open up the Configure Filters... window and select the filter you just created.

  2. Since you want to filter all messages that have ‘kde@lists.netcentral.net’ in the To: or Cc: field and that are from Fred, check the Match all of the following radio button.

  3. Now, go to the second search rule and select the following from the pull-down menus: From, contains. Now, type fj@anywhere.com in the text field.

You now have a filter that transfers all KDE List messages that are from fj@anywhere.com.

KDE Logo