发送mime邮件的优化类

Class sendMailPro {
    var 
$to       = array();    // 收信人
    
var $from     "";         // 发信人
    
var $headers  "";         // 头信息
    
var $subject  "";         // 主题
    
var $body     "";         // 内容
    
var $mime     "";         // Mime信息
    
var $parts    = array();    // 附件
    
var $filec;                 // 文件数目
    
var $boundary "";         // 分隔符
    
var $type     "text/plain";
    function 
sendMailPro($to$from ""$subject "",$body ""$headers "") {
        if(
is_array($to)) {
            
$this->to    $to;
        } else {
            
$this->to[0] = $to;
        }
        
$this->from      $from;
        
$this->subject   $subject;
        
$this->body      $body;
        
$this->headers   $headers;

        
$this->boundary  Chop("b".md5(uniqid(time())));
    }
    function 
addreciever($to) {
        
$this->to[]      = $to;
        return 
1;
    }
    function 
addAttachment($filecontent$filename ""$filetype "application/octet-stream") {
        
$this->filec++;
        if(
$filename=="") {
            
$filename "attachment_".$this->filec;
        }
        
$this->parts[] = array(
            
"type" => $filetype,
            
"content" => $filecontent,
            
"name" => $filename
        
);
        return 
1;
    }
    function 
addFile($filename) {
        
$filecontent fread(fopen($filename,"r"), filesize($filename));
        
$filetype    "application/octet-stream";
        
$this->addAttachment($filecontent$filename$filetype);
    }
    function 
buildBody() {
        
$body =$this->body;
        
$boundary $this->boundary;
        
$bodyr "--".$boundary;
        
$bodyr "\nContent-Type: ".$this->type."; charset=\"GB2312\"\nContent-Transfer-Encoding: quoted-printable\n\n";
        
$bodyr.= $body."\n\n";
        
$bodyr.= "--".$boundary;
        return 
$bodyr;
    }
    function 
buildContent($part) {
        
$content  $part["content"];
        
$content  chunk_split(base64_encode($content));
        
$encoding "base64";
        
$return   "Content-Type: ".$part["type"].";";
        
$return  .= " name = \"".$part["name"]."\"\n";
        
$return  .= "Content-Transfer-Encoding: $encoding\n";
        
$return  .= "Content-Disposition: attachment; filename=\"".$part["name"]."\"\n\n";
        
$return  .= "$content\n";
        return 
$return;
    }
    function 
buildMail() {
        
$boundary $this->boundary;
        
$multipart "Content-Type: multipart/mixed; boundary = \"$boundary\"\n\nThis is a MIME encoded message send by sendMailPro class(zlib).\n\n--$boundary";
        
$multipart.= $this->buildBody();
        
$attachmentCount count($this->parts);
        if(
$attachmentCount==0) {
            return 
$multipart;
        }
        for(
$i $attachmentCount-1;$i>=0;$i--){
            
$multipart .= "\n".$this->buildContent($this->parts[$i])."--$boundary";
        }
        
$multipart chop($multipart);
        
$multipart.= "--\n";
        return 
$multipart;
    }
    function 
isHtml($html 'yes') {
        if(
$html) {
            
$this->type "text/html";
        }
        return 
1;
    }
    function 
send() {
        if(
$this->from<>'') {
            
$this->mime.= "From: ".$this->from."\n";
        }
        
$this->mime.= "MIME-Version: 1.0\n";
        
$this->mime.= $this->buildMail();
        
$to_count count($this->to);
        for(
$i 0;$i<$to_count;$i++) {
            
mail($this->to[$i], $this->subject""$this->mime);
        }
        return 
1;
    }
    function 
attachmentCount() {
        return 
$this->filec;
    }
}
?>

Example:
------------------------------------------------------------------------------------
You can send a non-HTML mail like this:
<?
$mail 
= new sendMailPro($to,$from,$subject,$body);
$mail->send();
?>

You can send a HTML mail like this:
<?
$mail 
= new sendMailPro($to,$from,$subject,$body);
$mail->isHtml();
$mail->send();
?>

You can send a mail with Attachment like this:
<?
$mail 
= new sendMailPro($to,$from,$subject,$body);
$mail->isHtml();
$mail->addFile($attachmentpath);
$mail->send();
?>