Message Filters

From MozillaZine Knowledge Base

Revision as of 09:17, 23 July 2008; view current revision
←Older revision | Newer revision→

Message filters are useful if you routinely want to perform certain actions on messages, according to criteria that you've specified. For instance, you can have incoming mail automatically sorted into different folders, with certain messages labelled, marked as Junk, or even deleted. Filters can be applied automatically to incoming mail, or you can run them manually when desired.


Creating filters

This tutorial has clear, step-by-step instructions (with screenshots) for how to set up message filters in Thunderbird. You can also create a new filter from any message by right-clicking on the hyperlinked email address of the sender, where it appears above the message pane, and choosing "Create Filter From Message...."

Important: filters must be set up on a per-account basis, and they likewise operate on a per-account basis. There is not currently any option to create "global filters" that will operate on all accounts 'except in the case of multiple POP accounts using the Global Inbox. In this case, you can create filters for Local Folders that will be applied automatically to incoming mail for all those accounts.

Running filters

Filters that are enabled will be run automatically, in the order in which they are listed in the "Message Filters" dialog ("Tools -> Message Filters..."). Note that if you set up a filter to move an incoming message to a specified folder, subsequent filters will not be automatically applied to that message once it has been moved from the Inbox. There is currently no option to have filters be applied automatically to outgoing mail.

To run filters manually on the currently opened folder, select "Tools -> Run Filters on Folder". To manually run only some of your filters rather than all of them, go to "Tools -> Message Filters...", select the filters you want to run, and click the "Run Now" button.

Filtering the message body

You can filter based on the contents of a message body in a POP account if you select "Body". This will also search attachments, though its not clear if thats formally supported. In Thunderbird 1.5 filtering the message body may not work if you have enabled the feature for quarantining individual incoming messages. [1]

By default, Thunderbird doesn't support filtering the message body in a IMAP account. There is no "Body" in the list box. Older versions of Thunderbird will let you filter the message body in a IMAP account if you add "Body" as a custom header . Either create a new filter or edit a existing filter and select "customize" in the left most list box. Enter "Body" as the "new message header". This adds user_pref("mailnews.customHeaders", "Body");' to prefs.js. Unfortunately, this feature doesn't work with recent versions. The developers explanation that its too hard to do because it would require downloading and storing the the message bodies before applying filters doesn't address the issue of why it used to work for many versions.

Filtering based on Tags

Thunderbird doesn't have explicit support for filtering by tags but you can select Custom Headers from the leftmost list box while editing the filter rule and add support for X-Mozilla-Keys, and then test whether it "contains" that tag. Thunderbird uses that header to store tags. For example, X-Mozilla-Keys: forum fyi indicates the message has the forum and fyi tags.

IMAP accounts store tags using message attributes instead, so its not possible to do that with messages stored in remote folders. If you copy the message to a local folder it currently doesn't add a X-Mozilla-Keys header with the tags, though it will add a empty X-Mozilla-Keys header when you compact it. [2]


While you can create message filters to test for Spam thats re-inventing the wheel. Its more efficient to use the junk mail controls . This article explains how to tweak the the junk mail controls to make them learn faster. You could also run other tools such as SpamAssassin or SpamPal that add headers to describe its analysis of the message and some sort of spam score that you can test. For example, SpamPal adds a X-SpamPal: PASS header if the message isn't spam.

When you check for new mail message filters are run before the junk mail controls. There is no way to change that order.

Account specific filters

Each account has its own set of message filters, but they normally have no concept of accounts. You can create account specific message filters by checking the value of the X-Account-Key header.

Use View -> Message Source and find the header, noting what its set to. For example, X-Account-Key: account10. Then create a message filter and scroll the leftmost list box (it defaults to Subject) down to Customize. Enter X-Account-key as the new message header, and press OK. Select X-Account-Key in that list box and test whether "X-Account-Key" "is" "account10" (replace account10 with the value your account uses) and select in "Perform these actions" what it should do.

If the filter doesn't work see if Thunderbird changed the header it tests to Subject behind your back. Sometimes you run into a temporary glitch after adding a customized header. If that happens, exit Thunderbird, restart it , and edit the message filter to use X-Account-Key.


  • Check whether the filter is enabled. It should have a check mark next to it in the enabled column.
  • A bug prevents tags from being automatically applied by filters if you enable "allow antivirus clients to quarantine individual messages". It will work if you run the filter manually.
  • Try running a filter that always matches, and sets something that you can see. That will help identify if you accidentally edited the filter for the wrong account.
  • Try running the filter manually. There might be nothing wrong with your filter, and a bug prevented it from running automatically.
  • Use a text editor to look at the "msgFilterRules.dat" for that account and see if it stored what you thought you specified.
  • Its frequently safer to test using "contains" rather than "is", especially if you had signed up for a newsletter. They might make a trivial change in the From address or subject that breaks your filter. Use "View -> Message Source" to look at the message source to eliminate any possibility that what you see when you read the message is not what the message filter saw.

If you have never written a message filter before and can't figure out what is wrong, try starting smaller and working your way up. For example, you might have written a filter to move certain new messages to a different folder and it doesn't seem to do anything. Modify the message filter to make it add a star to all new messages. That is something harmless you can see and easily undo manually. If that works then modify it to add the star only for those messages that you want to move to a folder. If that works then modify it to move the messages instead of adding a star. The reason for doing this is by starting small its easier to catch and debug common errors, or identify if there is something wrong with that account that prevents any message filter from working etc.

You could create a message filter that adds a star to all messages by testing whether the "Subject" "isn't" "~!@#$%^&*()_+i" (or some other extremely unlikely string) and selecting "Add Star" in the actions list box. Test it using "Tools -> Run Filters on Folder". If no matter what you do it doesn't work, try replacing the contents of the "msgFilterRules.dat" file for your account in the profile as follows. If that doesn't work you probably choose the wrong account directory.

name="Apply a star to all messages"
action="Mark flagged"
condition="OR (subject,isn't,\"~!@#$%^&*()_+i\")"

Other information

  • The file that stores your filter settings in called "msgFilterRules.dat". Each account will have its own "msgFilterRules.dat" file, stored in the account subfolder in the "Mail" or "ImapMail" folder in your profile folder. (In the case of filters for Local Folders, "msgFilterRules.dat" will be found inside the "Local Folders" folder itself.)
  • If you want to duplicate filters from one account to another without manually recreating each individual filter, you can copy "msgFilterRules.dat" from one account subfolder to another. Also note that if you filter based on custom headers you will need to copy the user_pref("mailnews.customHeaders","") line from the prefs.js file in the other profile into your new one.
  • The Message Filter Import/Export extension can be used to import/export message filters. However, its only useful with Thunderbird, Netscape, Mozilla Suite, and SeaMonkey message filters since it doesn't know how to convert other formats.
  • To enable and view your filtered messages log, follow "Tools -> Message Filters," select "Filter Log," and check the box for "Enable the Filter Log." If you can't view your filter log for some reason look in the "filterlog.html" file in that accounts subfolder in the profile.
  • There is no support for regular expressions or wild cards.
  • If you want one or more actions to always occur, test whether the subject matches a string that you're unlikely to ever see such as %#!*&;2Q~! .
  • If your email provider supports IMAP see if they support server side filtering or the Sieve mail filtering language. That may provide more functionality, and any filtering would take place before the mail server stores the message.
  • "Size" in a message filter is in kilobytes. If you append a unit such as "KB" to that number it will silently save the setting as zero.

See also

External links

  • The NewFilters extension adds support for auto forward, auto reply and sound.
  • The MailClassifier extension uses Bayesian filtering to decide what folder to move a new message to.