<?
class sendMail {

	var $charSet     = "ks_c_5601-1987"; 
	var $defaultHelo = "test.com";
	var $errmsg= ""; 
    var $relayServer = "";
	var $host;
	var $from;
	var $to;
	var $to_name;
	var $subject;
	var $header;
	var $body;
	var $sendOk;
		
    function sendMail($host, $_from, $_to,$_to_name, $subject, $body) { 
		
		$this->host = $host;
		$this->from = $_from;
		$this->to = $_to;
		$this->to_name = $_to_name;
		$this->subject = $subject;
		$this->body = $body;
			
		$this->header = $this->mkHeader($_from, $_to,$_to_name, $subject); 
		$this->body = $this->mkBody();
		
		$this->body = $this->header.$this->body;

		$this->sendOk = $this->SendSmtp();
		
    }
    
	function mkHeader($from_mail, $to_mail, $to_name, $subject) {

		$from = "<${from_mail}>";

		if ($to_name) 
			$to = "=?$this->charSet?B?".base64_encode($to_name)."?= <${to_mail}>";
		else
			$to = "<${to_mail}>";

		$header = "";
		$header.= "From: $from\r\n";
		$header.= "To: $to\r\n";
		$header.= "Subject: =?$this->charSet?B?".base64_encode($subject)."?=\r\n";
		$header.= "Errors-To: $from\r\n";
		$header.= "Reply-To: $from\r\n";
		$header.= "X-Priority: 3\r\n"; 
		$header.= "X-MSMail-Priority: High\r\n";
		$header.= "X-Mailer: mail 2003\r\n";
		$header.= "Content-Type: text/html; charset=$this->charSet\r\n\r\n";
        return $header;
    }
    


	function mkBody(){

		$this->body = str_replace(chr(13).chr(10),chr(10),$this->body);
		$this->body = str_replace(chr(13),chr(10),$this->body);
		$this->body = str_replace(chr(10),chr(13).chr(10),$this->body);
		return $this->body;

	}

    function SendSmtp() {
        $conn = @fsockopen($this->host, 25,$fsock_errno,$fsock_errstr,1);   

		if ($conn) {
			/* protocol 을 주고받는 부분 */
			if(!$this->recvmsg($conn, $recv_buf)) {
				$this->setError($recv_buf);
				return 0;   
			}
			// HELO
			$buf = sprintf("HELO %s\r\n", $this->defaultHelo);
			fputs($conn, $buf);
			if(!$this->recvmsg($conn, $recv_buf)) {
				$this->setError($recv_buf);
				return 0;   
			}                
			// MAIL this->from
			$buf = sprintf("MAIL this->from: <%s>\r\n", $this->from);
			fputs($conn, $buf);
			if(!$this->recvmsg($conn, $recv_buf)) {
				$this->setError($recv_buf);
				return 0;   
			}
			// RCPT TO
			$buf = sprintf("RCPT TO: <%s>\r\n", $this->to);
			fputs($conn, $buf);
			if(!$this->recvmsg($conn, $recv_buf)) {
				$this->setError($recv_buf);
				return 0;   
			}
			// DATA
			fputs($conn, "DATA\r\n");
			$this->recvmsg($conn, $recv_buf);
			$buf = sprintf("%s\r\n.\r\n", $this->body);
			fputs($conn, $buf);
			if(!$this->recvmsg($conn, $recv_buf)) {
				$this->setError($recv_buf);
				return 0;   
			}
			// QUIT
			fputs($conn, "QUIT\r\n");
			$this->recvmsg($conn, $recv_buf);

			fclose($conn);
			return 1;
		} else { 
			/* 컨넥팅 실패 */
			$this->setError("Connecting Fail");
			return 0;
		} 
	}

	function recvMsg($conn, &$recv_buf) {
		$ok_code = array("220", "250", "354", "221");
		$recv_buf = fgets($conn, 1024);
		for ($i = 0; $i < 4; $i++) {
			if (substr($recv_buf, 0, 3) == $ok_code[$i])
				return 1;
		}
        return 0;
    }

	function setError($msg) {
			$this->errmsg = $msg;
    }

}

?>
