<?php
	class MIMEMail {
		var $_parts;
		var $xmailer;
		var $_mimesep;
		var $_mimeheader;
		var $from_name;
		var $from_email;
		var $signatur;
		var $cc;
		var $bcc;
		var $_i;
		var $subject;
		var $to;
		
		function MIMEMail() {
			$this->xmailer = "KOOBI (".$HTTP_SERVER_VARS['HTTP_HOST'].")";
			$this->_mimesep = "--Boundary-=_" . md5(uniqid(time()));
			$this->from_name = "";
			$this->from_email = "";
			$this->cc = "";
			$this->bcc = "";
			$this->_mimeheader = "";
			$this->_i = 0;
			$this->_parts = array();
			$this->subject = "";
			$this->to = "";
		}
		
		function Headers () {
			global $bm_prefs;
			
			$mimeheader = "From: \"" . $this->from_name . "\" <" . $this->from_email . ">\n";
			if (!($this->cc=="") or !isset($this->cc)) {
				$mimeheader .= "Cc: " . $this->cc . "\n";
			}
			if (!($this->bcc=="") or !isset($this->bcc)) {
				$mimeheader .= "Bcc: " . $this->bcc . "\n";
			}
			$mimeheader .= "Return-Path: <" . $this->from_email . ">\n";
			$mimeheader .= "MIME-Version: 1.0\n";
			$mimeheader .= "Content-Type: multipart/mixed;boundary=\"" . $this->_mimesep . "\"\n";
			$mimeheader .= "X-Mailer: " . $this->xmailer . ""."\n";
			$this->_mimeheader = $mimeheader;
		}
		
		function AddTextPart ($html,$text) {
			if ($html==true) {
				$delim = "html";
				$brake = "";
			} else {
				$delim = "plain";
				$brake = "\n\n";
			}
			$newmessage = "--" . $this->_mimesep . "\n";
			$newmessage .= "Content-Type: text/".$delim." ; CHARSET=gb2312\n\n";
			$newmessage .= str_replace("\n", $brake, $text);
			$this->_parts[$this->_i] = $newmessage;
			
			$this->_i++;
		}
		
		function AddAttachment ($content,$filename,$typ,$af="ATTACHMENT") {
			$newmessage = "\n\n--" . $this->_mimesep . "\n";
			$newmessage .= "Content-Type: " . $typ . "; name=\"" . $filename . "\"\n";
			$newmessage .= "Content-Transfer-Encoding: base64\n";
			$newmessage .= "Content-Disposition: $af; filename=\"" . $filename . "\"\n\n";
			$newmessage .= base64_encode($content);
			$this->_parts[$this->_i] = $newmessage;
			$this->_i++;
		}
		
		function Finish() {
			$newmessage = "\n--" . $this->_mimesep . "--\n\n";
			$this->_parts[$this->_i] = $newmessage;
			$this->_i++;
		}
		
		function Send() {
			$body = $this->_mimeheader;
			for ($i=0;$i<=count($this->_parts);$i++) {
				$body .= $this->_parts[$i];
			}
			
			return (mail($this->to, $this->subject, "", $body));
		}
	}
?>