Mail_mimeDecodeでメール本文取得

このQ&Aのポイント
  • <form>タグ内のメール本文を取得する方法を説明します。
  • Mail_mimeDecodeを使用してメールのデコードを行い、メール本文を取得する方法を解説します。
  • 送信元から受け取ったメールデータを解析し、メール本文を取得する手順について説明します。
回答を見る
  • ベストアンサー

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
  • 回答数1
  • ありがとう数0

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

  • ベストアンサー
回答No.1

こんばんは。 何ができてて、何ができてないんですかね? 一応、$friendの値はメール本文として返ってきて、GETのパラメータに引き渡しているように見えますが・・・? (パラメータの渡し方はおかしそうですが・・・)

関連するQ&A

  • 自動返信での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
  • PEAR の Mail_mimeDecodeで

    お世話になります 下記スクリプトにてメールを受信してDBに格納しようと思っています DBに格納後、メールサーバからメールを消したいのですが、メールボックスを空にするためにの処理を教えて頂けないでしょうか? 宜しくお願いいたします。 /////////////////////////////////// //PEARのパスを設定 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"; $mail_data = Mail_mimeDecode::decode($params); #-- From $FromAddress = $mail_data->headers['from']; $FromAddress = addslashes($FromAddress); //エスケープ処理 $FromAddress = str_replace('"','',$FromAddress); #-- 署名つきの場合 preg_match("/<.*>/",$FromAddress,$str); if($str[0]!=""){ $str=substr($str[0],1,strlen($str[0])-2); $FromAddress=$str; } #-- Subject フィールドの取得 $Subject = $mail_data->headers['subject']; $Subject = mb_convert_encoding($Subject,"UTF-8","JIS"); #-- 本文の取得 switch(strtolower($mail_data->ctype_primary)){ case "text": //テキストメール $MailBody = $mail_data->body; $MailBody = mb_convert_encoding($MailBody,"UTF-8","JIS"); break; case "multipart": //マルチパート(添付ファイル(画像前提)付) foreach($mail_data->parts as $part){ switch(strtolower($part->ctype_primary)){ case "text": $MailBody = $part->body; $MailBody = mb_convert_encoding($MailBody,"UTF-8","JIS"); break; case "image": $type = strtolower($part->ctype_secondary); // jpg,gif,png以外の画像形式は受け付けない if($type != "jpeg" and $type != "jpg" and $type != "gif" and $type != "png"){ continue; } // ファイルを保存 /* $fp = fopen("/tmp/". time() . $type, "w"); $length = strlen($part->body); fwrite($fp,$part->body,$length); fclose($fp); */ break; } } break; default: $MailBody=""; }

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

    お世話になります 下記スクリプトにてメールをPHPで受け取ることは出来ているのですが、 画像データの処理について教えて頂けないでしょうか? $FromAddress,$Subject,$MailBodyとそれぞれの変数で取得したデータはデータベースに格納出来ています。 しかし、添付されている画像データを取り出してデータベースに格納する所でつまずいています。 // ファイルを保存 $fp = fopen("/tmp/". time() . $type, "w"); $length = strlen($part->body); fwrite($fp,$part->body,$length); fclose($fp); 上記部分で画像データは取得出来ていると思われるのですが、データベースに格納出来るデータに変更するための処理をアドバイス願います。 宜しくお願いします ちなみにPHP4です #PHPファイル########################### /////////////////////////////////// //PEARのパスを設定 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"; $mail_data = Mail_mimeDecode::decode($params); #-- From $FromAddress = $mail_data->headers['from']; $FromAddress = addslashes($FromAddress); //エスケープ処理 $FromAddress = str_replace('"','',$FromAddress); #-- 署名つきの場合 preg_match("/<.*>/",$FromAddress,$str); if($str[0]!=""){ $str=substr($str[0],1,strlen($str[0])-2); $FromAddress=$str; } #-- Subject フィールドの取得 $Subject = $mail_data->headers['subject']; $Subject = mb_convert_encoding($Subject,"UTF-8","JIS"); #-- 本文の取得 switch(strtolower($mail_data->ctype_primary)){ case "text": //テキストメール $MailBody = $mail_data->body; $MailBody = mb_convert_encoding($MailBody,"UTF-8","JIS"); break; case "multipart": //マルチパート(添付ファイル(画像前提)付) foreach($mail_data->parts as $part){ switch(strtolower($part->ctype_primary)){ case "text": $MailBody = $part->body; $MailBody = mb_convert_encoding($MailBody,"UTF-8","JIS"); break; case "image": $type = strtolower($part->ctype_secondary); // jpg,gif,png以外の画像形式は受け付けない if($type != "jpeg" and $type != "jpg" and $type != "gif" and $type != "png"){ continue; } // ファイルを保存 $fp = fopen("/tmp/". time() . $type, "w"); $length = strlen($part->body); fwrite($fp,$part->body,$length); fclose($fp); break; } } break; default: $MailBody=""; }

    • ベストアンサー
    • 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
  • 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
  • 携帯からの空メールの処理

    こんにちは。今、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
  • 空メールからの標準入力が受け取れません。

    よろしくお願いいたします。 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
  • 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
  • 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

専門家に質問してみよう