Deflexion.com blog >> 
Join the conversation >>
 

[Infinite Ink logo]

PROCMAIL QUICK START
An introduction to email filtering with a focus on procmail

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright © Nancy McGough & Infinite Ink
Originally published in 1994 as part of the Filtering Mail FAQ
Last modified 27-Nov-2007


 

   “... Quick Start at Infinite Ink, one of the best primers to a complex program I've ever seen.   
-- Kai Schätzl, 2004 May 16, Procmail list  

 

 

Historical MetaNote   I wrote the first incarnation of this Procmail tutorial more than thirteen years ago as part of the Usenet Filtering Mail FAQ. In 1995 it was published in the book Internet Secrets. In 1999, I HTMLized it and turned it into this Procmail Quick Start. In 2000, a revised version was published in the book Internet Secrets, 2nd Edition. I've been editing and updating this document at all stages, so it is quite mature at this point (and I'm still editing it!). Thanks to all the people who have sent me feedback and suggestions.

 

Linking MetaNote   I regularly update this page so when you want either the latest version of this Procmail Quick Start or the Procmail Section of the Filtering Mail FAQ, please go to <www.ii.com/internet/robots/procmail/qs/>. Please do not use or link to an outdated, unauthorized, or plagiarized version of this. For example, do not link to the Procmail Section of the Filtering Mail FAQ, which I haven't updated in years, and do not link to plagiarizations, which I'm collecting at del.icio.us / Deflexion.com / _MyWriting / Uncredited. If you know anyone who has stolen my writing or design, please tell Pirated-Sites.com and me. If you have a suggestion for a way to name & shame people who are plagiarizing and pirating, please tell me that too!

 

Searching MetaNote   You can use this form to search the web, the Infinite Ink site, or the Deflexion.com site (my blog) for information about Procmail (or about anything!).

 
Web
Infinite Ink
Deflexion.com
 

[New!] Procmail-Related News MetaNote   The Procmail-related news items that used to be posted here are now posted on my Deflexion.com blog with the label PROCMAIL.

 

 

 On This Page  

 

 

 

What is Procmail and What You Need to Use this Quick Start

Procmail is free/libre open-source software that is both a mail processor and a mail delivery agent (MDA). It can be used by either a system administrator or a user to automatically process and deliver incoming mail messages. It can also be used to re-process and re-deliver messages that are already in a mailbox.

This Procmail tutorial is aimed at regular users, not system administrators. In order to use these instructions, you need:

  1. A basic understanding of Internet mail flow, which you can learn about at:

In addition to understanding Internet mail flow, your mail messages must be delivered to a system that:

  1. has procmail installed
  2. you have shell access to
  3. either
    1. uses procmail as the default local delivery agent (LDA), or
    2. uses a message transfer agent (MTA) that lets you use a .forward file to set procmail as your LDA, or
    3. allows you to set procmail as your LDA some other way. For example, with qmail, use dot-qmail files; with MMDF, use .maildelivery; with getmail, use .getmailrc; and with fetchmail, use .fetchmailrc.

If your system satisfies 4a or 4c, make sure that you skip Steps 8 & 9 below, that is, do not set up a .forward file.

 

 
[Note]  If you are looking for a provider that has procmail installed, see my list of Free or Reasonably-Priced IMAP Service Providers — most of the providers who give full Unix shell access also let their users write and manage their own procmail recipes.  

 

 

Terms Used in This Article

Note about the ordering of these terms:  The terms listed earlier are used in the definitions of later terms.

PM  or  pm
In this article I use PM as an abbreviation for Procmail. For example, I use pmlog for the Procmail log file, PMRE for Procmail regular expression (defined below), and PMDIR for the variable that points to the directory that holds your Procmail-related files. Note that there are others who use this abbreviation, for example, Paul Chvostek's Procmail Log Watch awk script is called pmlw and Jari Aalto's Procmail Documentation and Procmail Library are located at pm-doc.sf.net and pm-lib.sf.net.

mail client  or  mail user agent  or  MUA
A program — such as Mulberry, pine, alpine, mutt, Opera Mail (M2), Thunderbird, SeaMonkey Suite, Outlook Express, Outlook, Mail.app, or Eudora — that a user uses to process mail messages. "Processing" includes reading, extracting or launching attachments, moving, printing, deleting, and composing & initiating the sending of messages. For more information, see the definition of MUA at FOLDOC and Email client at [Wikipedia Icon]. For discussion about offline-, online-, and disconnected-mode IMAP clients, see this 2002-June-01 comp.mail.imap message. For a comparison of Pine, Mutt, Mulberry, SeaMonkey Suite and Thunderbird, see Comparison of Pine and Its Main Competitors on the All About Pine page.

news client  or  NNTP client
A program — such as pine, alpine, nn, slrn, Opera Mail (M2), SeaMonkey Suite, or Microsoft Outlook Express — that a user uses to process Usenet and other NNTP messages. "Processing" includes reading, extracting or launching attachments, moving, printing, canceling, and composing & initiating the sending of messages. Note that many NNTP clients can save to, read from, and process the mailboxes that Procmail delivers messages to. For more information, see the definition of news reader at FOLDOC.

server
A computer or a computer program that responds to requests from — or serves — other computers or computer programs. Whether the word “server” means “server software” or “server computer” is usually clear from the context and sometimes people explicitly say which is meant. For example, the following questions are equivalent and both are in standard parlance.

  • “What IMAP server software are you running?”
  • “What IMAP server are you running?”

For more information, see the definition of server and client-server at FOLDOC.

MTA  or  message transfer agent  or  mail transport agent  or  mailer
The underlying program that a mail server uses to send and receive mail messages. There are many MTAs in use today, for example CommuniGate Pro, Courier, Exchange, exim, MMDF, postfix, Post.office, qmail, sendmail, smail, and Zmailer. Note that on some systems the sending MTA is different from the receiving MTA. For more information, see the definition of MTA at FOLDOC, Cameron Laird's personal notes on message transfer agents, Unix Mail Transport Systems reviewed by JdeBP, and the definition of Message Transfer Agent at [Wikipedia Icon].

MSA  or  mail submission agent
To “send” a message, a mail user agent traditionally submitted the message to a message transfer agent. Nowadays most systems have a layer that sits between the MUA and MTA called the mail submission agent or MSA, and the MUA submits the message to the MSA rather than to the MTA. For details, see RFC2476, Message Submission and Gregory Neil Shapiro's Message Submission Agent (MSA). An MSA is used to authenticate the sender and, optionally, to rewrite headers, catch outgoing spam, viruses, & other vermin, and to provide an alternate outgoing port number (because, e.g., port 25 is often blocked or hijacked by Internet Access Providers).

LDA  or  local delivery agent  or  MDA  or  mail delivery agent  or  DA  or  delivery agent  or  local mailer
The program your server uses to deliver mail messages to the mailboxes of users on the system. Sometimes delivery is built into the message transfer agent (for example, sendmail's mail.local and Postfix's local) and sometimes another program, such as tmail, dmail, Dovecot deliver, maildrop, or procmail, is used as the delivery agent.

mailbox  or  mail folder  or  folder
A collection of messages stored on either a local or a remote computer. A mailbox might be a single flat file (this is the structure used by spool (mbox) and c-client MBX mailboxes); it might be a directory with each message a file in the directory (this is the structure of MH, maildir, and Netnews mailboxes); or it might use some other data structure, for example the structure used by c-client mix mailboxes. For more information, see "Appendix A: Folder Formats" in the Procmail FAQ and Before You Begin: Note About MH, maildir, and Traditional Unix Spool (mbox) Mailbox Formats below.

default inbox  or  system mailbox
Whatever you or your system administrator has specified as your primary incoming mailbox. Messages that fall through all your procmail recipes are delivered to this mailbox. To find out your system mailbox, type  procmail -v  at your Unix prompt. To tell procmail to override your default inbox, set the DEFAULT environment variable in your procmailrc. Warning: Overriding $DEFAULT may have undesired consequences with some MTAs.

MAILDIR variable  and  maildir format  (which are two completely different beasts!)
Unfortunately, the word “maildir” is used in (at least) two different ways in the Procmail world. MAILDIR (in all caps) is a Procmail variable that changes Procmail's current working directory and is used to point to the directory where your personal (non-system-spool) mailboxes are stored. For more information about Procmail's MAILDIR variable, see the blinking IMPORTANT note in Step 4 below. Maildir (not in all caps) is the name of a mailbox format. For more information about the maildir mailbox format, see Using maildir format, the maildir man page, and the section below about delivering to a mailbox that is in maildir format.
 
list  or  subscription  or  solicited bulk email  or  SBE or  solicited bulk mail  or  SBM
A mailing list that is used either for group discussion or for newsletters & announcements. For more information, see the definition of mailing list at FOLDOC and Webopedia; also see the definition of UBE (Unsolicited Bulk Email), which is an antonym of these terms.

 
recipe
A set of conditions and an action to be performed if the conditions are satisfied. Note that some programs that filter messages use a different term for this concept. For example, Pine uses the term filtering rule, Visual Office use the term rule, SeaMonkey Suite and Thunderbird use the term message filter, SmartSieve uses the term Mail Filter Rule, and Mulberry uses the terms script, trigger, & rule to encompass this concept.

 
string
A sequence of characters. Letters, digits, punctuation, symbols, tabs, and spaces are all considered characters. A substring is a string that is contained in another string. For more information, see the definition of string at FOLDOC.
 

regular expression  or  RE  or  RegEx  or  RegExp
A string in which some characters are interpreted as “wild cards” or metacharacters. Here are some metacharacters and metaphrases that can be used in a Procmail regular expression (PMRE).

Metacharacter
or Metaphrase
Meaning in a Procmail Regular Expression
. any character other than newline (i.e., other than Line Feed or LF or ASCII character 10)
(string) treat string as a single item
* zero or more of preceding item
? zero or one of preceding item
+ one or more of preceding item
^ newline; usually used to match beginning of line; Note: As discussed in Re: procmail help! $MATCH grabs newline too, the meaning of the ^ metacharacter is different in Procmail than in most other RE tools.
$ newline; usually used to match end of line; Note: As discussed in Re: procmail help! $MATCH grabs newline too, the meaning of the $ metacharacter is different in Procmail than in most other RE tools.
[characterList] any single character in characterList
[^characterList any single character NOT in characterList
\<
\>
each of \< and \> is a metaphrase for a "non-word character"; details are in the definition of word & Matching a Word below, and in Jari Aalto's Procmail and egrep differences
| OR; ORing in Procmail is discussed below
() () is the null item and is always a match; it is used to make a recipe condition more readable, to escape a leading backslash (\), or to escape the special meaning of other metacharacters or metaphrases. For examples, see this discussion of ()< and this discussion of ()\/. Note that it is more common to escape a metacharacter with a backslash, which is discussed in the next item.
\metacharacter Escape the special meaning of metacharacter and treat it as the literal character that it is;  for example  \.  means the dot character rather than “backslash followed by any character” and  \*  means the asterisk character rather than “zero or more backslashes.”  Note: The less-than character (<) needs to be escaped differently. Also see David W. Tamkin's 2004-April-24 message about leading backslash problem (was persistent lock files).  Tip: Another way to escape a metacharacter is to put it in a [characterList], e.g., you can use either \. or [.] to mean a literal dot (.) in a PMRE.  Example: In Setting Keywords or Labels below, there is a recipe condition that contains a literal $ character.
\/ extraction operator (discussed below)
\newline If the last two characters on a line are a backslash (\) followed by newline, this is a “continuation backslash” and it tells Procmail to continue the current line with the text on the next line and ignore these characters: backslash (\), newline, white space (spaces, tabs, and/or newlines).

In Procmail, regular expressions are used in the conditions of a recipe to, e.g., look for a pattern in the Subject header of a message. For more information, see the definition of regular expression at FOLDOC, regular-expressions.info, Regular Expressions HowTo at OmniWeb, Columbia University's page on regular expressions for procmail, the Regular Expression section of era eriksson's Procmail Quick Reference, Scott Wiersdorf's Regular Expressions - Procmail Mail Filtering Syntax, manual page for regexp, the Extended regular expressions section of the procmailrc manual page, and [Wikipedia Icon].
 

 

[Note] 

 
 
 

word
A substring that contains only word characters - letters, digits, and underscores - and is bounded on the left by the beginning of a line or non-word character and on the right by the end of a line or non-word character. For example, in the line
           Subject: testing procmail
the substrings Subject, testing, and procmail are each words and the substrings Subject:, mail, and test are not words. The diagram below shows the relationship between words and substrings, namely: Every word is a substring but not every substring is a word.

           +----------------+
           |  +---------+   |
           |  |  words  |   |
           |  +---------+   |
           |     substrings |
           +----------------+

Since you need to look at the boundaries of a string to determine whether it is a word, the term word only makes sense when you are talking about a substring in the context of a larger string. For more information, see the discussion about matching the word “test” in Understanding and Refining Your Procmail Recipes below.
 
pico
A friendly text editor that is the default pine composer. Throughout this article you can replace pico with a different text editor if you like. To learn more about Pico, see the pico man page and What is Pico at the University of Washington Pine Information Center.
 
or  $HOME  or  ${HOME} 
Your home directory. You can get home by typing any of the following:
cd
cd ~
cd $HOME
cd ${HOME}
Note: Do not use ~ in a procmailrc file; use $HOME or ${HOME} instead.
Remember:  There's no place like ~ 
 

 

See Also

 

 

Strategies

With procmail you can organize your environment variables, recipes, mailboxes, and directories however you like. Only the .procmailrc file, which I discuss below in Step by Step Through Setting Up and Testing Procmail, is required to have this name and to reside in your home directory. This freedom is part of what gives procmail its power but it can lead to chaos if you do not have a strategy for organizing and naming your procmail-related environment variables, files, and directories.

 

Simplicity

  Everything should be as simple as possible but no simpler.   

-- Albert Einstein as quoted and discussed in the
Mentors section of Peter G. Neumann’s page

 

The first strategy that I recommend and try to follow in this article is simplicity. For example, I recommend that you use the Procmail defaults whenever possible and do not set unnecessary environment variables in your procmailrc file(s). For more about this, including problems caused by not using the defaults, see the Warnings ([Warning!]) notes in Step 4 below.

 

Modularize and Plug & Play

A key to good programming is modularization. This, along with simplicity, will help to make your Procmail configuration portable and will make it easy to plug in, unplug, or reorder recipes. Here are examples of recipe modules you might want to use:

In the Step-By-Step section below, I walk you through creating the first two recipe modules and then, after testing, I show you how to unplug rc.testing. I discuss rc.quarantine and rc.spamassassin in the Advanced Recipes section.

 

 

Trivium

 
 

The rc in procmailrc and in my recipe module names is a Unix naming convention that stands for either runtime configuration, runlevel change, or run commands (take your pick!). Thanks to Richard Smith, the maintainer of geekrave.org, for telling me he thinks of rc as runtime configuration; Elmar Hinz for telling me he thinks of it as runlevel change; and Imperial College for FOLDOC, the Free On-Line Dictionary Of Computing, for its definition and history of rc.

 

 

Use Consistent Naming and Formatting Styles

Using consistent naming and formatting styles will help you keep your directories, files, and mailboxes organized. Here are the styles that I use:

What     My Naming & Formatting Style
directory   initial upper-case letter
file   all lower case
procmail recipe module   begins with rc.
unplugged command in an rc file   begins with ##
comment (that is not an unplugged command) in an rc file   begins with #
environment variable    all upper case; also see the 2005-Jan-20 message Re: Variable names by Ruud H.G. van Tol
symbolic link to a file   begins with L.  (for an example, see Tips for Managing Your Procmail Files below)
symbolic link to a directory   begins with LD.  
mailbox (*)   does not contain forward slash (/), dot (.), backslash (\), hash (#), colon (:), asterisk (*), percent (%), quotes, apostrophes, parentheses, brackets, or braces; is not the string  inbox  or  mbox  or  mail.txt  or  inbox.mtx   or  default    or  Mailbox  or any combination of upper and lower case letters of these strings. Also, because of this story, you may want to avoid using core as a mailbox name. (* Also see the Notes About Mailbox Names below.)
incoming mailbox   begins with -- or IN-  (I use one of these prefixes so that all my incoming mailboxes are grouped together when they they are sorted by name.)
incoming “Solicited Bulk Email” (SBE) mailbox   begins with --s- or IN-S-  (you could also think of the S as standing for subscription or shared)
archived SBE mailbox   begins with == or zz- or S- and ends with -YYYY-MM-DD (the date of the latest message in the box)
special mailbox such as SENT, DRAFTS, and TRASH   Some email clients and IMAP servers do not let users change the names of special mailboxes but if it's possible, I like to use all upper case for these so they are more noticeable. I also prefer to use the name SENT-FCC (FCC = “Folder Carbon Copy”) for my sent mailbox to distinguish it from SENT-BCC, which is where I deliver (via procmail) messages that I Bcc to myself.
personal special mailbox     begins with plus (+), dash (-), equals (=), or a number so that it is listed near the top when all mailboxes are sorted by name using an ASCII sort. For example, I use 2Reply for messages that I want to reply to and 2Web for messages that contain information that I'd like to incorporate into my web site.
“catch-all” mailbox   contains either the string catchall or dregs or fallthrough or dropthrough or wildcard or yellow or red (this is where messages that are not delivered to one of my virus, magenta, blue, green, or lime mailboxes are delivered; for more about catch-all mailboxes and my general email strategy, see Reverse Spam Filtering: Winning Without Fighting)
mailbox when I care about the mailbox format   ends with a meaningful extension, for example .unix for Unix mbox format or.cmbx for c-client MBX format.  (Tip: Do not use the extension .mbx because different mail clients use it to mean different mailbox formats and thus it is ambiguous. Also some viruses go after files with the .mbx extension.)

 

* Notes About Mailbox Names [Updated!]  
  Here are more details about my strategy for choosing mailbox names. These notes are in addition to the comments in the starred item (*) above.
  • I use dash (-) rather than underscore (_) or a space ( ) as a visual separator in my mailbox names. I don't like underscores because they are a pain to type (you need to hold down the SHIFT key) and they are sometimes hard to see (they can fade into the baseline of the text and looks like a space). I don't like spaces in mailbox names because in many tools, especially command-line tools, spaces need to be escaped.

  • [New!] Another reason to avoid the underscore (_) character in a mailbox name is discussed in the 2007-August thread with subject "-" and "_" in folder names in the dbmail mailing list.

  • The string “INBOX” has a special meaning to IMAP servers and if you give a mailbox this name, your IMAP client might not be able to see this mailbox on the IMAP server. This is because when it sends the string “INBOX” to the server, the server interprets it to mean a special mailbox, which is not necessarily the mailbox that has the name “INBOX”.

  • Each of the strings mbox, mail.txt, INBOX, and INBOX.MTX have a special meaning to clients and servers that are based on the University of Washington's c-client tools. It is best to not use any of these for a mailbox name, unless, of course, you want a mailbox to be interpreted in the special c-client way.

  • The string “default” has a special meaning to the qmail MTA and it is safest not to use this as a mailbox name.

  • The strings new, cur, and tmp are used in the internal structure of maildir-formatted mailboxes and, to avoid confusion, I recommend that you do not use any of these as a mailbox name.

  • The strings Trash, Discard, Junk, Junkmail, Spam, and Ham have a special meaning on some systems. For example, in the default Tuffmail.com configuration, the Trash, Discard, and Junkmail folders are periodically auto-deleted; at Panix.com, “mail in the Trash folder will get thrown out automatically when it's a month old.” To ensure that you have complete control over your mailboxes, do not use any of the special mailbox names on your system. For example, I use a mailbox named MaybeSpam for my possibly-spam messages and I do not need to worry about my system auto-deleting these messages (because MaybeSpam is not a special mailbox on my system).

  • Do not use a forward slash (/), dot (.), or backslash (\) in a mailbox name because these are used as a namespace hierarchy separator on some IMAP servers. For example, Binc IMAP and UW IMAP use forward slash for the hierarchy separator; Lotus & Exchange use backslash; and Dovecot (when using Maildir-format), Mirapoint, Courier, & Cyrus all use dot. (Note that most IMAP servers can be configured to use a non-default hierarchy separator. For example, Tuffmail.com has configured their Cyrus server to use forward slash as the hierarchy separator.)

    Avoiding these characters in mailbox names makes it possible for you to have a consistent naming style across different IMAP systems, and should make your life easier if your system's IMAP server changes or if you move your mailboxes to a system using a different flavor of IMAP.

  • You might consider not using a dollar sign ($) or exclamation mark (!) in a mailbox name because some IMAP servers, for example Cyrus, do not allow these characters. However, Courier and UW IMAP do seem to allow these characters in a mailbox name.

  • You might consider not using a caret (^) in a mailbox name because Cyrus IMAP will convert any dot (.) in a mailbox name to a caret if the hierarchy separator is changed from slash to dot. If you use ^ in your mailbox names, you will not be able to distinguish between carets that have been converted from dots and carets that have always been carets.

  • I have been wondering if there is any problem with using tilde (~) as the 1st character in a mailbox name? If you have any thoughts about this, please post in this comp.mail.imap thread that I started 2005 Nov 18.

  • If your mail system allows dual-use names (which are aka hybrid folders), i.e., allows a mailbox and a directory (hierarchy) to have the same name, I recommend that you not use the same name for both a mailbox and a directory. This will make your life easier if you move your mailbox hierarchy to a system that does not support dual-use names. For example, if you use UW IMAP with mix-format mailboxes, Dovecot IMAP, Cyrus IMAP, our Courier IMAP, it is possible to create a container named Archive that contains both mailboxes and messages. My advice is to use this Archive container as either a container of only mailboxes (a directory) or as a container of only messages (a mailbox).

  • Some of the other characters and strings to avoid in mailbox names that I list above were suggested in the 2001-July-09 comp.mail.imap message Re: Mailbox Names (Was: Which IMAP Server is better?) and in the 2002-Sep-23 comp.mail.imap thread characters allowed in mailbox names.

  • Do not start a mailbox name with the hash character (#) because some IMAP servers interpret a string that begins with # as an IMAP namespace token. This is discussed in RFC 3501, section 5.1 (Mailbox Naming), which says “Two characters, "#" and "&", have meanings by convention, and should be avoided except when used in that convention.” The next item discusses the problem with ampersand (&).

  • You might also consider not using an ampersand (&), a space, or an upper-case character in a mailbox name for the reasons discussed in the gmane.mail.imap.dovecot thread Ampersand in Folder names causing folders to disappear?, in the gmane.mail.imap.courier.general thread IMAP Folders and the ampersand, in the EMD thread Ampersand in Folder Name, and in the FastMail.FM FAQ about How do I use FastMail.FM's subdomain addresses and "plus- addresses"?  Tip: If you use a Procmail recipe to deliver to a mailbox that contains a space in its name, you need to escape the space by preceding it with a backslash (\).

  • Another reason not to use an ampersand (&) in a mailbox name is that it might be interpreted as a MUTF7 string, rather than an ASCII string, by an IMAP server or client.

  • Some systems have problems with a special mailbox name — such as SENT, DRAFTS, SPAM, and TRASH — if it is created in a non-standard way, e.g. via procmail. This is discussed in this and this thread at EmailDiscussions.com.
 

 

 

My Latest Mailbox-Naming Experiment

Request:  I'm revising my mailbox naming style and I'd like to learn about names that other people use, especially any mailbox-naming scheme that is in common use. I'm thinking about changing the prefix of my incoming mailbox names from IN- to something that will sort above all alphabetic characters in an ASCII sort. I'm leaning towards prefixing these box names with a dash (-), so, for example, incoming procmail mailing-list messages would be put in --procmail. And then I'd put the archive of that mailbox in zz-procmail-YYYY-MM-DD or ==procmail-YYYY-MM-DD because the leading zz- or == will make it sort below my incoming mailboxes. I realize that these are kind of odd-looking names so I'm interested to hear what ideas other people have — please let me know what you think and if you have suggestions.

Note 1:  Someone emailed me and mentioned that if a mailbox name starts with a dash, it is a pain to manipulate from a Unix shell. That is true, but it provides a bit of security if a lamer breaks into your account and tries to read your mailboxes using shell commands such as more or less or pine -f mailboxName (and it's not that much of a pain if you know the trick)!

Note 2:  I'm now using this naming scheme and so far I haven't had problems with Procmail or with any mail client or IMAP server that I've used. For example, here are mailbox names that could be used with my reverse-spam-filtering system:


  --magenta		
  --violet		
  --blue
  --green
  --limegreen		
  --yellow
  --red
  ==archive1		
  ==archive2		
		

To force my most important incoming mailboxes to the top of an ASCII sort, I prefix each mailbox name with an appropriate number of dashes. For example:


  -------green
  ------limegreen		
  -----yellow
  ----blue
  ---red
  --violet		
  -magenta
  ==archive1		
  ==archive2
			

I actually use a modified version of this and don't really have a mailbox name that begins with seven dashes! If you are interested in why I name my incoming mailboxes after the colors of the rainbow, see Reverse Spam Filtering: Winning Without Fighting.

 

 

Ordering Procmail Flags

I recommend that you order Procmail flags using the style that Jari Aalto suggests on his Procmail Tips page in the section called The order of the flags. I use this style throughout this page, for example in the SpamAssassin recipes below.

 

Managing Spam

A lot of people are learning Procmail because they want to automatically separate non-spam messages from spam messages. My strategy is to do the following, in this order:

  1. Have my mail hosting provider do SMTP-level spam rejection using, for example, Greylisting, DNSBLs, or SPF (Sender Policy Framework).
  2. Quarantine or delete viruses.
  3. Deliver or forward known non-spam messages, possibly also setting appropriate flags/labels/keywords.
  4. Pipe the possibly-spam (not known non-spam) messages through a spam-detection tool, such as SpamAssassin, to assign a spam score or spam probability.
  5. Set appropriate flags/labels/keywords on possibly-spam messages.

To implement step number . . .

  1. see IMAP Service Providers page, which describes over 400 providers, many of whom offer SMTP-level spam rejection.
  2. see Snagging Viruses below.
  3. see some or all of the following:
    1. Step by Step Through Setting Up and Testing Procmail below, which includes recipes that catch and deliver specific “Solicited Bulk Email” (mailing-list and newsletter) messages.
    2. Alternatives to the ^TO_ Macro below, which includes more recipes that catch and deliver specific Solicited Bulk Email messages.
    3. Generic SBE Sorting below.
    4. Setting Keywords or Labels below.
    5. Minimizing False Positives With Greenlists and Bluelists below.
  4. see Using SpamAssassin below.
  5. see Setting Keywords or Labels below.

For more about my spam-deflexion strategy and about Unsolicited Bulk Email (UBE or spam), see my Reverse Spam Filtering: Winning Without Fighting page.

For other people's spam-deflexion strategies, see the December-2005 gmane.mail.procmail discussion about Spam and Procmail, which takes place in these two threads. Especially useful is this message from G.W. Haywood, in which he describes the system that he uses on his mail servers and says:

Most people haven't the faintest idea how much work is involved in keeping the bulk of spam and other junk at bay.

 

[Del.icio.us icon] I am collecting Spam-related links at del.icio.us/Deflexion.com/Messaging/Spam.

 

 

Before You Begin: Note About MH, maildir, and Traditional Unix Spool (mbox) Mailbox Formats

[Updated!] Overview

The step-by-step instructions below guide you through setting up Procmail to deliver to mailboxes that are in traditional Unix mail spool format (mbox). To deliver to a mailbox that is in maildir format, you need to append a forward slash (/) to the mailbox name. The table below gives examples of the various types of mailbox specifications that can be used in the action line of a Procmail recipe.

 

      Traditional
Unix mbox format
     
maildir format
      maildir format on a
Courier IMAP server
     
MH format (*)
IN-testing IN-testing/   .IN-testing/ IN-testing/.
IN-S-procmail IN-S-procmail/   .IN-S-procmail/ IN-S-procmail/.
IN-S-vim IN-S-vim/   .IN-S-vim/ IN-S-vim/.
 

     

[Important!]  
  • If Procmail tries to deliver a message to a maildir, MH, or mbox mailbox that does not exist, Procmail will create the mailbox in the appropriate format and then deliver the message to it.

  • If a maildir or mbox mailbox does not exist, it is safest to let Procmail create it (see previous item) rather than trying to create it yourself using Unix commands.

  • In order for Procmail to successfully deliver to a pre-existing mailbox, the mailbox must be in the appropriate format. For example, a maildir or MH mailbox must be a directory and a Unix mbox mailbox must be a file.

  • If you deliver to maildir mailboxes, use procmail 3.15.2 or 3.22+.  3.14 has serious problems delivering to maildir mailboxes and 3.15, 3.15.1, 3.20, and 3.21 have minor problems.

  • (*) If you use MH mailboxes, you will probably want to use the MH/nmh rcvstore program to deliver messages. Details are in Beedub's Wiki in the section Using the procmail program.

  • If you want to deliver to another type of mailbox, consult the relevant documentation. For example, to deliver to . . .

 

 

Details

An explanation of how Procmail delivers to various folder formats is in the latest (3.22) procmailrc man page. Here is the relevant section.

  [ . . .]
  Anything else will be taken as a mailbox  name  (either  a
  filename  or a directory, absolute or relative to the cur-
  rent directory (see MAILDIR)).  If it is a  (possibly  yet
  nonexistent) filename, the mail will be appended to it.
  
  If  it  is  a  directory,  the mail will be delivered to a
  newly created, guaranteed to be unique file named $MSGPRE-
  FIX* in the specified directory.  If the mailbox name ends
  in "/.", then this directory  is  presumed  to  be  an  MH
  folder;  i.e.,  procmail will use the next number it finds
  available.  If the mailbox name ends  in  "/",  then  this
  directory  is presumed to be a maildir folder; i.e., proc-
  mail will deliver the message to a file in a  subdirectory
  named  "tmp"  and  rename  it  to be inside a subdirectory
  named "new".  If the mailbox is  specified  to  be  an  MH
  folder  or maildir folder, procmail will create the neces-
  sary directories if they don't exist,  rather  than  treat
  the  mailbox as a non-existent filename.  When procmail is
  delivering to directories, you can specify multiple direc-
  tories  to  deliver  to  (procmail  will  do  so utilising
  hardlinks).



When delivering to a maildir or MH format mailbox, you do not need to use a lock file so the first line of a recipe does not need the second colon. For example, instead of using this as the first line:

   
:0:   

you can use this:

   
:0   

There are examples of recipes that deliver to maildir-formatted mailboxes in Step 7D and in Using SpamAssassin below.

 

Details Specific to maildir Format

 

 
[Important!]  If you want your system mailboxes $ORGMAIL and $DEFAULT to be in maildir format, it is best to specify this in the procmail source code and recompile procmail. This is discussed in the following messages, which were posted to the Procmail mailing list.  


If you are delivering messages to maildir-formatted mailboxes that will be served to mail clients by Courier IMAP (which supports only maildir format mailboxes), put a dot (.) in front of the mailbox name in your Procmail recipe. This leading dot is a hierarchy separator and will ensure that IMAP clients can see these mailboxes. IMAP clients will display the mailbox name without the leading dot (or the trailing slash, which is just used to tell procmail the format of the mailbox and is not part of the name that's used on the Unix system).

Technical Note:  On Courier IMAP, private mailboxes reside under the “INBOX.” hierarchy. On the server, this hierarchy is usually in the directory $HOME/Maildir but users do not need to tell their IMAP clients the actual name of this directory because the IMAP protocol will pass that information to the IMAP client.

 

 

The Qmail MTA

 
 

Systems that use the qmail MTA usually use maildir-formatted mailboxes. To use Procmail with qmail, put a line like one of the following in the relevant dot-qmail file:

|preline /usr/local/bin/procmail ~/.procmailrc || exit 111
|preline procmail -m -p myprocmail.rc
|preline procmail

(It is not clear to me which of the above is best and it would be great if someone could fill me in so I can include more information about this here.)

To learn more about using Procmail with qmail, see:

Qmail Tip: Depending on the preline command that you use in the dot-qmail file, you may want to put EXITCODE=99 in the Variables Section of your procmailrc. This Procmail EXITCODE will tell qmail that procmail completely handled — e.g. processed & delivered — a message and that qmail can forget about it. If you do not specify this EXITCODE, qmail may think it needs to deliver the message after Procmail finishes processing it and then you will (probably) get two copies of a message, one delivered by Procmail and one delivered by qmail.

 

 

See Also

 

Step by Step Through Setting Up and Testing Procmail

These 18 steps walk you through ensuring that procmail is installed on your system, setting it up to sort your incoming mailing-list messages, and testing your setup.

 

Preliminaries

 

 
[Important!]   Important:  For the following steps to work, you must be a regular user, not root.

If you are a system administrator who is learning Procmail, I highly recommend that you step through this procedure before you try to set up a global procmail rc file. You are much more likely to understand the basics of Procmail if you learn by doing (rather than by reading only). And your system & users will be much less likely to experience an email nightmare if you do your Procmail learning — and making mistakes — as a regular user!

 

 

  1. To find out if procmail is on your system and, if it is, what the path to it is, type one of the following commands.

     

    Shell Command
    csh, tcsh which procmail
    sh, bash, ksh type procmail
    various whereis procmail
    various where procmail
    various locate procmail

    Make a note of the path to procmail because this is needed in Step 9 below.


  1. To find out procmail's version, locking strategies, default rcfile, and your system mailbox (aka your default INBOX), type:
    procmail -v 
    
     

     

    [Important!] 
    • If you are using a version earlier than 3.11pre4, which was released on 29 October 1995, you need to use the procmail expression ^TO rather than the expression ^TO_ in Step 15.

    • If you are using version 3.13 or earlier, procmail cannot deliver to maildir mailboxes (unless you apply a patch to procmail or pipe a message to a program that can deliver to a maildir mailbox). If you deliver to maildir-formatted mailboxes, it is best to use procmail 3.15.2 or 3.22+.  3.14 has serious problems delivering to maildir mailboxes and 3.15, 3.15.1, 3.20, and 3.21 have minor problems.
     


    If your system does not have procmail or does not have the latest version, 3.15.2 or 3.22+, ask your system administrator to install it. You can find out what is the latest version of procmail and where to get it at www.procmail.org.



  2. Rename (move) ~/.forward, ~/.procmailrc, and the global procmailrc, if they exist. This will ensure that you do a clean Procmail setup.
    1. To find out if ~/.forward exists and what its contents are if it does, type:
      cat ~/.forward
      If ~/.forward exists, rename it using a command similar to this:
      mv -i ~/.forward ~/.forward-2007-11-27
      This will ensure that you use the optimal ~/.forward file for your system, which is discussed in Steps 8 and 9 below.


    2. To find out if ~/.procmailrc exists and what its contents are if it does, type:
      cat ~/.procmailrc
      If ~/.procmailrc exists, rename it using a command similar to this:
      mv -i ~/.procmailrc ~/.procmailrc-2007-11-27
      Steps 3 and 4 (in the next section) walk you through setting up a minimal ~/.procmailrc file. If you would like to use some of the recipes that are in your old procmailrc file, I recommend that you call them by using an INCLUDERC command in your new minimal ~/.procmailrc. For an example of an INCLUDERC command, see the end of Step 4 below (in the #### Processing Section ####).


    3. To find out if a global procmailrc exists and what its contents are if it does, type:
      cat /etc/procmailrc
      cat /usr/local/etc/procmailrc
      
      If a global procmailrc exists and if you are the administrator of your system, rename it using a command similar to this:
      sudo mv -i /etc/procmailrc /etc/procmailrc-2007-11-27
                 ^^^^^^^^^^^^^^^
                change to the path
                  on your system
      After you have experience setting up and using a personal procmailrc, you can, if you want, re-install this renamed global procmailrc. Details about using a global procmailrc are in See if There is a Global procmailrc below.

      Note: As discussed here, viaVerio VPS accounts use /usr/local/etc/procmailrc for the global procmailrc file. Also note that you can view the VPS default files and directory structure in the /skel directory. (Verio is where I host the Infinite Ink web pages, including this page.)

      Warning: As discussed in this December-2005 gmane.mail.procmail thread, it seems that there is a Linux distribution that “out of the box” includes a broken /etc/procmailrc.


 

 

Set Up Your Procmail Files for Testing

  1. Create ~/.procmailrc by typing:
    cd
    pico -w .procmailrc
    

     

     
    [Note]    

  2.  

  3. Enter the following in your ~/.procmailrc either by typing it or by using copy & paste*. But first note these warnings.
       
    [Warning!]
     
    • Procmail does not support the expansion of tilde (~). You must use $HOME rather than the ~ character within your .procmailrc. This is documented in the procmailrc man page.

    • Make sure you replace  MAILDIR=$HOME/Msgs  with the directory where your personal (non-system-spool) mailboxes are stored and that this directory exists and is writable by your LOGNAME. The MAILDIR variable specifies Procmail's working directory and all relative paths are relative to this directory.

    • When you first test Procmail, do not set the PATH variable. As Philip Guenther said in this message,
      “you shouldn't alter PATH unless there's something actually missing from it or the order is wrong.”
    • As I discuss in Simplicity above, do not explicitly set any variable unnecessarily. In addition to not explicitly setting the PATH variable, which is discussed in the previous item, it is almost always best to not explicitly set the DEFAULT variable. For examples of problems caused by explicitly setting variables, see: The first two of these threads discuss problems caused by explicitly setting DEFAULT and the third thread discusses a problem caused by setting a user-invented variable named FORMAIL.

    To learn about the Procmail default environment variables on your system, see man procmailrc on your system.
     


    # Note: Anything after a hash character (#) is a comment & is ignored by procmail.
     
    #### Begin Variables Section ####
         
    # It is essential that you set SHELL to a Bourne-type shell if
    # external commands are run from your procmailrc, for example if
    # you use rc.spamassassin, rc.quarantine, or other advanced recipes.
    # Setting SHELL should not be needed for the simple sorting recipes in 
    # this step-by-step section, but to be safe and to future proof your
    # procmailrc, set it anyway! Details are in Check Your $SHELL and $PATH.