<?php
class emailModel extends model{
	public $base;
	public $emailServer=NULL;
	public $smsServer=NULL;
	function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="email";
	}
	
	public function setEmail($data){
		$this->emailServer=$data;
		return $this;
	}
	
	public function unSetEmail(){
		$this->emailServer=NULL;
		return $this;
	}
	
	public function setSms($data){
		$this->smsServer=$data;
		return $this;
	}
	
	public function unSetSms(){
		$this->smsServer=NULL;
		return $this;
	}
	
	public function sendEmail( $smtpemailto, $mailsubject, $mailbody , $mailtype="html"){
		$this->base->loadClass("phpmailer",false,false);
		
		if(!$this->emailServer){
			$smptArr=array(
				"smtphost"=>SMTPHOST,
				"smtpport"=>SMTPPORT,
				"smtpemail"=>SMTPEMAIL,
				"smtpuser"=>SMTPUSER,
				"smtppwd"=>SMTPPWD
			);
		}else{
			$smptArr=$this->emailServer;
		
		}
		print_r($smptArr);
		//开始发送
		$mail             = new PHPMailer();
		$mail->IsSMTP();
		$mail->Host       = $smptArr['smtphost'];
		$mail->SMTPAuth   = true;
		$mail->Port       = $smptArr['smtpport'];
		$mail->Username   = $smptArr['smtpuser']; 
		$mail->Password   = $smptArr['smtppwd'];
		$mail->SetFrom($smptArr['smtpemail']);
		$mail->Subject    = $mailsubject;

		$address = $smtpemailto;
		$mail->AddAddress($address);
		$mail->MsgHTML($mailbody);  
		if(!$mail->Send()) {
		  return false;
		} else {
		  return true;
		} 
		

	}
	/*
	*HTTP接口案例：http://www.chinaccnet.com/api.php?uid=ms1380895386&sign=0e774008fc20dac1b24ef145c077d1b3&mob=&content=&intime= 

说明：uid:SMS用户ID pwd:SMS用户密码 
mob:要发给对方的手机号码，多个以“,”号隔开，如13500000000,13900000000 一次最多可以发送100条。
content:短信内容，intime:发短信的时间
返回状态码	信息说明
000	成送成功！
-01	当前账号余额不足！
-02	当前用户ID错误！
-03	当前密码错误！
-04	短信内容含有非法关键词！
-05	手机号码格式不对！
-06	您的IP地址未指定！
-10	手机号码数量超长！（100个/次 超100个请自行做循环）
-11	短信内容超长！（70个字符）
-12	其它错误！
	*/
	function sendSMS($mobile,$content,$time=0,$mid=0)
	{
	 	if(empty($this->smsServer)){
			$data=array(
				"uid"=>PHONE_USER,
				"sign"=>PHONE_PWD
			);
		}else{
			$data=$this->smsServer;
		}
		$http = 'http://www.chinaccnet.com/api.php';
		$data = array
			(
			'uid'=>$data['uid'],					//用户账号
			'sign'=>$data['sign'],	//MD5位32密码
			'mob'=>$mobile,				//号码
			'content'=>iconv("utf-8","gbk",$content),			//内容
			'intime'=>$time,		//定时发送
			'mid'=>$mid						//子扩展号
			);
		$re= $this->postSMS($http,$data);			//POST方式提交
		if( preg_match("/state=000/i",$re)==true )
		{
			return true;
		}
		else 
		{
			//echo $re;
			return  false;
		}
	}
	

	
	function postSMS($url,$data='')
	{
		$row = parse_url($url);
		$host = $row['host'];
		$port = $row['port'] ? $row['port']:80;
		$file = $row['path'];
		while (list($k,$v) = each($data)) 
		{
			$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		$post = substr( $post , 0 , -1 );
		$len = strlen($post);
		$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
		if (!$fp) {
			return "$errstr ($errno)\n";
		} else {
			$receive = '';
			$out = "POST $file HTTP/1.1\r\n";
			$out .= "Host: $host\r\n";
			$out .= "Content-type: application/x-www-form-urlencoded\r\n";
			$out .= "Connection: Close\r\n";
			$out .= "Content-Length: $len\r\n\r\n";
			$out .= $post;		
			fwrite($fp, $out);
			while (!feof($fp)) {
				$receive .= fgets($fp, 128);
			}
			fclose($fp);
			$receive = explode("\r\n\r\n",$receive);
			unset($receive[0]);
			return implode("",$receive);
		}
	}
	
}
?>