Skip to content


SendMail メール受信をトリガーにしてプロセスを起動

このエントリーをはてなブックマークに追加
はてなブックマーク - SendMail メール受信をトリガーにしてプロセスを起動
Share on Facebook
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Livedoor Clip
Share on FriendFeed
Share on GREE

 SendMailでメールを受信したタイミングでプロセスを起動させる。

 【環境】

  • CentOS 5
  • sendmail-8.13.8-8.el5

  1. 適当にプログラムを書く。今回はPHPで実験
  2. 
    $ cd $HOME/bin
    $ vi test.php
    #!/usr/bin/php -q
    <?php
    $filepath = "test.txt";
    $filetext = "メールが届いたよ!";
    $fp = fopen($filepath, "w");
    @fwrite( $fp, $filetext, strlen($filetext) );
    fclose($fp);
    ?>
    

  3. .forwardファイルをホームディレクトリに作成
  4.  SendMailは.forwardファイルを参照し、受信したメールのforward(転送)を行う。
     転送のみではなく、パイプを使用して自作プログラムやシェルなどに渡すことも可能。
     今回は上記のtest.phpに処理を渡す。
    
    $ cd $HOME
    $ vi .forward
    "| [test.phpが置いてあるパス]/test.php"
    

     尚、このファイルのアクセス権限を自ユーザのみread/write可能としておかないとforwardしてくれません。
     /var/log/maillogで怒られます。
    
    Jun 24 07:26:50 localhost sendmail[3783]: o5NMQnCE003782: forward /xxx/xxx/.forward: Group writable file
    

     以下のように変更。
    
    $ chmod 600 .forward
    

  5. /etc/smrshディレクトリに作成したプログラムのシンボリックリンクを置く
  6.  SendMailではforwardによる不正なプログラムの実行を制限しており、/etc/smrshディレクトリ配下に存在する実行ファイルしか実行できないようになっている。
     よってtest.phpのリンクを/etc/smrshに置く。
    
    $ ln -s [test.phpが置いてあるパス]/test.php /etc/smrsh
    

    ■メモ
    さくらインターネットの環境では、.forwardファイルの代わりに.mailfilterファイルを作成する。


    以上。

    関連するかもしれない記事



    他のブログも読む

Posted in Linux/FreeBSD.

Tagged with , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

*