2009-02-01 受信メールのPHPを起動から添付ファイルの保存まで(さくらインタ
■[PHP][PEAR][MAIL][SAKURA]受信メールでPHPの起動から添付ファイルの保存まで(さくらインターネット)
さくらインターネットのレンタルサーバ環境にて、受信メールによるPHPプログラムの
起動に成功しましたので以下にメモします。
http://www.cpa-lab.com/tech/0143
を参考にsakuraエディターでテキストファイルを編集してftpでアップした。
このようにエラー発生
----- The following addresses had permanent fatal errors ----- <post@xxxx.com> (reason: 127) ----- Transcript of session follows ----- /home/xxxx/www/work/mail/example.php: not found 554 5.3.0 unknown mailer error 127
%ls example.php %./example.php ./example.php: Command not found.
http://www.y-tti.com/blog/2007/06/mailfilter.php
ここを見て”保存設定 EUC LF(改行)としたら成功しました。
コマンドラインも成功
%ls example.php %./example.php X-Powered-By: PHP/5.2.8 Content-type: text/html
example.php
#!/usr/local/bin/php <?php $content = null; $fp=fopen("php://stdin",'r') or die('File Open Error'); $fpw=fopen("mail.txt",'w'); while( !feof($fp) ){ $content .= fgets( $fp ,1024); } fputs($fpw,$content); ?>
実行結果 mail.txt が出力されました。
ありがとうございます。
添付ファイルの保存は、ここの class ReceiptMailDecoder を使うことにする。
http://d.hatena.ne.jp/ya--mada/20080415/1208318475
PEARのインストールが必要なのでここを見ながらインストール。
http://masha.maakikaku.jp/2008/05/gopearpear.php
Go-PEARは便利でした。
一番下にスクロールするところが判り難い。
Start Web Frontend of the PEAR Installer >> をクリックする必要がある。
サンプルをmail.txtを読み込んで処理するように修正。
<?php require_once('ReceiptMailDecoder.class.php'); $fp = fopen("mail.txt","r"); $body = ""; while (!feof($fp)){ $body .= fgets($fp,1024); } $decoder =& new ReceiptMailDecoder($body); // To:アドレスのみを取得する $toAddr = $decoder->getToAddr(); print "$toAddr <br>"; // To:ヘッダの値を取得する $fromAddr = $decoder->getFromAddr(); print "$fromAddr <br>"; // from:ヘッダの値を取得する $toString = $decoder->getDecodedHeader( 'to' ); // Subject:ヘッダの値を取得する $subject = $decoder->getDecodedHeader( 'subject' ); print "$subject <br>"; // text/planなメール本文を取得する $body = mb_convert_encoding($decoder->body['text'],"eucjp-win","jis"); print "$body <br>"; // text/htmlなメール本文を取得する $body = mb_convert_encoding($decoder->body['html'],"eucjp-win","jis"); print "$body <br>"; // マルチパートのデータを取得する if ( $decoder->isMultipart() ) { $tempFiles = array(); $num_of_attaches = $decoder->getNumOfAttach(); for ( $i=0 ; $i < $num_of_attaches ; ++$i ) { /* * ファイルを一時ディレクトリ _TEMP_ATTACH_FILE_DIR_ に保存する * 一時ファイルには tempnam()を使用する * この部分は使用に合わせて変更して下せい */ $fpath = tempnam("./tmp", "todoattach_" ); print "fpath=$fpath <br>\n"; // $fpath = tempnam( _TEMP_ATTACH_FILE_DIR_, "todoattach_" ); if ( $decoder->saveAttachFile( $i, $fpath ) ) { $tempFiles["$fpath"] = $decoder->attachments[$i]['mime_type']; } } } ?>
class ReceiptMailDecoder のサンプルは、テンポラリーのファイル名を使うようになっているので
受信メールよりファイル名を取得してそのファイル名で保存するように修正した。
$filename = $this->attachments[$index]['file_name'];
でファイル名が取得できる。
function saveAttachFile ( $index, $str_path ) {
if ( !file_exists($str_path) ) {
if ( !is_writable(dirname($str_path)) ) {
return false;
}
}
else {
if ( !is_writable($str_path) ) {
return false;
}
}
if ( !isset($this->attachments[$index]) ) {
return false;
}
$filename = $this->attachments[$index]['file_name'];
print "filename = $filename <br>\n";
// if ( $fp=fopen($str_path, "wb") ) {
if ( $fp=fopen($filename, "wb") ) {
fwrite($fp, $this->attachments[$index]['binary'] );
fclose($fp);
return true;
}
return false;
}
ここまでで、受信メールでPHPの起動から添付ファイルの保存までのプログラムが出来た。
私のアプリケーションの場合受信ファイル名が重要なのでこのあたりの調整が必要。
次に2つのPHPをくっつけたらうまく動かない。
2つのPHPをくっつけて実行したらうまくいかない。
telnetにて
% %php -i >php.htm %
を実行してパスを確認したら
Loaded Configuration File が /usr/local/php-5.2.8/lib/php.ini
になっていた。
ソースにini_setを追加してうまくいきました。
ini_set('include_path', '/home/xxxx/www/work/pear/php');
require_once('ReceiptMailDecoder.class.php');
先人たちの記事にてここまでくることが出来ました。
感謝します。
#!/usr/local/bin/php <?php print "php start!!\n"; /*標準出力をmail.txtに保存*/ $content = null; $fp=fopen("php://stdin",'r') or die('File Open Error'); $fpw=fopen("mail.txt",'w'); $fplog=fopen("log.txt",'a'); //fputs($fplog, "log open \n"); while( !feof($fp) ){ $content .= fgets( $fp ,1024); } fputs($fpw, $content); //fputs($fplog, "mail.txt write \n"); /**/ ini_set('include_path', '/home/anakureon/www/work/pear/php'); require_once('ReceiptMailDecoder.class.php'); print "open mail.txt\n"; $fp = fopen("mail.txt","r"); $body = ""; while (!feof($fp)){ $body .= fgets($fp,1024); } $decoder =& new ReceiptMailDecoder($body); // To:アドレスのみを取得する $toAddr = $decoder->getToAddr(); print "$toAddr <br>"; // To:ヘッダの値を取得する $fromAddr = $decoder->getFromAddr(); print "$fromAddr <br>"; // from:ヘッダの値を取得する $toString = $decoder->getDecodedHeader( 'to' ); // Subject:ヘッダの値を取得する $subject = $decoder->getDecodedHeader( 'subject' ); print "$subject <br>"; // text/planなメール本文を取得する $body = mb_convert_encoding($decoder->body['text'],"eucjp-win","jis"); print "$body <br>"; // text/htmlなメール本文を取得する $body = mb_convert_encoding($decoder->body['html'],"eucjp-win","jis"); print "$body <br>"; // マルチパートのデータを取得する if ( $decoder->isMultipart() ) { $tempFiles = array(); $num_of_attaches = $decoder->getNumOfAttach(); for ( $i=0 ; $i < $num_of_attaches ; ++$i ) { /* * ファイルを一時ディレクトリ _TEMP_ATTACH_FILE_DIR_ に保存する * 一時ファイルには tempnam()を使用する * この部分は使用に合わせて変更して下せい */ $fpath = tempnam("./tmp", "todoattach_" ); print "fpath=$fpath <br>\n"; // $fpath = tempnam( _TEMP_ATTACH_FILE_DIR_, "todoattach_" ); if ( $decoder->saveAttachFile( $i, $fpath ) ) { $tempFiles["$fpath"] = $decoder->attachments[$i]['mime_type']; } } } /**/ $created = date('Y-m-d H:i:s'); fputs($fplog, "$created,$fromAddr,\n"); ?>
- 230 http://www.google.co.jp/search?q=php+メール本文 取得&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja-JP-mac:official&client=firefox-a
- 224 http://www.google.co.jp/search?hl=ja&q=php+メール取得+添付&btnG=Google+検索&meta=lr=&aq=f
- 172 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGLR_ja___JP210&q=受信メール php
- 172 http://www.google.co.jp/url?sa=t&rct=j&q=php メール 添付 受信&source=web&cd=1&ved=0CB4QFjAA&url=http://d.hatena.ne.jp/mk18/20090201/p1&ei=jBGETuzdLIzKiALrmPi_DA&usg=AFQjCNGyQY6v8SQJUBj52Uo
- 151 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=0wu&q=php+while+open&btnG=検索&lr=lang_ja
- 108 http://www.google.co.jp/url?sa=t&source=web&cd=1&ved=0CBkQFjAA&url=http://d.hatena.ne.jp/mk18/20090201/p1&rct=j&q=php メール受信 添付ファイル&ei=xxqxTfvnN4i
- 104 http://www.google.co.jp/search?hl=ja&source=hp&q=php+メール+受信+添付ファイル&lr=&aq=1&oq=php+メール 受信
- 86 http://search.yahoo.co.jp/search?p=php+メール受信&search_x=1&tid=top_ga1_sa&ei=UTF-8&pstart=1&fr=top_ga1_sa&b=11&qrw=0
- 86 http://www.y-tti.com/blog/2007/06/mailfilter.php
- 78 http://www.google.co.jp/search?num=20&hl=ja&client=firefox-a&rls=org.mozilla:ja:official&q="メールで起動" レンタル&btnG=検索&aq=f&aqi=&aql=&oq=&gs_rfai=

