■概要

複数ドメイン宛のメールを自宅サーバーのメールアドレス宛に集約する。
ここでは、プロバイダアカウント宛メール、GMailアカウント宛メール、Hotmailアカウント宛メール※をFetchmailで自宅メールサーバーに取り込む。
なお、FetchmailではHotmailアカウント宛メールを直接取得することはできないので、hotwaydを経由して取り込む。

2004年11月19日以降に取得したHotmailアカウント(hotmail.co.jp)宛メールは、Hotmail側制約によりhotwaydで取得できない

※メールサーバー(sendmail編Postfix編qmail編)構築済であること


■hotwaydインストール(Hotmailアカウント宛メールを取り込む場合のみ)

(1)xinetdインストール
hotwaydはxinetd配下で動作するため、xinetdをインストールする。
[root@centos ~]# rpm -q xinetd ← xinetdインストール確認
パッケージ xinetd はインストールされていません

※以下はxinetdがインストールされていない場合のみ
[root@centos ~]# yum -y install xinetd ← xinetdインストール

[root@centos ~]# /etc/rc.d/init.d/xinetd start ← xinetd起動
xinetd を起動中:                                           [  OK  ]

[root@centos ~]# chkconfig xinetd on ← xinetd自動起動設定

[root@centos ~]# chkconfig --list xinetd ← xinetd自動起動設定確認
xinetd          0:off   1:off   2:on    3:on    4:on    5:on    6:off ← ランレベル2~5のonを確認

(2)hotwaydインストール
[root@centos ~]# yum -y install libxml2 libxml2-devel ← hotwaydインストールに必要なパッケージをインストール

[root@centos ~]# wget http://jaist.dl.sourceforge.net/sourceforge/hotwayd/hotwayd-0.8.4.tar.gz ← hotwaydダウンロード

※最新版のURLはダウンロードページで確認すること

[root@centos ~]# tar zxvf hotwayd-0.8.4.tar.gz ← hotwayd展開

[root@centos ~]# cd hotwayd-0.8.4 ← hotwayd展開先ディレクトリへ移動

[root@centos hotwayd-0.8.4]# ./configure && make && make install ← hotwaydインストール

[root@centos hotwayd-0.8.4]# cd ← hotwayd展開先ディレクトリを抜ける

[root@centos ~]# rm -rf hotwayd-0.8.4 ← hotwayd展開先ディレクトリを削除

[root@centos ~]# rm -f hotwayd-0.8.4.tar.gz ← ダウンロードしたhotwaydを削除

■hotwayd起動(Hotmailアカウント宛メールを取り込む場合のみ)

[root@centos ~]# vi /etc/xinetd.d/hotwayd ← xinetd用hotwayd設定ファイル新規作成
service hotwayd
{
	only_from = 127.0.0.1
	disable = no
	type = unlisted
	socket_type = stream
	protocol = tcp
	wait = no
	user = nobody
	groups = yes
	server = /usr/local/sbin/hotwayd
	port = 8110
}

[root@centos ~]# /etc/rc.d/init.d/xinetd restart ← xinetd再起動(hotwayd起動)
xinetd を停止中:                                           [  OK  ]
xinetd を起動中:                                           [  OK  ]

[root@centos ~]# chkconfig hotwayd on ← hotwayd自動起動設定

[root@centos ~]# chkconfig --list hotwayd ← hotwayd自動起動設定確認
hotwayd         on ← onを確認

■Fetchmailインストール

[root@centos ~]# yum -y install fetchmail ← Fetchmailインストール

■Fetchmail設定

(1)Fetchmail設定
Fetchmailの設定は各ユーザで行なう。
ここでは、プロバイダアカウント宛メール、GMailアカウント宛メール、Hotmailアカウント宛メールをcentosユーザが取り込むものとする。
[centos@centos ~]$ vi .fetchmailrc ← Fetchmail設定ファイル作成
# 共通設定
set daemon 300 # 300秒間隔でメールチェックを行なう
set postmaster root # 最終的なメールの送信先
set no bouncemail # エラーメールをpostmasterに送る
set syslog # ログを/var/log/maillogに記録する

# 全サーバー共通デフォルト設定
defaults
  protocol auto
  no mimedecode
  no fetchall # 未読メールのみ取り込む場合
  #fetchall # 既読・未読にかかわらず全てのメールを取り込む場合
  no keep # 取り込んだメールをサーバー上から削除する場合
  #keep # 取り込んだメールをサーバー上に残しておく場合

# プロバイダアカウント宛メール取り込み設定※POP3Sが提供されている場合
poll xxxxxxxx # プロバイダ受信メールサーバー名
  protocol pop3
  port 995
  username "xxxxxxxx" # プロバイダユーザ名
  password "xxxxxxxx" # プロバイダパスワード
  ssl
  mda "/usr/sbin/sendmail centos@wakuwakustation.com" # 転送先メールアドレス

# プロバイダアカウント宛メール取り込み設定※POP3のみでPOP3Sが提供されていない場合
poll xxxxxxxx # プロバイダ受信メールサーバー名
  username "xxxxxxxx" # プロバイダユーザ名
  password "xxxxxxxx" # プロバイダパスワード
  mda "/usr/sbin/sendmail centos@wakuwakustation.com" # 転送先メールアドレス

# GMailアカウント宛メール取り込み設定
poll pop.gmail.com
  protocol pop3
  port 995
  user "xxxxxxxx" # GMailユーザ名
  pass "xxxxxxxx" # GMailパスワード
  ssl
  mda "/usr/sbin/sendmail centos@wakuwakustation.com" # 転送先メールアドレス

# Hotmailアカウント宛メール取り込み設定(受信トレイのメール)
poll localhost # hotwaydサーバー名(localhost)
  protocol pop3
  port 8110 # hotwayd待ち受けポート番号
  username "xxxxxxxx@hotmail.com" # Hotmailユーザ名
  password "xxxxxxxx" # Hotmailパスワード
  mda "/usr/sbin/sendmail centos@wakuwakustation.com" # 転送先メールアドレス

# Hotmailアカウント宛メール取り込み設定(迷惑メールフォルダのメール)
poll localhost # hotwaydサーバー名(localhost)
  protocol pop3
  port 8110 # hotwayd待ち受けポート番号
  username "xxxxxxxx@hotmail.com/Junk E-Mail" # Hotmailユーザ名
  password "xxxxxxxx" # Hotmailパスワード
  mda "/usr/sbin/sendmail centos@wakuwakustation.com" # 転送先メールアドレス

[centos@centos ~]$ chmod 600 .fetchmailrc ← Fetchmail設定ファイルのパーミッションを所有者以外参照できないようにする

(2)GMail設定(GMailアカウント宛メールを取り込む場合のみ)
GMailの設定でPOPアクセスを有効にする

(3)Hotmail設定(Hotmailアカウント宛メールを取り込む場合のみ)
Hotmailの言語を英語にする
サインイン⇒個人情報⇒オプション⇒言語⇒English

■Fetchmail起動

[centos@centos ~]$ fetchmail ← Fetchmail起動

■Fetchmail確認

取り込み対象メールアドレス宛にテストメールを送信してみて、メールチェック間隔時間経過後、送信したテストメールがfetchmail起動ユーザ宛に届いていることを確認

■Fetchmail自動起動設定

(1)Fetchmail自動起動設定
システム起動時にfetchmail設定ユーザ(ホームディレクトリに.fetchmailrcがあるユーザ)でfetchmailを自動起動できるようにする。
[root@centos ~]# vi /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプト作成※
#!/bin/bash
#
# Fetchmail
#
# chkconfig: - 99 20
# description: Fetchmail auto start script

# Source function library.
. /etc/rc.d/init.d/functions

start() {
    # Start daemons.
    for user in `ls /home/`
    do
        if [ -f /home/$user/.fetchmailrc ]; then
            if [ ! -f /home/$user/.fetchmail.pid ]; then
                echo "fetchmail for $user starting..."
                su $user -s "/bin/bash" -c "/usr/bin/fetchmail"
            else
                PID=`cat /home/$user/.fetchmail.pid|cut -d " " -f 1`
                ps $PID>/dev/null
                if [ $? = 0 ]; then
                    echo "fetchmail for $user is already started..."
                else
                    echo "fetchmail for $user is restartng..."
                    su $user -s "/bin/bash" -c "/usr/bin/fetchmail"
                fi
            fi
        fi
    done

    if [ -f /root/.fetchmailrc ]; then
        if [ ! -f /var/run/fetchmail.pid ]; then
            echo "fetchmail for root starting..."
            /usr/bin/fetchmail
        else
            PID=`cat /var/run/fetchmail.pid|cut -d " " -f 1`
            ps $PID>/dev/null
            if [ $? = 0 ]; then
                echo "fetchmail for root is already started..."
            else
                echo "fetchmail for root is restartng..."
                /usr/bin/fetchmail
            fi
        fi
    fi
}

stop() {
    # Stop daemons.
    if [ -f /var/run/fetchmail.pid ]; then
        echo "fetchmail for root stoping..."
        /usr/bin/fetchmail --quit
    fi

    for user in `ls /home/`
    do
        if [ -f /home/$user/.fetchmail.pid ]; then
            echo "fetchmail for $user stoping..."
            su $user -s "/bin/bash" -c "/usr/bin/fetchmail --quit"
        fi
    done
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        run="0"

        if [ -f /var/run/fetchmail.pid ]; then
            PID=`cat /var/run/fetchmail.pid|cut -d " " -f 1`
            ps $PID>/dev/null
            if [ $? = 0 ]; then
                run="1"
                echo "fetchmail for root is running..."
            fi
        fi

        for user in `ls /home/`
        do
            if [ -f /home/$user/.fetchmail.pid ]; then
                PID=`cat /home/$user/.fetchmail.pid|cut -d " " -f 1`
                ps $PID>/dev/null
                if [ $? = 0 ]; then
                    run="1"
                    echo "fetchmail for $user is running..."
                fi
            fi
        done

        if [ $run == "0" ]; then
            echo "fetchmail is not running"
            exit 1
        fi
        ;;
    *)
        echo "Usage: fetchmail {start|stop|restart|status}"
        exit 1
esac

exit $?

[root@centos ~]# chmod +x /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプトへ実行権限付加

[root@centos ~]# /etc/rc.d/init.d/fetchmail start ← Fetchmail起動
fetchmail for centos is already started...

[root@centos ~]# chkconfig --add fetchmail ← Fetchmail起動スクリプトをchkconfigへ追加

[root@centos ~]# chkconfig fetchmail on ← Fetchmail自動起動設定

[root@centos ~]# chkconfig --list fetchmail ← Fetchmail自動起動設定確認
fetchmail       0:off   1:off   2:on    3:on    4:on    5:on    6:off

(2)メールサーバーでウィルス&スパムチェック(Qmail-Scanner+Clam AntiVirus+SpamAssassin+maildrop)対応
[root@centos ~]# vi /etc/rc.d/init.d/fetchmail ← fetchmail起動スクリプト編集
# Source function library.
. /etc/rc.d/init.d/functions

----追加(ここから)----
[ -f "/var/qmail/bin/qmail-scanner-queue.pl" ] && \
export QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl"
----追加(ここまで)----

[root@router ~]# /etc/rc.d/init.d/fetchmail restart ← fetchmail再起動
fetchmail for xxxxxxxx stoping...
fetchmail: バックグラウンドの fetchmail (PID=1670) が終了しました。
fetchmail for xxxxxxxx starting...