Product SiteDocumentation Site

13.4.2. Procmail レシピ

New users often find the construction of recipes the most difficult part of learning to use Procmail. To some extent, this is understandable, as recipes do their message matching using regular expressions, which is a particular format used to specify qualifications for a matching string. However, regular expressions are not very difficult to construct and even less difficult to understand when read. Additionally, the consistency of the way Procmail recipes are written, regardless of regular expressions, makes it easy to learn by example. To see example Procmail recipes, refer to 「レシピの例」.
Procmail レシピは、次の形式をとります:
:0flags: lockfile-name * special-condition-character
        condition-1 * special-condition-character
        condition-2 * special-condition-character
        condition-N
        special-action-character
        action-to-perform
The first two characters in a Procmail recipe are a colon and a zero. Various flags can be placed after the zero to control how Procmail processes the recipe. A colon after the flags section specifies that a lockfile is created for this message. If a lockfile is created, the name can be specified by replacing lockfile-name .
A recipe can contain several conditions to match against the message. If it has no conditions, every message matches the recipe. Regular expressions are placed in some conditions to facilitate message matching. If multiple conditions are used, they must all match for the action to be performed. Conditions are checked based on the flags set in the recipe's first line. Optional special characters placed after the asterisk character (*) can further control the condition.
The action-to-perform argument specifies the action taken when the message matches one of the conditions. There can only be one action per recipe. In many cases, the name of a mailbox is used here to direct matching messages into that file, effectively sorting the email. Special action characters may also be used before the action is specified. Refer to 「特別な条件とアクション」 for more information.

13.4.2.1. 配信レシピと非配信

The action used if the recipe matches a particular message determines whether it is considered a delivering or non-delivering recipe. A delivering recipe contains an action that writes the message to a file, sends the message to another program, or forwards the message to another email address. A non-delivering recipe covers any other actions, such as a nesting block. A nesting block is a set of actions, contained in braces { }, that are performed on messages which match the recipe's conditions. Nesting blocks can be nested inside one another, providing greater control for identifying and performing actions on messages.
メッセージが配信レシピと一致する場合、 Procmail はその特定アクションを実行し、他のレシピとメッセージの比較を停止します。非配信レシピと一致するメッセージは他のレシピに対して比較が続けられます。

13.4.2.2. フラグ

フラグは、いかにレシピの条件をメッセージと比較するか、あるいはレシピの条件をメッセージと比較するかどうかを決定する際に非常に重要です。次のフラグが一般に使用されます:
  • A — Specifies that this recipe is only used if the previous recipe without an A or a flag also matched this message.
  • a — Specifies that this recipe is only used if the previous recipe with an A or a flag also matched this message and was successfully completed.
  • B — メッセージの本文を解析し、条件への一致を探します。
  • b — Uses the body in any resulting action, such as writing the message to a file or forwarding it. This is the default behavior.
  • c — Generates a carbon copy of the email. This is useful with delivering recipes, since the required action can be performed on the message and a copy of the message can continue being processed in the rc files.
  • D — Makes the egrep comparison case-sensitive. By default, the comparison process is not case-sensitive.
  • E — While similar to the A flag, the conditions in the recipe are only compared to the message if the immediately preceding the recipe without an E flag did not match. This is comparable to an else action.
  • e — The recipe is compared to the message only if the action specified in the immediately preceding recipe fails.
  • f — フィルターとしてパイプを使います。
  • H — Parses the header of the message and looks for matching conditions. This is the default behavior.
  • h — Uses the header in a resulting action. This is the default behavior.
  • w — Tells Procmail to wait for the specified filter or program to finish, and reports whether or not it was successful before considering the message filtered.
  • W — Is identical to w except that "Program failure" messages are suppressed.
For a detailed list of additional flags, refer to the procmailrc man page.

13.4.2.3. ローカルロックファイルの指定

Lockfiles are very useful with Procmail to ensure that more than one process does not try to alter a message simultaneously. Specify a local lockfile by placing a colon (:) after any flags on a recipe's first line. This creates a local lockfile based on the destination file name plus whatever has been set in the LOCKEXT global environment variable.
別の方法として、コロンの後にこのレシピで使用するローカルロックファイルの名前を指定します。

13.4.2.4. 特別な条件とアクション

Procmail レシピの条件とアクションの前に使用される特別な文字は、それら条件とアクションを解釈する方法を変更します。
The following characters may be used after the asterisk character (*) at the beginning of a recipe's condition line:
  • ! — In the condition line, this character inverts the condition, causing a match to occur only if the condition does not match the message.
  • < — Checks if the message is under a specified number of bytes.
  • > — Checks if the message is over a specified number of bytes.
特別なアクションを実行するには、次の文字を使用します。
  • ! — In the action line, this character tells Procmail to forward the message to the specified email addresses.
  • $ — Refers to a variable set earlier in the rc file. This is often used to set a common mailbox that is referred to by various recipes.
  • | — Starts a specified program to process the message.
  • { and } — Constructs a nesting block, used to contain additional recipes to apply to matching messages.
アクション行で特別な文字を使用しない場合、 Procmail はメッセージを書く為のメールボックスをそのアクション行が指定していると判定します。

13.4.2.5. レシピの例

Procmail は非常に柔軟性のあるプログラムで、この柔軟性の結果、初めから Procmail レシピを作成することは、新規ユーザーにとって困難である可能性があります。
The best way to develop the skills to build Procmail recipe conditions stems from a strong understanding of regular expressions combined with looking at many examples built by others. A thorough explanation of regular expressions is beyond the scope of this section. The structure of Procmail recipes and useful sample Procmail recipes can be found at various places on the Internet (such as http://www.iki.fi/era/procmail/links.html). The proper use and adaptation of regular expressions can be derived by viewing these recipe examples. In addition, introductory information about basic regular expression rules can be found in the grep man page.
次の簡単なサンプルが Procmail レシピの基本的な組み立てを示し、そしてより複雑な構成の基盤を提供します。
基本的なレシピには、次の例で示すように条件が付いていないものさえあります:
:0: new-mail.spool
The first line specifies that a local lockfile is to be created but does not specify a name, so Procmail uses the destination file name and appends the value specified in the LOCKEXT environment variable. No condition is specified, so every message matches this recipe and is placed in the single spool file called new-mail.spool, located within the directory specified by the MAILDIR environment variable. An MUA can then view messages in this file.
A basic recipe, such as this, can be placed at the end of all rc files to direct messages to a default location.
次のサンプルは特定の電子メールアドレスからのメッセージと一致しており、それを廃棄します。
:0 * ^From: spammer@domain.com /dev/null
With this example, any messages sent by spammer@domain.com are sent to the /dev/null device, deleting them.

/dev/null にメッセージの送信

Be certain that rules are working as intended before sending messages to /dev/null for permanent deletion. If a recipe inadvertently catches unintended messages, and those messages disappear, it becomes difficult to troubleshoot the rule.
A better solution is to point the recipe's action to a special mailbox, which can be checked from time to time to look for false positives. Once satisfied that no messages are accidentally being matched, delete the mailbox and direct the action to send the messages to /dev/null.
次のレシピは特定のメーリングリストから配送された電子メールを取り込み、それを指定されたフォルダーに配置します。
:0: * ^(From|Cc|To).*tux-lug tuxlug
Any messages sent from the tux-lug@domain.com mailing list are placed in the tuxlug mailbox automatically for the MUA. Note that the condition in this example matches the message if it has the mailing list's email address on the From, Cc, or To lines.
Consult the many Procmail online resources available in 「その他のリソース」 for more detailed and powerful recipes.

13.4.2.6. スパムフィルタ

新しい電子メールの受信時に Sendmail 、 Postfix 、 Fetchmail によってコールされますので、 Procmail はスパムと戦う強力なツールとして使用されます。
これは特に Procmail が SpamAssassin と併用される時に明確になります。一緒に使用するとこれらの2つのアプリケーションは素早くスパムを認識して、分類するか又は破壊します。
SpamAssassin はヘッダ解析、テキスト解析、ブラックリスト、スパム追跡データベース、及び学習機能のある Bayesian スパム解析を使用し、スパムを正確に素早く識別してタグを付けます。

spamassassin パッケージのインストール

In order to use SpamAssassin, first ensure the spamassassin package is installed on your system by running, as root:
yum install spamassassin
Yum を用いたパッケージのインストールに関する詳細は「パッケージのインストール」を参照してください。
The easiest way for a local user to use SpamAssassin is to place the following line near the top of the ~/.procmailrc file:
INCLUDERC=/etc/mail/spamassassin/spamassassin-default.rc
The /etc/mail/spamassassin/spamassassin-default.rc contains a simple Procmail rule that activates SpamAssassin for all incoming email. If an email is determined to be spam, it is tagged in the header as such and the title is prepended with the following pattern:
*****SPAM*****
電子メールのメッセージ本文もまた、何が原因してスパムと診断されたのかという流動符号が前付けされます。
スパムとしてタグの付く電子メールをファイルするには、次の規則と良く似たものが使用されます:
:0 Hw * ^X-Spam-Status: Yes spam
This rule files all email tagged in the header as spam into a mailbox called spam.
Since SpamAssassin is a Perl script, it may be necessary on busy servers to use the binary SpamAssassin daemon (spamd) and the client application (spamc). Configuring SpamAssassin this way, however, requires root access to the host.
spamd デーモンを開始するには、以下のコマンドを入力します:
systemctl start spamassassin.service
システムの起動時に SpamAssassin を開始するには、次を実行します:
systemctl enable spamassassin.service
Fedora においてサービスを設定する方法の詳細は7章サービスおよびデーモンを参照してください。
To configure Procmail to use the SpamAssassin client application instead of the Perl script, place the following line near the top of the ~/.procmailrc file. For a system-wide configuration, place it in /etc/procmailrc:
INCLUDERC=/etc/mail/spamassassin/spamassassin-spamc.rc