携帯からの空メールの処理|送信者アドレスが取得できない問題

このQ&Aのポイント
  • 携帯から送られてくる空メールを処理するプログラムを作成中です。しかし、送信者のアドレスが取得できず、メールを送信する際にエラーが発生しています。
  • 作成したプログラムでは、携帯からの空メールをエイリアスでPHPプログラムに渡し、送信者に自動返信する仕組みです。
  • Mail_mimeDecode::decode()を使用してメールを解析し、送信者のアドレスを取得していますが、取得できない問題が発生しています。print_r()を使用して$structureを出力したところ、headersが空の配列となっています。
回答を見る
  • ベストアンサー

携帯からの空メールの処理

こんにちは。今、PHP5.3で、携帯からの空メールを処理するプログラムを作っています。 具体的には、空メールが携帯から送られてくると、エイリアスでPHPプログラムに渡し、送信者に自動返信するというプログラムです。 現在、以下のようなプログラムを作っていますが、うまく動きません。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー <?php require_once("/opt/lampp/lib/php/PEAR.php"); // PEAR::Mailライブラリ require_once("/opt/lampp/lib/php/Mail.php"); // PEAR::Mail_mimeライブラリ require_once("/opt/lampp/lib/php/Mail/mimeDecode.php"); $params['include_bodies'] = false; $params['decode_bodies'] = false; $params['decode_headers'] = true; $params['input'] = file_get_contents("php://stdin"); $params['crlf'] = "\r\n"; //メールを解析する $structure = Mail_mimeDecode::decode($params); print_r($structure); //送信元を取得する $mail = $structure->headers['from']; $mail = addslashes($mail); $mail = str_replace('"','',$mail); preg_match("/<.*>/", $mail,$str); if($str[0]!=""){ $str=substr($str[0],1,strlen($str[0])-2); } $mail = $str; //送信データを設定する $recipients = $mail; $new_from = 'register@7ws.jp'; $subject = 'empty mail OK!'; $body ='登録が完了しました。'; $headers = "From:".$new_from; //メールを送信する $headers = array(); $headers['From'] = $new_from; $headers['To'] = $recipients; $headers['Subject'] = $subject; $headers['Sender'] = $new_from; $mail = Mail::factory('sendmail'); $result = $mail->send($recipients, $headers, $body); ?> ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 参考にしたのは、http://www.phppro.jp/phptips/archives/vol35/1 と「PHP×携帯サイト デベロッパーズバイブル」です。 プログラムを実行しても、空メールの送信者のアドレスが取得できず、 送り先を空白のまま、メールを送信していまい、エラーとなります。 正常に送信者アドレスを取る方法をご存知の方が、いらっしゃいましたら、ご教授お願いします。 なお、$structure = Mail_mimeDecode::decode($params);の下で、 print_r($structure);を実行したところ、 print_r($structure); ( [headers] => Array ( [] => ) [ctype_primary] => text [ctype_secondary] => plain ) という表示なっていました。headersがArrayなのに空っぽなところに、 問題があるのではないかと思います。以上、よろしくお願いします。

  • PHP
  • 回答数1
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • junkUser
  • ベストアンサー率56% (218/384)
回答No.1

エイリアスの書き方に問題があるのではないでしょうか。 php://stdin から何も受け取れていないようです。

sirius2003
質問者

お礼

いままで、インターネットからメールを送れなかった関係で、 telnet localhost 25 で擬似的なメールセッションをおこなっていました。 その際、mail from:やrcpt to:は指定していたのですが、 Dataコマンドの後の、ヘッダFromやヘッダToをつけていませんでした。 この結果を元に、送信されたメールデータを、test_mailファイルとして 使用していたため、うまくいきませんでした。 ヘッダToやヘッダFromをつけたらうまくいきました。 ありがとうございました。

sirius2003
質問者

補足

ご回答、ありがとうございます。 エイリアスの書き方の問題を切り分けるため、コマンドラインにて、 /opt/lampp/bin/php /opt/lampp/htdocs/****/****/empty_mail_v2.php < test_mail という具合に、test_mailというファイルを用意して、実行しておりますが、 やっぱりうまくいきません。なお、test_mailファイルは、/var/spool/mailに入っていた、メールの実データをアドレスだけ変えて使用しています。 なにか、他に原因があるのでしょうか? 以上、よろしくお願いいたします。

関連するQ&A

  • Xserverでの空メール実装について

    Xserverで空メールを実装しようとしています。 しかしうまく動作しません。 マニュアルを参考に「メールの振り分け設定」より 条件:あて先が ○○○○○@△△△△ を一致する 処理方法:| /○○○○/□□□□/sippai.php へ 転送する としました。 しかし、携帯電話より○○○○○@△△△△へメール送信しても、 差出人:MAILER-DAEMON@△△△.xserver.jp 件名:Undelivered Mail Returned to Sender と返信されてしまいます。 サーバー側の設定が何か必要なのでしょうか? スクリプトを以下に記載します。 使用スクリプト;PHP5、ライブラリ;PEAR <?php //PEAR::MailCu require_once '/aaaaaa/bbbb/cccc/ddddd/eeeee/fff/Mail.php'; //PEAR::Mail_mimeCu require_once '//aaaaaa/bbbb/cccc/ddddd/eeeee/ggg/mimeDecode.php'; //メールソースを読み込む $source = file_get_contents("php://stdin"); if(!$source){ exit(); } //メールを解析する $decoder = new Mail_mimeDecode($source); $structure = $decoder->decode($params); //送信元を取得する $from = $structure -> headers['from']; $from = mb_decode_mimeheader($from); $from = mb_convert_encoding($from, mb_internal_encoding(),'auto'); if(preg_match( '/<(.*?)>$/' , $from , $match)){ $from = $match[1]; } $from = trim($from); $from = strtolower($from); //送信データを設定する $recipients = $from; $new_from = '○○○○○@△△△△'; $subject = 'empty mail ok!'; $body = 'http://fmob.jp'; //メールを送信する $headers = array(); $headers['From'] = $new_from; $headers['To'] = $recipients; $headers['Subject'] = $subject; $headers['Sender'] = $new_from; $mail = Mail::factory('sendmail'); $result = $mail->send($recipients, $headers, $body); ini_set('log_errors', '1'); ini_set('error_log', 'エラーログのパス'); ?> よろしくお願いします。

    • ベストアンサー
    • PHP
  • 空メール受信時 php8 さくらインターネット 

    phpを8.0にバージョンアップしたら空メール受信時のphp起動が稼働しなくなりました。 稼働しなくなった理由や原因など教えていただきたいです。 よろしくお願いします。 過去のバージョン時に参考にしたサイトは以下の通りのものです http://www.aiwake.co.jp/modules/bulletin/index.php?page=article&storyid=3 -------------reply.php------------------------ #!/usr/local/bin/php -q <?php // PEARのパスを設定 $path = '/home/{さくらアカウント名}/pear/PEAR/'; set_include_path(get_include_path() . PATH_SEPARATOR . $path); require_once 'Mail/mimeDecode.php'; // 受信メールから読み込み $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = file_get_contents("php://stdin"); $params['crlf'] = "\r\n"; if (!$params['input']) { // 読み込み失敗 exit(); } $structure = Mail_mimeDecode::decode($params); // Fromフィールド(会員のアドレス)の取得 $fromField = $structure->headers['from']; $fromField = addslashes($fromField); $fromField = str_replace('"','',$fromField); $fromField = preg_replace('/(^.*<|>$)/', '', $fromField); // 会員のアドレスを暗号化 $addr = base64_encode($fromField); // Subjectフィールド(広告コード)の取得 $subjectField = $structure->headers['subject']; $subjectField = mb_convert_encoding($subjectField,"UTF-8","JIS"); $ad = $subjectField; // メール作成 $to = $fromField; $fromAddr = "{メールアドレス}"; $from = "From: $fromAddr"; $subject = "{メールタイトル}"; $body = "{メール内容}"; // メール送信 if (mb_send_mail($to, $subject, $body, $from)) { exit(); } else { echo "メールの送信に失敗しました。\n"; exit(1); // エラーコード 1 を返す } ?> -------------reply.php------------------------   to "| /usr/local/bin/php -q /home/gayhiroba/MailBox/{さくらアカウント名}/reply.php" exit

  • 空メールからの標準入力が受け取れません。

    よろしくお願いいたします。 CentOS5.2 & PHP5(お名前.COMのVPSディフォルトです。) お名前.COMのVPSで空メールでのユーザー認識したいと思って います。説明いただいているサイトを参考に、以下のコードを動かして います。 空メールを送ってみると、 $sss=file_get_contents("php://stdin"); をコメントアウトしない場合は、 テストで作らせているテキストファイルもでき、返信メールが帰ります。 しかし、上記文を入れるとテストで作らせているテキストファイルも 返信もありません。 アドバイスいただければ助かります。 pearは Mail_Mime 1.8.0 stable Mail_mimeDecode 1.5.4 stable でインストールしてあります。 <?php //PEARのライブラリ読み込み require_once("/usr/share/pear/Mail/mimeDecode.php"); //メールソースを標準入力から読み込み $sss=file_get_contents("php://stdin"); $frm="AAAAAA"; if(!$source) { exit(); // 読み込み失敗 } //メール解析 $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $decoder = new Mail_mimeDecode($sss); $structure = $decoder->decode($params); $from = mb_convert_encoding(mb_decode_mimeheader($structure->headers['from']), mb_internal_encoding(), "auto"); $gdata="ddddddd".$sss.$frm; $ffp2="ggg.txt"; $fh=fopen($ffp2,"w"); fwrite($fh,$gdata); fclose($fh); $from="xxxxx@hhhhh.com"; //メール返信 $to = $from; $title = "空メールの返信(例)"; $body = "登録が完了しました。\n(実際は何も登録していません。)\n※このメールは配信専用です。\n返信されても対応は出来ませんので、ご了承下さい。"; $from = "From: support@XXXXXXXXX"; mb_internal_encoding("SJIS"); mb_language("japanese"); mb_send_mail($to, $title, $body, $from); ?>

    • 締切済み
    • PHP
  • PHP5での空メールが実行されない

    現在PHP5を利用して携帯の空メールを実装しており、3日ほどトライし続けておりますが、携帯側にエラーが返信される所で先に進めずにいます(;;)プログラムは勉強し始めて4ヶ月程度の未熟者です。どなたか是非ご教授頂ければと思います。よろしくお願いします!! ちなみに、pearは使えているのを、他のライブラリで確認しました。 【使用言語とライブラリ】 PHP5、pearの『Mail.php』『mimeDecode.php』 【参考書籍】 PHP携帯 デベロッパーズバイブル 【利用サーバー】 Xサーバー 【メールの振り分け設定】 『条件』あて先が ○○○@△△.com を一致する 『処理方法』 | /home/サーバーID/ドメイン/public_html/フォルダ名/フォルダ名/empty_mail.php へ 転送する 【PHPのソース】 #!/usr/bin/php5 <?php //PEAR::Mailライブラリ ini_set('include_path', '/home/サーバーID/ドメイン/public_html/pear/PEAR'); require_once 'Mail/Mail.php'; //PEAR::Mail_mimeライブラリ ini_set('include_path', '/home/サーバーID/ドメイン/public_html/pear/PEAR'); require_once 'Mail/mimeDecode.php'; //メールソースを読み込む $source = file_get_contents("php://stdin","r"); if(!$source){ exit("標準入力に失敗"); } //メールを解析する $decoder = new Mail_mimeDecode($source); $structure = $decoder->decode($params); //送信元を取得する $from = $structure -> headers['from']; $from = mb_decode_mimeheader($from); $from = mb_convert_encoding($from, mb_internal_encoding(),'auto'); if(preg_match( '/<(.*?)>$/' , $from , $match)){ $from = $match[1]; } $from = trim($from); $from = strtolower($from); //送信データを設定する $recipients = $from; $new_from = ○○○@△△.com'; $subject = 'empty mail ok!'; $body = '(仮)こんにちわ'; //メールを送信する $headers = array(); $headers['From'] = $new_from; $headers['To'] = $recipients; $headers['Subject'] = $subject; $headers['Sender'] = $new_from; $mail = Mail::factory('sendmail'); $result = $mail->send($recipients, $headers, $body); ini_set('log_errors', '1'); ini_set('error_log', 'エラーです'); 【エラー内容(すごく長いですが…)】 This is the Postfix program at host sv225.xserver.jp. I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below. For further assistance, please send mail to <postmaster> If you do so, please include this problem report. You can delete your own text from the attached returned message. The Postfix program <○○○@△△.com>: Command died with status 255: "/usr/bin/maildrop". Command output: PHP Warning: require_once(/home/サーバーID/ドメイン名/public_html/pear/PEAR/Mail/Mail.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /home/サーバーID/ドメイン名/public_html/mobile_search/gotempty_mail2.php on line 7 PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required '/home/サーバーID/ドメイン名/public_html/pear/PEAR/Mail/Mail.php' (include_path='home/サーバーID/ドメイン名/public_html/pear/PEAR') in /home/サーバーID/ドメイン名/public_html/mobile_search/gotempty_mail2.php on line 7 X-Powered-By: PHP/5.1.6_ Content-type: text/html_ _ とても煩雑でごめんなさい(;;) どうかよろしくお願いします。

    • ベストアンサー
    • PHP
  • pearのMailライブラリがうまくいきません

    自分で解決できなかったので、申し訳ないですが他力本願させていただきます。 空メール登録のシステムを作っています。 スクリプト以前に、メールを送ると 「Fw:Returned mail: see transcript for details」のタイトルで 以下のような文章でかえってきます。 rom: Mail Delivery Subsystem <MAILER-DAEMON@www.abc.com> Date: Fri, 23 Jan 2009 19:15:27 +0900 To: <hoge@ezweb.ne.jp> Subject: Returned mail: see transcript for details The original message was received at Fri, 23 Jan 2009 19:15:27 +0900 from xxxxxxxxxx.ezweb.ne.jp [11.111.11.123] ----- The following addresses had permanent fatal errors ----- "|php /var/www/hoge-html/hogehoge/empty_mail.php" (reason: Service unavailable) (expanded from: <hohogege@hoge.jp>) ----- Transcript of session follows ----- smrsh: "empty_mail.php" not available for sendmail programs (stat failed) 554 5.0.0 Service unavailable aliasesの設定には以下のようにしています。パスはあっています。 hohogege:"|php /var/www/hoge-html/hogehoge/empty_mail.phpp" sendmail, postfixはインストールされています。 ちなみにスクリプトは以下のように書きました。 //PEAR::Mailライブラリ require_once 'Mail.php'; //PEAR::Mail_mimeライブラリ require_once '/usr/share/pear/Mail/mimeDecode.php'; //メールソースを読み込む $source = file_get_contents("php://stdin"); if(!souce){ exit(); } //メールを解析する。 $decoder = new Mail_mimeDecode($source); $structure = $decoder->decode($params); //送信元を取得する。 $form = $structure->headers['from']; $form = mb_decode_mimeheader($from); $form = mb_convert_encoding($form, mb_internal_encoding(), 'auto'); if(preg_match( '/<(.*?)>$/', $from, $match)){ $from = $match[1]; } $from = trim($from); $from = strtolower($from); //送信データを設定する $recipients = $from; $new_from = 'hogehoge@abc.jp'; $subject = 'empty mail ok'; $body = 'http://www.hogehoge.jp'; //メールを送信する $headers = array(); $headers['From']=$new_from; $headers['To']=$recipients; $headers['Subject']=$subject; $headers['Sender']=$new_from; $mail = Mail::factory('sendmail'); $result = $mail->send($recipients, $headers, $body);

    • ベストアンサー
    • PHP
  • Mail_mimeDecodeでメール本文取得

    index.php <form action="mailto:info@hoge.com" method="get" id="join"> <input type="hidden" name="body" value="<?php echo $friend;?>" /> </center></form> empty_mail.php $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = file_get_contents("php://stdin"); $params['crlf'] = "\r\n"; $mail_data = Mail_mimeDecode::decode($params); $MailBody = getbody($mail_data); if (!$MailBody) { print "cannot get MailBody"; // エラー処理 }// $MailBodyを使った処理 function getbody($arg) { if ($arg->ctype_primary == 'multipart') { foreach($arg->parts as $parts) { $ret = getbody($parts); if ($ret) { return $ret; } } } if ($arg->ctype_primary == 'text') { if ($arg->ctype_secondary == 'plain') { if (strtolower($arg->ctype_parameters['charset']) == 'iso-2022-jp') { return mb_convert_encoding($arg->body, "UTF-8", 'JIS'); } else { return $arg->body; } } } return false; } //メールを解析する $decoder = new Mail_mimeDecode($source); $structure = $decoder->decode($params); //送信元を取得する $mail = $structure->headers['from']; $mail = addslashes($mail); $mail = str_replace('"','',$mail); $from = $structure -> headers['from']; $from = mb_decode_mimeheader($from); $from = mb_convert_encoding($from, mb_internal_encoding(),'auto'); if(preg_match( '/<(.*?)>$/' , $from , $match)){ $from = $match[1]; } $from = trim($from); $from = strtolower($from); //送信データを設定する $recipients = $from; $new_from = 'info@hoge.com'; mb_language('ja'); mb_internal_encoding('sjis'); $subject = mb_encode_mimeheader(mb_convert_encoding("登録URL", "JIS", "auto"), "JIS"); $body = "下記のURLをクリックして登録を行ってください http://hoge/hoge.php?". $MailBody . "". session_name()."=". htmlspecialchars(session_id()); と$friendを登録フォーム画面にいくまで情報を維持したいのですが、ご教授お願いします。

    • ベストアンサー
    • PHP
  • メールを受信した後、特定メールを削除

    メールフィルターで受信後にプログラムを起動し特定のメールだけ削除したいのですが可能でしょうか? メールフィルターは以下です。CCはメールボックスから削除しない。 cc "| /home/○○/○○/mailgo.php" PEARを使ってまず受信します。 $params['input'] = file_get_contents("php://stdin"); $structure = Mail_mimeDecode::decode($params); メールのファイル名などを取得し、その後、削除したいメールも特定できたのですが どうすればいいのでしょうか?

    • 締切済み
    • PHP
  • PEARのmimeDecodeでメール解析をしたい

    レンタルサーバにPEARをインストールし、 mimeDecode.phpを使って、メールを解析したいのですが、 上手くいきません。 下記のサイトを参考にしましたが、やはりできませんでした。 http://d.hatena.ne.jp/makotoworld/20071106/1194309820 http://www.abe-tatsuya.com/web_prog/php/mail2php.php メールを分解するファイル(.php)では、 file_get_contents("php://stdin")のようなカタチで、 メール情報はキャッチできています(確認済みです)が、 それを、 $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = file_get_contents("php://stdin"); // 標準入力 $params['crlf'] = "\r\n"; $mail_data = Mail_mimeDecode::decode($params); #-- 本文の取得 $MailBody = $mail_data->body; $MailBody = mb_convert_encoding($MailBody,"UTF-8","JIS"); としても、取り出せないのです。 (つまり、$MailBody は空っぽのままなのです。) さらに、 #-- From フィールドの取得 $FromAddress = $mail_data->headers['from']; #-- To フィールドの取得 $ToAddress = $mail_data->headers['to']; としている部分では、error_reportingが、 Notice: Undefined index: from in~~ Notice: Undefined index: to in~~ と、エラーを吐いています。 これも謎です…。一体、どういうことになっているんでしょうね?!汗 ・パスの書き方に誤りがあり、 正常にPEARにアクセス(利用)できていない? ・そもそも、インストールが怪しい? ・上記の、参考にした2サイトでは、 mimeDecode.phpの扱い方が違うのですが、この違いとは? これは、PEARのバージョンの新旧の問題? だとすると、今となっては、 どちらか一方しか使えない、もしくは、両方とも使えない? つまり、サイトを参考にして書いたコードがそもそも古くて使い物にならない? ・ファイルのパーミッションの問題?(保存形式はEUCとしています) 以上の点が、気になっています。 この事態を打開するためには、どう切り分けながら改善していったらよろしいでしょうか。 どなたかお詳しい方、どうか教えて下さい。 なお、 FFFTPにより、PEARのファイル群が置かれていることは確認しています。 (mimeDecode.phpもあります。) コードの冒頭は、 #!/usr/local/bin/php <?php error_reporting(E_ALL); としています。 レンタルサーバは、さくらインターネットのスタンダードプランです。 PHPバージョンは、5.2.14。 PEARは、最近、http://pear.php.net/go-pear経由でインストールしました。 長くなりましたが、宜しくお願い致します。

    • ベストアンサー
    • PHP
  • PHPのmimeDecode.php について

    Redhat Linux4 で、メールを受信した際、添付ファイルがあれば 特定のフォルダに添付ファイルを保存するPHPを作っています。 以下のメール受信時にPHPプログラムを起動すると、 添付ファイル付きメールなのに、 $structure->ctype_primary = 'multipart' となってくれず 途方にくれています。 どなたかご教授いただきたくよろしくお願いいたします。 require_once("Mail/mimeDecode.php"); $input = file_get_contents("php://stdin"); $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $decoder = new Mail_mimeDecode($input, "\r\n"); $structure = $decoder->decode($params); $GB['sMailBody']=$structure->ctype_primary; この結果、$GB['sMailBody']は、text となってしまいます。

    • 締切済み
    • PHP
  • 自動返信でのMail_mimeDecode

    //メールソースを読み込む $source = file_get_contents("php://stdin"); if(!$source){ exit(); } $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = file_get_contents("php://stdin"); $params['crlf'] = "\r\n"; $mail_data = Mail_mimeDecode::decode($params); $MailBody = getbody($mail_data); if (!$MailBody) { print "cannot get MailBody"; // エラー処理 }// $MailBodyを使った処理 function getbody($arg) { if ($arg->ctype_primary == 'multipart') { foreach($arg->parts as $parts) { $ret = getbody($parts); if ($ret) { return $ret; } } } if ($arg->ctype_primary == 'text') { if ($arg->ctype_secondary == 'plain') { if (strtolower($arg->ctype_parameters['charset']) == 'iso-2022-jp') { return mb_convert_encoding($arg->body, "UTF-8", 'JIS'); } else { return $arg->body; } } } return false; } //メールを解析する $decoder = new Mail_mimeDecode($source); $structure = $decoder->decode($params); //送信元を取得する $mail = $structure->headers['from']; $mail = addslashes($mail); $mail = str_replace('"','',$mail); $from = $structure -> headers['from']; $from = mb_decode_mimeheader($from); $from = mb_convert_encoding($from, mb_internal_encoding(),'auto'); if(preg_match( '/<(.*?)>$/' , $from , $match)){ $from = $match[1]; } $from = trim($from); $from = strtolower($from); //送信データを設定する $recipients = $from; $new_from = 'get@hoge.com'; mb_language('ja'); mb_internal_encoding('sjis'); $subject = mb_encode_mimeheader(mb_convert_encoding("登録URL", "JIS", "auto"), "JIS"); $body = "下記のURLをクリックして登録を行ってください http://hoge.com/regist.php?". $MailBody . "". session_name()."=". htmlspecialchars(session_id()); とメールを自分のHPに送る際に本文にデータがあるのですが、そのデータを取り出し登録URLに付加し、情報を維持させたいのですが、上記のソースですと$MailBodyに情報がはいっていません。ベテランさん!ご指導、ご教授お願い致します

    • ベストアンサー
    • PHP

専門家に質問してみよう