複数ドメイン宛メールの集約(Fetchmail)

最終更新日: 2017.01.21

<<トップページ <<新着情報 <<サイト内検索 <<CentOSで自宅サーバー構築 <<Scientific Linuxで自宅サーバー構築

■概要

複数ドメイン宛のメールを自宅サーバーのメールアドレス宛に集約する。
ここでは、プロバイダアカウント宛メール、GMailアカウント宛メール、Hotmailアカウント宛メールをFetchmailで自宅メールサーバーに取り込む。

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


■Fetchmailインストール

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

■Fetchmail設定

(1)Fetchmail設定
Fetchmailの設定は各ユーザで行なう。
ここでは、プロバイダアカウント宛メール、GMailアカウント宛メール、Hotmailアカウント宛メールをfedoraユーザが取り込むものとする。
[fedora@fedora ~]# 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 # 取り込んだメールをサーバー上に残しておく場合

# プロバイダアカウント宛メール取り込み設定
poll xxxxxxxx # プロバイダ受信メールサーバー名
  username "xxxxxxxx" # プロバイダユーザ名
  password "xxxxxxxx" # プロバイダパスワード
  mda "/usr/sbin/sendmail fedora@fedorasrv.com" # 転送先メールアドレス

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

# Hotmailアカウント宛メール取り込み設定
poll pop3.live.com # プロバイダ受信メールサーバー名
  protocol pop3
  port 995
  username "xxxxxxxx@hotmail.com" # Hotmailユーザ名
  password "xxxxxxxx" # Hotmailパスワード
  ssl  mda "/usr/sbin/sendmail fedora@fedorasrv.com" # 転送先メールアドレ>ス

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

(2)GMail設定
GMailアカウント宛メールを取り込む場合のみ、GMailの設定でPOPアクセスを有効にする
また、Fetchmail設定でno keep(取り込んだメールをサーバー上から削除する)設定を行う場合、「POP でメールにアクセスする場合」で「Gmailのメールを削除する」を選択、keep(取り込んだメールをサーバー上に残しておく)設定を行う場合、「POP でメールにアクセスする場合」で「Gmailのメールを既読にする」を選択する

■Fetchmail起動

[fedora@fedora ~]# fetchmail ← Fetchmail起動

■Fetchmail確認

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

■Fetchmail自動起動設定

(1)Fetchmail自動起動設定
システム起動時にfetchmail設定ユーザ(ホームディレクトリに.fetchmailrcがあるユーザ)でfetchmailを自動起動できるようにする。
[root@fedora ~]# vi /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプト作成※
#!/bin/bash
#
# Fetchmail
#
# chkconfig: 2345 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@fedora ~]# chmod +x /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプトへ実行権限付加

[root@fedora ~]# /etc/rc.d/init.d/fetchmail start ← Fetchmail起動
Starting fetchmail (via systemctl):                        [  OK  ]

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


■関連コンテンツ




▲このページのトップへ戻る

プライバシーポリシー