procmail 是一個電子郵件過濾器 (mail filter)。它可 將到的信件在存入您的信箱前先做處理,例如將信件分類等。不過這 裡要教您的是如何利用 procmail 來將被編碼的中文信 件解碼成純文字,以及在不同編碼的信件轉換為您常用的編碼。
在您的 home 目錄下建立 .procmailrc 檔案,內容如下:
# 在用 procmail 處理前先將信件儲存下來,以免發生意外
# 您可以設定那些信件不必備份,例如由 MAILER-DAEMON 來的退信
:0 c
* !^From.*MAILER-DAEMON
mail/procmail-backup
# 將以 quoted-printable 或 base64 編碼的信件解碼
:0
* ^Content-Type: *text/plain
{
:0 fbw
* ^Content-Transfer-Encoding: *quoted-printable
| mimencode -u -q
:0 Afhw
| formail -I "Content-Transfer-Encoding: 8bit" \
-I "X-Mimed-Autoconverted: quoted-printable to 8bit by procmail"
:0 fbw
* ^Content-Transfer-Encoding: *base64
| mimencode -u -b
:0 Afhw
| formail -I "Content-Transfer-Encoding: 8bit" \
-I "X-Mimed-Autoconverted: base64 to 8bit by procmail"
}
# 這裡判斷信件是否為 GB 編碼,若是則轉為 BIG5 編碼
# 如果您要由 BIG5 轉為 GB 碼,要將例子中所有的 big5
# 及 gb2312 互換,並將 hc -m b2g 改為 hc -m g2b
:0
* ^Content-Type:.*text/plain;.*charset=gb2312
{
:0 fw
| hc -m b2g -t /usr/local/lib/chinese/hc.tab
:0 Afhw
| formail -I "Content-Type: text/plain; charset=big5" \
-I "X-Charset-Autoconverted: gb2312 to big5 by procmail"
}
# 將信件存回信箱
:0:
${ORGMAIL}
這個例子中,配合了 mimencode,formail 將信件解碼,並用 hc 將 GB 編碼的信件轉為 BIG5 編碼。所以您還必須安裝這些工具。
最後再將更改您的 .forward 檔就可以了
"|IFS=' ' && exec /usr/bin/procmail -f- ~/.procmailrc ||exit 75 name"
注意:其中最後的 name 要改為您自己的簽入名稱(login name)。