CentOS7でメール受信時にスクリプトを実行する

Postfixの設定

すでにインストール済みのはずなのでアップデートしてから設定。DNSの設定は済んでいるものとする。

yum -y update postfix
cp /etc/postfix/main.cf /etc/postfix/main.cf.org
vi /etc/postfix/main.cf
diff /etc/postfix/main.cf.org  /etc/postfix/main.cf

サブドメイン限定で受信するだけなので編集するのはこれだけ。ユーザーのローカルから処理するように alias_maps の設定も変える。

76a77
> myhostname = noah.rocca.co.jp
113c114
< #inet_interfaces = all
---
> inet_interfaces = all
116c117
< inet_interfaces = 127.0.0.1
---
> #inet_interfaces = 127.0.0.1
386c387,388
< alias_maps = hash:/etc/aliases
---
> #alias_maps = hash:/etc/aliases
> alias_maps = hash:/home/<username>/.aliases

エイリアスに以下を追記して受信時にスクリプト呼び出し。今回、ローカルから処理したいので、ローカル側で以下の設定とした。

touch /home/<username>/.aliases.db
vi /home/<username>/.aliases

test@sub.domain.ltd で受信。

test: "| /home/<username>/commands/bin/script"

Rootで反映して再起動

postalias /home/<username>/.aliases
systemctl restart postfix

なんかあったら以下でログ確認のこと。

tail -f /var/log/maillog

ポートについて

今回は受信だけなので25さえ空けてればOKです。