rmail spam filter
for Gnu Emacs 21

Examples of spam definitions Download Installation BBDB and spam TODO Thanks Version history

Co-maintainers: Bernd Kiefer and Dale Worley

Current version: 2.4g, Sep 3, 2004

For all you RMAIL users who always wanted to filter spam in RMAIL. You can conveniently define what spam is via the custom utility, where you can specify which senders, recipients, subjects and/ or contents are considered spam (examples below), and you'll never see these again.
There are several ways to add items to the list of spam definitions:

  1. Emacs-21: menubar -> options -> customize Emacs -> specific group -> rmail-spam-filter <enter>
  2. Then:  Block future mail with the subject or sender of a message while reading it in RMAIL: just click on the "Spam" item on the menubar, and add the subject or sender to the list of spam definitions using the mouse and a special menu item.   can also highlight some part of the message you are reading and use the menu to add this highlighted text to the list of spam definitions. You need to later also save the list of spam definitions using the same menu item.  Like this...:
spam-menu-item appears on menubar
(to find out which emacs version you have, press M-x emacs-version<enter>; that is, alt-x and then emacs-version<enter>).   The spam can either be automatically saved to a spam-rmail file and deleted from your main rmail file, or just automatically deleted.  This filter works with a regular spool mail file, as well as with pop servers. You do not need to setup any external filter programs and the interface with the filter is entirely via emacs's customize utility. Tested in Gnu Emacs 21.3.

Examples of spam definitions as they appear in customize:

spam list from customize
More examples:

Rmail Spam Definitions Alist: [Hide]
[INS] [DEL] From: operators
            To: all-users
            Subject: computer maintenance
            Action selection: [Value Menu] output to spam folder and delete
[INS] [DEL] From: spam.com
            Action selection: [Value Menu] delete spam
[INS] [DEL] From:
            Subject: make money fast!!
            Action selection: [Value Menu] delete spam
   [State]: you have edited the value as text, but you have not set the option.
Alist matching strings defining spam. [Hide]
Each definition may contain specifications of one or more of the
elements {subject, sender, recipients or contents}, as well as a
definition of what to do with the spam (action item).  A spam e-mail
is defined as one that fits all of the specified elements of any one
of the spam definitions.
Parent groups: [Rmail]


The .el file itself is at


This version of rmail-spam-filter will be part of GNU emacs version 21.4 and above. If you want to try it with an earlier version of emacs (21.3), you need to also get this: get-new-mail-for-spam-filter.el, and some features may not work well….

Having problems trying this with xemacs? Alan Wehmann posted a version that is based on the latest rmail-spam-filter (July 2004) which works with the latest stable xemacs here (wehmann at fnal dot gov). Here are some installation instructions. Please write him regarding this version. Will try to merge this into the regular code soon…

doesn't work for emacs 21.1? here is a patch by Dale Worley.

Installation (in emacs versions before 21.4):

(1) Add to your .emacs:

(load "rmail-spam-filter.el")
(require 'rmail)
(require 'rmailsum) [or (load-library "rmailsum") for older emacs versions]
(load "get-new-mail-for-spam-filter.el")

Then, for all emacs versions, including 21.4 and later:
(2) Open the customization group of rmail-spam-filter as explained above, and toggle and save the rmail-use-spam-filter variable.

(3) specify in variable rmail-spam-definitions-alist what sender, subject and contents make an email be considered spam. you can do this using customize as in the above images, or by putting in your .emacs file an appropriate entry, see below.

In addition, you may:

(4) specify if blind-cc'ed mail (no "To:" header field) is to be treated as spam (variable rmail-spam-no-blind-cc; Thanks to Ethan Brown for this).

(5) specify if rmail-spam-filter should ignore case of spam definitions (variable rmail-spam-filter-ignore-case; Thanks to Ethan Brown for the suggestion).

(6) Specify a "white-list" of trusted senders. If any rmail-spam-white-list string matches a substring of the "From" header, the message is flagged as a valid, non-spam message (Ethan Brown ).

(7) rmail spam filter also works with bbdb to prevent spam senders from entering into the .bbdb file. See variable "rmail-spam-filter-auto-delete-spam-bbdb-entries". This is done in two ways: (a) bbdb is made not to auto-create entries for messages that are deleted by the rmail-spam-filter, (b) when a message is deleted in rmail, the user is offered to delete the sender's bbdb entry as well _if_ it was created at the same day.

(8) if you just absolutely cannot deal with customize and just must use elisp only, try setting what spam is using an entry such as the following in your .emacs file:
(setq rmail-spam-definitions-alist
'(((from . "Barbara Paster")
(to . "all@gfdl.noaa.gov")
(subject . "Seminar")
(contents . "")
(action . output-and-delete))))

(9) If you still have problems having done all this, take a look here at some reports and fixes people have sent in. These seem to help especially if you don't run a recent Emacs version.

BBDB creates too many entries for spam messages? .bbdb grows quickly?

The following code (which should work in both RMAIL and vm) looks at the time the BBDB entry was created. If you are deleting a mail message from a sender whose BBDB entry was just created, it is assumed that this might be a spam message for which no BBDB entry is desired. In this case the code prompts you to delete the BBDB entry as well. This way you wont be asked next time if to change the entry etc, as it wont be there. You easily can have this code delete the message without prompting you if you are desperate.

Want to help?

Here is the TODO list...


to Ethan Brown, Brian Youmans, Nils Sandoy, Bernd Kiefer, Alan Wehmann, Dale Worley for their useful suggestions; Comments welcome...

Version history:

  1. The format of rmail-spam-definitions-alist has changed between versions 0.1 and 0.2, so you need to re-enter it in customize if using the previous version.]
  2. version 1.1 fixes a bug that caused an error message in case an incoming message does not have a "From:" field.
  3. version 1.2 had a bug that made it useless. Discard and upgrade!
  4. version 1.3: a minor cosmetic fix to make sure quitting from rmail and not from rmail-summary does not leave the frame divided in a funny way between pre-existing arbitrary windows and buffers.
  5. version 1.3a: still struggling with the cosmetic quitting issue: found a better solution using defadvice for rmail-quit. Seems OK.
  6. version 1.3d: Ethan Brown has added an option to specify if blind-cc'ed mail (no "To:" header field) is to be treated as spam (variable rmail-spam-no-blind-cc as well as a "white-list" of trusted senders: if any rmail-spam-white-list string matches a substring of the "From" header, the message is flagged as a valid, non-spam message. Found and fixed a bug: if a spam definition includes several fields (e.g. from, to, subject) and only one of them fits the scanned message, the message could have still been defined as spam in some cases. This has been fixed now.
  7. version 1.4: the rmail-spam-filter is turned off automatically when using rmail to look at the rmail-spam-file into which the spam filter puts all messages found to be spam.
  8. version 1.5: fixed a bug where incoming messages with empty "subject", "To" or "from" field could have been defined as spam incorrectly if one of the other fields matched the spam definition list.
  9. version 1.5a: added bbdb-related functionality to prevent spam senders from entering the .bbdb file.
  10. version 1.5b: minor documentation fixes.
  11. version 1.6: can now add a sender or subject to list of spam definitions from the menu-bar while reading a message in rmail.
  12. version 1.6a: spam-filter menus now appear when the cursor is in rmail buffer, in addition to when it is in the rmail-summary-buffer.
  13. version 1.6b: cosmetics and doc fixes.
  14. version 1.6c: adding spam definitions using the menu now results in a message printed at the bottom line of the screen (echo region); also added a customization variable that determines whether to auto save the customization file when a spam definition is added using the menubar.
  15. version 1.6d: rmailsum not provided in emacs before v 21, change accordingly.
  16. version 1.6e: copied the new features added in version 2.0e.

  17. version 2.0: in preparation to including the code in the GNU emacs distribution, make the filter be called from rmail-get-new-mail, and not require the rmail-summary. The number of spam messages found is printed in the echo area, and a couple of new customization variables control the display of this echo area message. this version requires some changes to rmail.el that will hopefully be in the next release of emacs.
  18. version 2.0a,b: minor bug and documentation fixes
  19. version 2.0c: added variable rmail-spam-filter-scanning-messages-now to make sure rmail-bbdb-auto-delete-spam-entries remains inactive while the spam filter scans new messages and deletes spam.
  20. version 2.0d: minor fix.
  21. version 2.0e: added key bindings for all spam filter commands, and also added an option/ menu item to add a highlighted region in the rmail buffer to the spam definitions (thanks to Brian Youmans for these two ideas).
  22. version 2.1: created a separate file for get-new-mail-for-spam-filter.el, so that the older emacs version before 21.4 can use the same code as the newer ones.
  23. version 2.1a: hopefully fixed a bug due to which spam definitions were not saved when they should have been (thanks to Brian Youmans for pointing this out).
  24. version 2.1b: implemented a minimum length of a region that may be added to the spam list.
  25. version 2.1c: added Cc: and To: recipients together for the check of who the email was addressed to.
  26. version 2.1d: added require for rmailsum, needed so that menu bar items can be defined.
  27. version 2.1e: handle the case when rmail-delete-after-output is turned on (thanks to Nils Sandoy for this fix).
  28. version 2.2: support automatically deleting spam entries in BBDB files when using vm, not only RMAIL.
  29. version 2.2a: adding to spam definitions list using the menubar did not work for earlier emacs versions. hopefully fixed now.
  30. version 2.2b: revert the change from last version which did not actually work. instead, add the new custom function custom-mark-to-save to the get-new-mail.el file. Seems OK now. Thanks to Terry Harris for pointing out this problem.
  31. version 2.3: changes by Bernd Kiefer (kiefer at dfki.de): Add a content-type field, and rewrite code to use a new function check-field to simplify code.
  32. version 2.3a: changes by Bernd Kiefer (kiefer at dfki.de): Fix a bug that would cause messages marked deleted to be expunged when getting new mail.
  33. version 2.3b: changes by Bernd Kiefer (kiefer at dfki.de): added a function (rmail-spam-filter-add-content-type-field) to update the customization file to the new format of rmail-spam-definitions-alist. Need to run this function once if you upgraded to version 2.3 and above.
  34. version 2.4: Bug fixes with menu-driven addition of spam definitions. Many changes to variable names etc to conform with gnu-emacs coding standards. No user visible changes, hopefully. This version went into the GNU emacs CVS to be released with 21.4.
  35. Version number 1.0 of the get-new-mail-for-spam-filter.el, with some improvements by Bernd. 2.4a for rmail-spam-filter.el with some minor edits.
  • rmail-spam-fitler 2.4b,c,d,e,f: misc code cleanup and improvements by Dale Worley.
  • get-new-mail-for-spam-filter 1.0d: merging recent changes with most recent gnu emacs from CVS.
  • get-new-mail-for-spam-filter 1.0e: cleanup and improvements from Dale Worley.
  • rmail-spam-fitler 2.4g: documentation fix by Alan Wehmann.