Javaな日々

NO JAVA, NO LIFE.

fetchmail で i.softbank.jp のメールを自動転送させる

Gmail の設定で i.softbank.jp のメールアドレスを追加することで,SMTP 経由で i.softbank.jp からメールを送信することはできますが,i.softbank.jp に送信されたメールをどう転送するか迷う.

普通に Gmail の設定で i.softbank.jp メールを転送しようとしたが,GmailPOP3 のみしかサポートしておらず,SoftbankIMAP のみしかサポートいないためどうにも無理が….

自分のサーバーを持っている人は fetchmail で転送出来ますので,今回はその設定方法です.

環境

手順

fetchmail をインストール.

$ sudo yum -y install fetchmail

一般ユーザーで fetchmail の設定ファイルを書いてもログイン後にしか起動してくれないので,root に設定ファイルを書きます.

$ su -
# cd /root/
# vi .fetchmailrc
set postmaster <転送先メールアドレス>
set nobouncemail

poll imap.softbank.jp
proto imap
timeout 30
username <i.softbank.jp のメールアドレスの @ 前>
password <i.softbank.jp メールのパスワード>
smtpname <転送先メールアドレス>
smtphost 127.0.0.1
idle
no mimedecode
keep
ssl

自動起動させるために,fetchmail スクリプトを書きます.

# cd /etc/rc.d/init.d/
# vi fetchmail
#!/bin/bash
#
# Fetchmail
# chkconfig: - 99 20
#
# description: Fetchmail auto start script

# Source function library.
. /etc/rc.d/init.d/functions
prog="fetchmail"
FETCH_PID=fetchmail.pid

function getpid()
{
    pid=0
    rc=-1
    if [ "$user" = "root" ]; then
      # for root.
      if [ -f /root/.fetchmailrc ]; then
        rc=0
        if [ -f /var/run/$FETCH_PID ]; then
          pid=`cat /var/run/$FETCH_PID  | sed -n '1p' | awk '{print $1}'`
        fi
      fi
    else
      # for users.
      if [ -f /home/$user/.fetchmailrc ]; then
        rc=0
        if [ -f /home/$user/.$FETCH_PID ]; then
          pid=`cat /home/$user/.$FETCH_PID  | sed -n '1p' | awk '{print $1}'`
        fi
      fi
    fi
}

start() {
    # Start daemons for root.
    user=root
    getpid
    if [ $rc -ne -1 ]; then
      if [ $pid = 0 ]; then
        echo "fetchmail for root starting..."
        /usr/bin/fetchmail
      else
        echo "fetchmail for root already runnning!!!(PID=$pid)"
      fi
    fi
    # Start daemons for users.
    for user in `ls /home/`
    do
      getpid
      if [ $rc -ne -1 ]; then
        if [ $pid = 0 ]; then
          echo "fetchmail for $user starting..."
          su $user -c "/usr/bin/fetchmail"
        else
          echo "fetchmail for $user already runnning!!!(PID=$pid)"
        fi
      fi
    done
}

stop() {
    # Stop daemons for root.
    user=root
    getpid
    if [ $rc -ne -1 ]; then
      if [ $pid = 0 ]; then
        echo "fetchmail for root No running"
      else
        echo "fetchmail for root stoping...(PID=$pid)"
        /usr/bin/fetchmail --quit
      fi
    fi
    # Stop daemons for users.
    for user in `ls /home/`
    do
      getpid
      if [ $rc -ne -1 ]; then
        if [ $pid = 0 ]; then
          echo "fetchmail for $user No running"
        else
          echo "fetchmail for $user stoping...(PID=$pid)"
          su $user -c "/usr/bin/fetchmail --quit"
        fi
    fi
    done
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload)
    stop
    start
    ;;
  status)
    # Status daemons for root.
    user=root
    getpid
    if [ $rc -ne -1 ]; then
      if [ $pid = 0 ]; then
        echo "fetchmail for root No running"
      else
        echo "fetchmail for root runnning...(PID=$pid)"
      fi
    fi
    # Stop daemons for users.
    for user in `ls /home/`
    do
      getpid
      if [ $rc -ne -1 ]; then
        if [ $pid = 0 ]; then
          echo "fetchmail for $user No running"
        else
          echo "fetchmail for $user runnning...(PID=$pid)"
        fi
      fi
    done
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0

スクリプトを認識させ,自動起動の設定をします.

# chkconfig --add fetchmail
# chkconfig fetchmail on

サーバーを再起動させて動作を確認してください.