<?php
/*
邮件发送类
showin@2008-8-14 modify
支持base64验证登录
*/
class Easy_Mail{
	public $server = array();	//smtp服务器
	public $subject = 'subject';	//标题
	public $to = array();	//接受?
	public $htmlbody = '';
	public $textbody = '';
	public $debug = false;
	public $indexno = 0;
	public $ErrorInfo = array();
	public $auth = true;
	public $fromname = '天天网络';
	public $mailtype = 'HTML';
	public $charset ='gb2312';
	/*
	发?email
	*/
	public function sendMail(){
		$fp = fsockopen ( $this->server[$this->indexno]['server'], $this->server[$this->indexno]['port'], $errno, $error, 30);
		if (!$fp){
			$this->error('连接服务器失?!', $error, __line__);
			exit;
		}else{
			stream_set_blocking($fp, true );
			if ( substr( fgets($fp,512), 0, 3) != 220 ){
				$this->error("错误信息:" , $lastmessage , __LINE__);
				return false;
			}
			
			$host = "localhost";
			if ($this->auth ==true){
				$send = "EHLO ".$host."\r\n";
			}else{
				$send = "HELO ".$host."\r\n";
			}
			fputs($fp, $send);
			/*
			$lastmessage = fgets($fp,512);
			if (substr($lastmessage,0,3) == 250){
				$this->error("发?EHLO错误:" , $lastmessage , __LINE__);
				return false;
			}*/
			while ( true ) {
				$lastmessage = fgets($fp,512);
				//echo $lastmessage.'<br>';
				if ( substr($lastmessage,3,1) != "-" || empty($lastmessage)===true){
					//echo 'exit'.$lastmessage.'<br>';
					break;
				}

			}

			//如果?启验?
			if ($this->auth === true){
				$send = "AUTH LOGIN"."\r\n";		//发?登录信?
				fputs( $fp, $send);
				$lastmessage = fgets ($fp, 1024);
				if (substr($lastmessage,0,3) != 334){
					$this->error("验证失败:" , $lastmessage , __LINE__);
					return false;
				}
				//用户姓名
				$send = base64_encode($this->server[$this->indexno]['user'])."\r\n";
				fputs( $fp, $send);
				$lastmessage = fgets ($fp,512);
				if (substr($lastmessage,0,3) != 334){
					$this->error("用户名错?:" , $lastmessage , __LINE__);
					return false;
				}
				//用户密码
				$send = base64_encode($this->server[$this->indexno]['pass'])."\r\n";
				fputs( $fp, $send);
				$lastmessage = fgets ($fp,512);
				if (substr($lastmessage,0,3) != "235"){
					$this->error("登录失败:" , $lastmessage , __LINE__);
					return false;
				}
			}	//end auth
			//FROM:
			$send = "MAIL FROM: ". $this->server[$this->indexno]['from'] . "\r\n";
			fputs( $fp, $send);
			$lastmessage = fgets ($fp,512);
			if (substr($lastmessage,0,3) != 250){
				$this->error("发生错误在send from:" , $lastmessage , __LINE__);
			}
			//TO:
			foreach($this->to as $mail){
				$send = "RCPT TO: ".$mail."\r\n";
				fputs( $fp, $send);
				$lastmessage = fgets ($fp,512);
				if (substr($lastmessage,0,3) != 250){
					$this->error("发生错误在send from:" , $lastmessage , __LINE__);
				}
			}
			//DATA
			$send = "DATA\r\n";
			fputs($fp, $send);
			$lastmessage = fgets ($fp,512);
			if (substr($lastmessage,0,3) != 354){
				$this->error("发生错误在send from:" , $lastmessage , '105');
				return false;
			}
			//处理Subject

			$headers = '';
			$maildelimiter = "\r\n";
			$subject = '=?'.$this->charset.'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', $this->subject))).'?=';
			$email_message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $this->htmlbody)))))));
			$email_from = '=?'.$this->charset.'?B?'.base64_encode($this->fromname)."?= <".$this->server[$this->indexno]['from'].">";
			if ($this->mailtype == 'HTML' ){
				$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: vodcms.com! {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=$this->charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
			}
			$headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}";
			//处理From头
			//加上结束串
			fputs($fp, "Date: ".gmdate('r')."\r\n");
			fputs($fp, "To: ".implode(",", $this->to)."\r\n");
			fputs($fp, "Subject: ".$subject."\r\n");
			fputs($fp, $headers."\r\n");
			fputs($fp, "\r\n\r\n");
			fputs($fp, "$email_message\r\n.\r\n");
			fputs($fp, "QUIT\r\n");
			fclose($fp);
			return true;
		}
	}
	public function error($message, $error, $line=0){
		if ($this->debug == true){
			echo $message. $error.'line:'.$line.'<br />';
		}else{
			$this->ErrorInfo = $message.$error.'line:'.$line.'<br />';
			$this->indexno = $this->indexno + 1;	//发?失败尝试下?组服务器
			if ($this->server[$this->indexno]){	//若服务器地址被定?
				$this->sendmail();
			}
		}
	}
}
/*$mail = new smtpMailer;
$mail->server[] = array(
	'server'=>'mail.gol.com',
	'port' => 587 ,
	'user' => 'ko-wa.inter.n@gol.com',
	'pass' => 'bSrfpdY%',
	'from'=> 'b2b@made-in-china.co.jp');
$mail->server[] = array(
	'server'=>'mail.163.com',
	'port' => 25 ,
	'user' => 'zangguoping@163.com',
	'pass' => 'yzmbhack<>?',
	'from'=> 'zangguoping@163.com');
$mail->to = array('10514873@qq.com', '24498936@qq.com','jin@made-in-china.co.jp','zangguoping@163.com');
$mail->subject = '测试幽默';
$mail->htmlbody = '测试的邮件内容';
$mail->sendmail();*/
?>