📄 linux-procmail.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><HTML><HEAD><TITLE>Josh's Linux Guide - Filtering Mail with Procmail</TITLE> <META NAME="Description" CONTENT="Instructions on configuring Procmail to filter mail."> <META NAME="Keywords" CONTENT="procmail, configuration, howto, instructions, help, setup, config, conf, procmailrc, linux, filter, mail, set, up, jbm, Josh's Linux Guide"> <META NAME="Author" CONTENT="jbm (jbm@intertek.net)"></HEAD><LINK REL="stylesheet" TYPE="text/css" HREF="default.css"><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#7F007F"><H2><A NAME="0">Setting Up Procmail</A></H2><FONT SIZE="-1"><B>Author: <a href="http://www.oths.k12.il.us/~jbm/">jbm <jbm@intertek.net></a></B></FONT><BR><FONT SIZE="-1"><B>Created on February 28, 1999</B></FONT><BR><FONT SIZE="-1"><B>Last Modified on September 14, 1999</B></FONT><BR><FONT SIZE="-1"><B>Development stage: Alpha</B></FONT><BR><P>Procmail is a great tool for sorting your mail under Linux, but itseems to have a bit of a reputation for being arcane. It can be, butwith a little help, configuring things is really easy to do!</P><P>This document assumes you've got <tt>procmail</tt> installed -sorry to those of you who don't, but most major distributions have apackage for it. It's also very easy to compile.</P><P> Please note that the authors of <tt>procmail</tt> use the word"recipie" to refer to filters. In deference to them, I willdo likewise.</P><a name="toc"><h3>Table of Contents</h3></a> <ul> <li><a href="#forward">Setting up the <tt>.forward</tt> file</a></li> <li><a href="#procmailrc">Setting up the <tt>.procmailrc</tt> file</a></li> <ul> <li><a href="#procmailrc-begin">Beginning the file</a></li> <li><a href="#procmailrc-regexp">Regular Expressions</a></li> <li><a href="#procmailrc-recipe">Recipes</a></li> <li><a href="#procmailrc-backup">Safety Nets</a></li> <li><a href="#procmailrc-example">Example</a></li> </ul> <li><a href="#pine">Configuring Pine</a></li> <li><a href="#further">Further configuring things</a></li> <li><a href="#reading">Further Reading</a></li> </ul> <a name="forward"><h3>Setting up the <tt>.forward</tt> file</h3></a> <p>Place the following line in the <tt>~/.forward</tt> file:</P> <pre>"|IFS=' '&&exec /usr/bin/procmail -f-||exit 75 #jbm" </pre> <P>The quotes <em>are</em> important! Replace the "jbm" with your username (this is so the file is totally unique; some systems have problems if every <tt>.forward</tt> isn't unique). This line should be the only thing in the file, unless you're doing some weird stuff with copying your mail (which I doubt you are).</p> <a name="procmailrc"><h3>Setting up the <tt>.procmailrc</tt> file</h3></a> <a name="procmailrc-begin"><h4>Beginning the file</h4></a> <P> Open up <tt>~/.procmailrc</tt> in your favorite text editor, for example '<tt>pico ~/.procmailrc</tt>' or '<tt>emacs ~/.procmailrc</tt>'. <tt>pico</tt> is a good option for beginners. At the top of this file need to go a few lines: <pre>SHELL=/bin/shMAILDIR=$HOME/mailLOGFILE=$HOME/.procmaillogVERBOSE=yes </pre> Make sure MAILDIR exists! <tt>procmail</tt> will replace the '<tt>$HOME</tt>'s with your home directory (e.g.: <tt>/home/jbm</tt>). The LOGFILE is optional, but I recommend it highly. It lets you see if there was mail redirected to <tt>/dev/null</tt> that shouldn't have been--which is a bad thing. This allows you to debug your regular expressions and/or the order of the recipes you're going to apply. There is an example in the <tt>procmailex</tt> manpage ('<tt>man procmailex</tt>') detailing a "safety net" to copy the last 32 messages to a directory for you; this will be covered later.</P> <p>A recipe in <tt>procmail</tt> is a set of rules that define a filter. These rules are generally a <a href="#procmailrc-regexp">regular expression</a> to match (e.g.: .*, ^From:.*bob@host.dom.au), and then a line or two tellng <tt>procmail</tt> what to do when it matches that regexp.</p> <a name="procmailrc-regexp"><h4>Regular Expressions</h4></a> <p>Regular expressions are extremely powerful ways to express complex things. They are used for pattern matching (like *.* or foo??.txt, but much more complex). Which makes them really hard to learn. There is at least one book on them and many many books devote chapters and sections to them. We will smooth over things a bit with a few "Regular expression formulas," in the table below: <pre>RegExp | Meaning -------+----------------------------------------- . | Matches any character (except newlines) ^ | Beginning of a line .* | Wildcard: matches anything </pre> Yep, those are the only regular expressions you need to know! Now, this is a very simple application of regular expressions, mind you. You should go out and do a little research on them some time, probably when you're learning <tt>perl</tt> or <tt>sed</tt>, two very useful utilities.</p> <a name="procmailrc-recipe"><h4>Recipes</h4></a> <P>Now that you have regular expressions under your belt :^), it's time to construct a recipe. First, every recipe begins with the line <pre>:0 </pre> This has <tt>procmail</tt> scan the headers of the document for matches, use <pre>:0B </pre> for body scanning.</P> <P>The next line is the regular expression <tt>procmail</tt> should match against. These are typically of the format <pre>* ^Subject:.*fm/newsor* ^From:.*oths.k12.il.us </pre> These lines all begin with "*" and continue on with a regular expression. The next line will be the "folder" to place the mail in; this is just a file in the MAILDIR. Unlike the MAILDIR, <tt>procmail</tt> will create this file own its own. When all three lines are combined, it looks something like this: <pre>:0* ^Subject:.*fm/newsfreshmeat:0 # Guide Mail* ^Subject:.*uidejlg </pre> </P> <P><tt>procmail</tt> evaluates the recipies in beginning-to-end order in the file. <tt>procmail</tt> compares a message with the first recipie in a file. If the message matches the criteria of the recipie, <tt>procmail</tt> does what the recipie instructs it to and then stops. If it doesn't match, it tries again with the second recipie, and so on. Therefore, the order you have recipies in does matter! If you want all mail about dogs to go to a dogs folder and all mail from your Significant Other to go to another with all mail from your Significant Other about dogs going to the dogs folder, you had better have the dogs filter above the SO filter.</p> <P><em>Comments</em>: comments must not be within your recipies! Just put them on the :0 line, and noplace else. Trust me.</P> <a name="procmailrc-backup"><h4>Safety Nets</h4></a> <p>In case you mess anything up, you should set up a "safety net", as described in the '<tt>procmailex</tt>' manpage. Begin by making a '<tt>backup</tt>' directory under your MAILDIR. Then add the lines <pre>:0 c backup:0 ic| cd backup && rm -f dummy `ls -t msg.* | sed -e 1,32d` </pre> at the very top of your <tt>.procmailrc</tt> file. That way, <em>every</em> message gets sent through the recipie. This recipie will keep the last 32 messages sent to you in <tt>MAILDIR/backup/</tt>.</p> <a name="procmailrc-example"><h4>An Example <tt>.procmailrc</tt> File</h4></a> <pre>MAILDIR=$HOME/mailLOGFILE=$HOME/.procmaillogVERBOSE=yes:0: # UF Mail* ^Subject:.*Ufies-STAFFuf:0: # JLG Mail* ^Subject:.*uidejlg:0: # FM digest* ^Subject:.*fm/newsfreshmeat:0: # APCi trash* ^Subject:.*ime Limit/dev/null:0: # yet more APCi junk* ^Subject:.*Reached.Hard/dev/null:0: # cron output* ^Subject: cron:cron:0: # SPAM!!!* ^Subject: .*fast.*cash/dev/null </pre> <P>Note that I use :0: for the first line; this sets up a "lockfile" so two <tt>procmail</tt>s don't try to write to the same file simultaneously (that would be a very bad thing). Hopefully this makes setting up a <tt>.procmailrc</tt> easier for you.</p> <a name="pine"><h3>Configuring Pine</h3></a> <P>Of course, it's fine and dandy that you've got your mail all sorted out, but you can't read it from pine this way. Now comes fun stuff. In Pine 4.05 (go grab a copy from <a href="http://www.washington.edu/pine/">http://www.washington.edu/pine/</a>), go to your Folder List from the Main Menu, then to the Incoming-Folders if it's there, and then do Add. It will prompt you for a server, just hit enter. Then it will ask for a folder name. This is what you named it in your <tt>.procmailrc</tt>, ie: <tt>$MAILDIR/foo</tt>. Next it prompts you for a nickname, give it one.</P> <P>As an example, let's say you subscribe to a homebrew list. In your .procmailrc you have:</P> <pre>:0:* ^Subject:.*PC.homebrewhomebrew </pre> <P>Your pine options would then be Folder: "mail/homebrew" and Nickname: "homebrew".</P> <a name="further"><h3>Further configuring things</h3></a> <P>[more to come here soon, on other things like WMMail and other biff-like apps]</P> <a name="reading"><h3>Further Reading</h3></a> <P>Other things to check out for info: <ul> <li>'<tt>man procmailrc</tt>': the <tt>.procmailrc</tt> documentation</li> <li>'<tt>man procmailex</tt>': examples of <tt>.procmailrc</tt></li> <li><a href="ftp://ftp.informatik.rwth-aachen.de/pub/packages/procmail/">Procmail Homepage</a></li> <li><a href="ftp://cs.uta.fi/pub/ssjaaa/pm-tips.html">Procmail tips page</a>: a very extensive collection of <tt>procmail</tt> info.</li> </ul> </P> <HR> <P><B><FONT SIZE="-1">Copyright © 1999 <A HREF="http://www.oths.k12.il.us/~jbm/">jbm (jbm@intertek.net)</A>. All rights reserved. Permission to use, distribute, and copy this document is hereby granted. You may modify this document as long as credit to me is given.</FONT></B></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -