4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

postfixとshellで自動メール返信

Posted at

条件

  • ec2環境で、postfixを使ったメールサーバー
  • .forwardとshellスクリプトだけで処理

補足

※vacationを使ってやりたかったけど、メール送信出来ているときとそうでないときがあり、
不安定だったため、仕方なく以下のようにスクリプトで対処した。

スクリプト

.forward

\{USER}, "|sh /home/{USER}/reply.sh"

reply.sh

#!/bin/sh

# 実行ディレクトリ
dir=$(cd $(dirname $0) && pwd)

# fromアドレス
from="no_reply@example.com"

# subject
subject="自動返信メール件名"

# body
body=`cat << EOF
自動返信メール本文

※返信できません

EOF
`

# 自動返信をスキップするFromのアドレスを正規表現で記載
invalid_address="MAILER-DAEMON"

# テンプレート関数
mail_tpl () {
    # subject, bodyをnkfでエンコード
    subject2=`echo $subject | /usr/local/bin/nkf -Mj`
    body2=`echo "$body" | /usr/local/bin/nkf -j`

    mail=`cat << EOF
From: $from
To: $1
Subject: $subject2

$body2
EOF
`
    echo "$mail"
}

# 標準入力有無の判別
if [ -p /dev/stdin ]; then
    # 標準入力取得
    in=$(cat -)

    # Fromアドレスを抽出
    to=`echo "${in}" | egrep -i "^From:" | sed -e "s/From:\s*\(.*\)/\1/g"`

    # スキップ判別
    invalid_num=`echo $to | egrep -i $invalid_address | wc -c`
    if [ $invalid_num -gt 0 ]; then
        exit
    fi

    # メール送信
    echo "`mail_tpl \"$to\"`" | /usr/sbin/sendmail -it
else
    echo "no stdin"
fi


4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?