ãã«ããã¼ããªã¡ã¼ã«ã解æãã PEAR::Mail::mimeDecode ãã©ããããã¯ã©ã¹
お前の予定!ã«ã¡ã¼ã«ãéä¿¡ããã¨ããã®ã¡ã¼ã«ãäºå®ã¨ãã¦ç»é²ã§ãã¾ãããã®æ©è½ãå®è£
ããããã«PEAR::Mail_mimeDecodeã使ã£ã¦ããã®ã§ãããå®éã«ä½¿ãã¨ãã«ã¯ã¡ãã£ã¨æéã¨ãããç
©éã«ãªã£ã¦ãã¾ãã®ã§ãMail_mimeDecodeãã©ããããã¯ã©ã¹ãä½ãã¾ãããæ·»ä»ãã¡ã¤ã«ä»ãã®ã¡ã¼ã«ãããªãã·ã³ãã«ã«å¦çã§ããã¨æãã¾ãã
ReceiptMailDecoderã¯ã©ã¹ã§ãã
(èªå°)ã¡ã¼ã«åä¿¡ãããã¯ããæ¹æ³
class ReceiptMailDecoder
PEAR::Mail_mimeDecodeããã£ã¨ã·ã³ãã«ã«ä½¿ããããã«ã©ããããã¯ã©ã¹ã§ãã
æºå¸¯ã®åã¡ã¼ã«å¯¾å¿ãããã¨ãã«ä½¿ãã¨ä¾¿å©ã§ãããã¡ããé常ã®PCã¡ã¼ã«ã§ã対å¿ã§ãã¾ãã
- 使ãæ¹
<?php require_once('ReceiptMailDecoder.class.php'); $decoder =& new ReceiptMailDecoder($raw_mail); // To:ã¢ãã¬ã¹ã®ã¿ãåå¾ãã $toAddr = $decoder->getToAddr(); // To:ãããã®å¤ãåå¾ãã $toString = $decoder->getDecodedHeader( 'to' ); // Subject:ãããã®å¤ãåå¾ãã $subject = $decoder->getDecodedHeader( 'subject' ); // text/planãªã¡ã¼ã«æ¬æãåå¾ãã $body = mb_convert_encoding($decoder->body['text'],"eucjp-win","jis"); // text/htmlãªã¡ã¼ã«æ¬æãåå¾ãã $body = mb_convert_encoding($decoder->body['html'],"eucjp-win","jis"); // ãã«ããã¼ãã®ãã¼ã¿ãåå¾ãã 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( _TEMP_ATTACH_FILE_DIR_, "todoattach_" ); if ( $decoder->saveAttachFile( $i, $fpath ) ) { $tempFiles["$fpath"] = $decoder->attachments[$i]['mime_type']; } } } ?>
- ããããã£
- ã¡ã½ãã
- ReceiptMailDecoder ReceiptMailDecoder ( &$raw_mail, string $raw_mail)
- string extractionEmails (string $raw_string)
- string getDecodedHeader (string $header_name)
- string getFromAddr ()
- string getHeaderAddresses (string $header_name)
- int getNumOfAttach ()
- string getRawHeader (string $header_name)
- string getToAddr ()
- bool isMultiPart ()
- bool saveAttachFile (int $index, string $str_path)
<?php /* -*-java-*- */ require_once('Mail/mimeDecode.php'); /** * åä¿¡ã¡ã¼ã«ã®ãããã®å¤ããã«ããã¼ãã®æ¬æããã¡ã¤ã«ãåå¾ããã¯ã©ã¹ * * @author [email protected] * @create 2008-03-11 * @package ya--mada */ class ReceiptMailDecoder { /** * æ¬æ * * @var array array('text'=>{text}, 'html'=>{html} ) */ var $body = array( 'text'=> null , 'html'=> null ); /** * æ·»ä»ãã¡ã¤ã« * * @var array array[] = array('mime_type'=>{mime_type}, * 'file_name'=>{file_name}, * 'binary'=>{binary} ) */ var $attachments = array(); /** * Mail_mimeDecode ãªãã¸ã§ã¯ã * * @var object */ var $_decoder; /** * constractor ReceiptMailDecoder class. * * @param string $raw_mail åä¿¡ãããã®ã¾ã¾ã¡ã¼ã«æåå */ function ReceiptMailDecoder ( &$raw_mail ) { if ( !is_null( $raw_mail ) ) { $this->_decode( $raw_mail ); } } /** * ãã®ã¯ã©ã¹ã使ç¨ããéã®åæå * * @access public * @param array */ function init() { } /** * çã¡ã¼ã«ããã³ã¼ããã¦ããããã£ã«ä»£å ¥ãã * * @access public * @param string $raw_mail åä¿¡ãããã®ã¾ã¾ã¡ã¼ã«æåå */ function _decode ( &$raw_mail ) { if ( is_null($raw_mail) ) { return false; } $params = array(); $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; // $params['input'] = $raw_mail."\n"; /* * Mail_mime::Decode ãã¤ãã£ã¦å解解æãã * ãã«ããã¼ãã®å ´åã¯ãæ¬æã¨æ·»ä»ã«åãã¾ãã */ $this->_decoder =& new Mail_mimeDecode( $raw_mail."\n" ); $this->_decoder = $this->_decoder->decode($params); $this->_decodeMultiPart($this->_decoder); } /** * æå®ãããã®åå¾ * * @access public * @param string $header_name * @return string */ function getRawHeader ( $header_name ) { return isset($this->_decoder->headers["$header_name"]) ? $this->_decoder->headers["$header_name"] : null; } /** * ããããmimeã¨ã³ã³ã¼ãããã¦ããå ´åã¯ãã³ã¼ããã¦åå¾ããã * * @todo æºå¸¯çµµæåã«ã¯å¯¾å¿ãã¦ããªãã * @access public * @param string $header_name * @return string */ function getDecodedHeader( $header_name ) { return mb_decode_mimeheader($this->getRawHeader( $header_name )); } /** * æå®ãããå ã®E-mailã¢ãã¬ã¹ã ããæãåºãã¦è¿ã * * @access public * @param string $header_name * @return string * @see extractionEmails() */ function getHeaderAddresses ( $header_name ) { return $this->extractionEmails($this->getRawHeader( $header_name )); } /** * STATIC * æååã®ä¸ããemailã¢ãã¬ã¹ã£ã½ããã®ã ããæ½åºãã¦è¿ãã¾ãã * emailã¢ãã¬ã¹ã£ã½ããã®ã®æ£è¦è¡¨ç¾ãããããã * see. http://red.ribbon.to/~php/memo_003.php * * @access public * @param string $raw_string * @return string $mail_addresses ã¡ã¼ã«ã¢ãã¬ã¹ã£ã½ããã®ãè¤æ°ããã°,(ã«ã³ã)åºåã㧠*/ function extractionEmails( $raw_string ) { /* * æ§emailã¢ãã¬ã¹ã£ã½ãæ£è¦è¡¨ç¾ * see. http://www.tt.rim.or.jp/~canada/comp/cgi/tech/mailaddrmatch/ * $email_regex_pattern = "/[\x01-\x7F]+@(([-a-z0-9]+\.)*[a-z]+|\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])/"; */ /* * æ°emailã¢ãã¬ã¹ã£ã½ãæ£è¦è¡¨ç¾ * see. http://red.ribbon.to/~php/memo_003.php */ $email_regex_pattern = '/(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|"[^\\\\\x80-\xff\n\015"]*(?:\\\\[^\x80-\xff][^\\\\\x80-\xff\n\015"]*)*")(?:\.(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|"[^\\\\\x80-\xff\n\015"]*(?:\\\\[^\x80-\xff][^\\\\\x80-\xff\n\015"]*)*"))*@(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\\\x80-\xff\n\015\[\]]|\\\\[^\x80-\xff])*\])(?:\.(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\\\x80-\xff\n\015\[\]]|\\\\[^\x80-\xff])*\]))*/'; if ( preg_match_all( $email_regex_pattern, $raw_string, $matches, PREG_PATTERN_ORDER ) ) { if ( isset($matches[0]) ) { return implode( ",", $matches[0] ); } } return null; } /** * ãã³ã¼ãããæ¬æã®åå¾ * * $this->body['text']; // ããã¹ãå½¢å¼ã®æ¬æ * $this->body['html']; // htmlå½¢å¼ã®æ¬æ */ /** * æ·»ä»ãã¡ã¤ã«ã®åå¾ * * $this->attachments[$i]['mime_type']; // MimeType * $this->attachments[$i]['file_name']; // ãã¡ã¤ã«å * $this->attachments[$i]['binary']; // ãã¡ã¤ã«æ¬ä½ */ /** * ã¡ã¼ã«æ¬æé¨åã®å¦ç * ãã«ããã¼ãã®å ´åã¯ããã®å¦çããã¾ããã * * @access private */ function _decodeMultiPart(&$decoder) { // ãã«ããã¼ãã®å ´å ãããããpartsé åå ã«åé ç½®ããã¦ããã®ã§ // å帰çã«å¦çãããã if ( !empty($decoder->parts) ) { foreach ( $decoder->parts as $part ) { $this->_decodeMultiPart($part); } } else { if ( !empty($decoder->body) ) { // æ¬æ (text or html ) if ( 'text' === strToLower($decoder->ctype_primary) ) { if ( 'plain' === strToLower($decoder->ctype_secondary) ) { $this->body['text'] =& $decoder->body; } elseif ( 'html' === strToLower($decoder->ctype_secondary) ) { $this->body['html'] =& $decoder->body; } // ãã®ä»ã®textç³»ãã«ããã¼ã else { $this->attachments[] = array( 'mime_type'=>$decoder->ctype_primary.'/'.$decoder->ctype_secondary, 'file_name'=>$decoder->ctype_parameters['name'], 'binary'=>&$decoder->body ); } } // ãã®ä» else { $this->attachments[] = array( 'mime_type'=>$decoder->ctype_primary.'/'.$decoder->ctype_secondary, 'file_name'=>$decoder->ctype_parameters['name'], 'binary'=>&$decoder->body ); } } } } /** * ã¡ã¼ã«ãæ·»ä»ãã¡ã¤ã«ã¤ãã調ã¹ã * * @access private * @return bool æ·»ä»ä»ããªã true ç¡ããã° false ãè¿ã */ function isMultiPart() { return (count($this->attachments) > 0) ? true : false; } /** * æ·»ä»ãã¡ã¤ã«ã®æ°ãæ°ãã * * @access private * @return int */ function getNumOfAttach() { return count($this->attachments); } /** * Toãããããã¢ãã¬ã¹ã®ã¿ãåå¾ãã * * @access public * @return string toã¢ãã¬ã¹ è¤æ°ããã°ã«ã³ãåºåãã§è¿ã * @see getHeaderAddresses(), extractionEmails() */ function getToAddr() { return $this->getHeaderAddresses( 'to' ); } /** * Fromãããããã¢ãã¬ã¹ã®ã¿ãåå¾ãã * * @access public * @return string Fromã¢ãã¬ã¹ è¤æ°ããã°ã«ã³ãåºåãã§è¿ã * @see getHeaderAddresses(), extractionEmails() */ function getFromAddr () { return $this->getHeaderAddresses( 'from' ); } /** * æ·»ä»ãã¡ã¤ã«ãä¿åãã * * @todo Mail_mimeDecode ã¯ãã«ããã¼ãã®base64decodeã®é¢åã¯ã¿ãªãï¼ * @access public * @param int * @param string $str_path * @return bool æåãªã true 失æãªã false ãè¿ã */ 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; } if ( $fp=fopen($str_path, "wb") ) { fwrite($fp, $this->attachments[$index]['binary'] ); fclose($fp); return true; } return false; } }