Mail_Mime、Mail_mimeDecodeあたりが必要なようだ
Mail_MimeとMail_mimeDecodeをインストールしてみる
# pear install --alldeps Mail_Mime # pear install --alldeps Mail_MimeDecode
# pear list Installed packages, channel pear.php.net: ========================================= Package Version State Archive_Tar 1.3.7 stable Archive_Zip 0.1.1 beta Console_Getopt 1.2.3 stable HTTP 1.4.1 stable HTTP_Download 1.1.3 stable HTTP_Header 1.2.0 stable MIME_Type 1.2.0 stable Mail_Mime 1.8.0 stable Mail_mimeDecode 1.5.1 stable PEAR 1.9.1 stable Structures_Graph 1.0.3 stable System_Command 1.0.6 stable XML_Util 1.2.1 stable
添付ファイルを受信して、指定フォルダに保存し、返信する
#!/usr/local/bin/php -q
<?php
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";
$structure = Mail_mimeDecode::decode($params);
//送信者のメールアドレス
$to_mail = $structure->headers['from'];
$to_mail = addslashes($to_mail);
$to_mail = str_replace('"','',$to_mail);
$to_mail = preg_replace('/(^.*<|>$)/', '', $to_mail);
// 件名を取得
$mail_subject = $structure->headers['subject'];
switch(strtolower($structure->ctype_primary)){
case "text": // シングルパート(テキストのみ)
$mail_body = $structure->body;
break;
case "multipart":
$kk=0 ;
foreach($structure->parts as $part){
//添付ファイル名取得
$filename = $structure->parts[$kk]->d_parameters['filename'];
$kk++ ;
// とりあえず保存
$fp = fopen("/home/okada/public_html/pear_test/temp2/".$filename,"w" );
$length = strlen( $part->body );
fwrite( $fp, $part->body, $length );
fclose( $fp );
$attached_file = $attached_file .$filename."\n" ;
// $mail_body = $structure->body;
}
break;
default:
$mail_body = "";
$attached_file = "";
}
// 受信したサブジェクトとファイル名を返信
$body = ""
.$mail_subject."について\n\n"
."内容は\n"
.$mail_body."\n\n"
."添付ファイルを保存しました。\n"
.$attached_file."\n"
."---------------------------------------------\n"
."test@Okada \n"
."送りもとは".$to_mail."だよ\n"
."\n" ;
$mailfrom="From:okada@abc-u.net.jp";
$mail_title = "メール自動返信" ;
mb_language("Ja") ;
mb_internal_encoding("EUC-JP") ;
// mb_internal_encoding("UTF-8") ;
$to_mail="okada@abc-u.net.jp";
$rc=mb_send_mail($to_mail,$mail_title,$body,$mailfrom);
?>