玄箱 Debianでメールを一元管理する

メモ:  Category:kuro_box

複数あるメールアドレスのメールを玄箱で全て受信し、IMAPを使うことでメールは サーバに保存します。ついでにSPAMを判定しSPAMを削除する手間を軽減しようと思います。

構築する環境は、次のようなイメージになります。

メール管理イメージ

  1. fetchmailでメールを受信します。
  2. 受取ったメールをexim4へ渡し各ユーザーへ配信します。
  3. exim4からprocmailへメールを渡します。
  4. procmailで振り分ける直前にspamassassinでSPAM判定を行います。
  5. SPAM判定した結果をメールのヘッダに追加し、procmailへ返します。
  6. SPAM判定された内容を確認し、procmailを使って任意のディレクトリへメールを振り分けます。
  7. メールは、doveot(IMAP)を通じてクライアントとの処理を行います。

fetchamailの設定

fetchmailにメールを受信するよう設定します。fetchmailの設定は、ユーザーディレクトリの .fetchmailrcに作成します。

$ vi .fetchmailrc

set postmaster admin      // エラーメールの送信先
set no bouncemail         // エラーメールを送信者に転送しないようにする

defaults
  no mimedecode           // メールをデコードしないようにする
  keep                    // メールをサーバに残すようにする(削除する場合:flush)

poll pop3.hoge.or.jp      // POP3サーバ
  protocol pop3           // 受信プロトコル
  user メールユーザー名   // メールユーザー
  password パスワード     // メールパスワード
  smtphost localhost      // ローカルのsmtpへフォワードする
  is ローカルユーザー名 here  // フォワードするユーザー名

パスワード等、重要な内容を記述するためパーミッションを変更しておきます。

$ chmod 600 .fetchmailrc

fetchmailコマンドを実行し、受信できることを確認したら定期的に受信するようcronを設定します。(この例では、3分毎)

$ crontab -e
*/3 * * * * /usr/bin/fetchmail > /dev/null

procmailの設定

procmailのレシピにprocmailのレシピにspamassassinを通すよう設定します。procmailの設定は、 ユーザーディレクトリの.procmailrcに行います。

:0 fw
*!^X-Spam.*
| /usr/bin/spamassassin

メールクライアントで受信中、エラーが発生したため念のため、ロックファイルを指定しました。

:0 fw: spamassassin.lock
*!^X-Spam.*
| /usr/bin/spamassassin

spamassassinをデーモンとして使用する場合、/usr/bin/spamassassinを/usr/bin/spamcとします。

spamassassinの処理後、ヘッダのX-Spamを確認しSPAM判定されたものをSPAMディレクトリに振り分けます。

spamassassin

spamassassinが最初に起動すると、ユーザーディレクトリに.spamassassinディレクトリを 作成します。ディレクトリ内には、次のようなファイルが作成されます。

auto-whitelist
bayes_seen
bayes_toks
user_prefs

spamassassinの設定は、大きく分けて2つあります。すべてのユーザーに対して設定される /etc/mail/spamassassin/local.cfとユーザー毎に設定するuser_prefsがあります。 いろいろ検索してみると、user_prefsは先駆者が作成したファイルと差し替えることで 運用開始時からある程度スパムの判定をしてくれるようになるようです。

参考:TLEC user_prefs

とりあえず私の環境では、今後の勉強のためにコツコツと設定したいと思います。

初期状態で作成されるファイルは、次のように作成されます。

### local.cf

# This is the right place to customize your installation of SpamAssassin.
#
# See 'perldoc Mail::SpamAssassin::Conf' for details of what can be
# tweaked.
#
###########################################################################
#
# rewrite_header Subject *****SPAM*****
# report_safe 1
# trusted_networks 212.17.35.
# lock_method flock

user_prefs

# SpamAssassin user preferences file.  See 'perldoc Mail::SpamAssassin::Conf'
# for details of what can be tweaked.
###########################################################################

# How many points before a mail is considered spam.
# required_score                5

# Whitelist and blacklist addresses are now file-glob-style patterns, so
# "friend@somewhere.com", "*@isp.com", or "*.domain.net" will all work.
# whitelist_from        someone@somewhere.com

# Add your own customised scores for some tests below.  The default scores are
# read from the installed spamassassin rules files, but you can override them
# here.  To see the list of tests and their default scores, go to
# http://spamassassin.apache.org/tests.html .
#
# score SYMBOLIC_TEST_NAME n.nn

# Speakers of Asian languages, like Chinese, Japanese and Korean, will almost
# definitely want to uncomment the following lines.  They will switch off some
# rules that detect 8-bit characters, which commonly trigger on mails using CJK
# character sets, or that assume a western-style charset is in use.
#
# score HTML_COMMENT_8BITS      0
# score UPPERCASE_25_50         0
# score UPPERCASE_50_75         0
# score UPPERCASE_75_100        0

動作確認

初期設定の状態でもSPAM判定は行われるので、メールを受信しヘッダを確認します。 ヘッダに次のように追加されていれば動作しています。

X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on *****
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=ham  version=3.0.3

bluenote by BBB