. */ class Receive_Mail { var $message; var $from; var $subject; var $date; var $boundary; var $spamscore; var $state; var $msgcount; var $SPAMHEADER="X-Spam-Level"; function Receive_Mail() { $this->message=array(); // Whole message, split in messages (first dimension) and parts (second dimension, see below): // ['header'] // ['ctype'] Content-Type: // ['encoding'] Content-Transfer-Encoding: // ['body'] $this->fullmessage=""; // Whole message $this->from=""; // From of the mail $this->subject=""; // Subject of the mail $this->date=""; // Date of the mail $this->boundary=""; // Boundary in case message consists of several parts $this->spamscore=""; // Spam score $this->state=0; // 0: reading main header; 1: reading main body; // 2: reading message header; 3: reading message body $this->msgcount=0; // Counter to keep track in which message we are reading } function receive($file) { $curheader = ""; $this->message[$this->msgcount] = array(); $this->message[$this->msgcount]['header'] = ""; $this->message[$this->msgcount]['body'] = ""; $this->message[$this->msgcount]['ctype'] = ""; if( $fp = fopen($file, "r") ) { while( $line=fgets($fp, 1024) ) { $this->fullmessage.=$line; switch($this->state) { case 0: // main header case 2: // message part header if( strlen(trim($line))==0 ) { // End of header $this->header_add($curheader); $this->state++; $curheader=""; }else { // Process header $this->message[$this->msgcount]['header'].=$line; if( ltrim($line)==$line ) { // No whitespace at beginning of line, start of new header item $this->header_add($curheader); $curheader=trim($line); }else { // Whitespace at beginning, this line should be added to the previous $curheader.=" ".trim($line); } } break; case 1: // main body case 3: // message part body if( strlen($this->boundary)==0 OR strpos($line, $this->boundary)===false ) { $this->message[$this->msgcount]['body'].=$line; }else { // Message boundary $this->state++; if( $this->state>3 ) $this->state=2; // Prevent too high state $this->msgcount++; // Move to next part $this->message[$this->msgcount] = array(); $this->message[$this->msgcount]['header'] = ""; $this->message[$this->msgcount]['body'] = ""; $this->message[$this->msgcount]['ctype'] = ""; } break; default: //Ehm? } } fclose($fp); return true; }else { // Could not open $file return false; } } function header_add($header) { if( preg_match("/(^Content-Type: )(.*?)$/i", $header, $match) ) { $this->message[$this->msgcount]['ctype'] = $match[2]; if( $this->state==0 AND preg_match("/(boundary=\")(.*?)(\")/i", $this->message[$this->msgcount]['ctype'], $match) ) { $this->boundary = trim($match[2]); } } if( preg_match("/(^Content-Transfer-Encoding: )(.*?)$/i", $header, $match) ) { $this->message[$this->msgcount]['encoding'] = $match[2]; } if( preg_match("/(^From: )(.*?)$/i", $header, $match) ) { $this->from = $match[2]; } if( preg_match("/(^Subject: )(.*?)$/i", $header, $match) ) { $this->subject = $match[2]; } if( preg_match("/(^Date: )(.*?)$/i", $header, $match) ) { $this->date = $match[2]; } if( preg_match("/(^".$this->SPAMHEADER.": )(.*?)$/i", $header, $match) ) { $this->spamscore = strlen($match[2]); } } function get_message() { return $this->message; } function get_from() { return $this->from; } function get_subject() { return $this->subject; } function get_date() { return $this->date; } function get_spamscore() { return $this->spamscore; } function get_boundary() { return $this->boundary; } function get_fullmessage() { return $this->fullmessage; } function new_boundary() { return date('YmdHis') . '_' . mt_rand(10000, 99999) . "/" . (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME'] : "localhost"); } } ?>