トップPHP携帯の空メール機能を実装
2006年03月14日

携帯の空メール機能を実装

仕事でやったので、次の機会のためにメモしときます。

やること
 1.ユーザーがregist@hoge.comへ空メールを送る
 2.サーバーでメールを受ける
 3.内容をコマンドラインphpに渡す
 4.phpでメール内容を解析し、ユーザーに会員登録用フォームのURLを挿入したメールを返信


aliasesの設定

vi等で/etc/aliasesを開き、おしりのところに以下を追加。

regist: "| /path/to/script/empty_mail_regist.php"

追加や編集をしたらnewaliasesを実行して設定を反映する。(忘れてしまいがちなので注意)


phpスクリプトを用意

/path/to/script/empty_mail_regist.phpを書く。PEARのMail_Mimeクラスを使ってヘッダを解析してます。

#!/usr/local/bin/php -q

<?php

//PEARのライブラリ読み込み
require_once('/path/to/pear/Mail/mimeDecode.php');

//メールソースを標準入力から読み込み
$source = file_get_contents("php://stdin");
if(!$source)
{
  echo "fail!\n";
  exit();
}

//メール解析
$params['include_bodies'] = true;
$params['decode_bodies']  = true;
$params['decode_headers'] = true;

$decoder = new Mail_mimeDecode($source);
$structure = $decoder->decode($params);
$from = mb_convert_encoding(mb_decode_mimeheader($structure->headers['from']), mb_internal_encoding(), "auto");
if( preg_match( '/<([^>]+)>$/', $from, $regs )){ $from=$regs[1]; }
$from = strtolower($from);

//会員登録用URL
$body = "https://hoge.com/user_regist.php?mail=" . rawurlencode($from);

//メール返信
mb_internal_encoding("utf-8");
mb_language("japanese");
mb_send_mail($from, "empty_mail", $body, "From: admin@hoge.com");

?>


以上。
仕組みがわかれば案外簡単です。(やってることが超シンプルってこともありますが)
上記の場合はヘッダからFromを取得してるだけですが、添付ファイルとかサブジェクトなどを取得する場合は日本語関連の問題などが出てきそうな気がします。


参考URL
http://labo0000.jp/maildo/maildo.html

投稿 : 2006年03月14日 17:07

トラックバック

トラックバックURL:

コメント

HOGE LUMBER COMPANYの関係者ならいいんですけど、hoge.comを使うとよくない気がしますよ

http://pentan.info/else/form_example.html

投稿者 pentan : 2009年08月22日 06:25

コメントしてください




保存しますか?


 

kawama.jp | © Kawama. All rights reserved.