<?php
require_once SYS_ROOT.'/phpmailer/class.phpmailer.php';
class cls_ctl_mail
{
	var $perms;
    function __construct($arr_cfg=array())
    {
		global $obj_tab_sys_config;
		$arr_sys_cfg=$obj_tab_sys_config->get_model("sys");
		$this->perms["config"]=array(
			"host"=>"",
			"port"=>"25",
			"from"=>"",
			"fromname"=>"",
			"username"=>"",
			"password"=>""
		);
		$this->perms["auto"]=array(
			"filename"=>"content.html",
		);
		$this->perms["path"]="/data/email";
		if(is_array($arr_sys_cfg)){
			if(isset($arr_sys_cfg["mail_host"])) $this->perms["config"]["host"]=$arr_sys_cfg["mail_host"];
			if(isset($arr_sys_cfg["mail_port"])) $this->perms["config"]["port"]=$arr_sys_cfg["mail_port"];
			if(isset($arr_sys_cfg["mail_from"])) $this->perms["config"]["from"]=$arr_sys_cfg["mail_from"];
			if(isset($arr_sys_cfg["mail_fromname"])) $this->perms["config"]["fromname"]=$arr_sys_cfg["mail_fromname"];
			if(isset($arr_sys_cfg["mail_username"])) $this->perms["config"]["username"]=$arr_sys_cfg["mail_username"];
			if(isset($arr_sys_cfg["mail_password"])) $this->perms["config"]["password"]=$arr_sys_cfg["mail_password"];
			if($this->perms["config"]["fromname"]=="") $this->perms["config"]["fromname"]=$this->perms["from"];
		}
		if(count($arr_cfg)>0){
			if(isset($arr_cfg["mail_host"])) $this->perms["config"]["host"]=$arr_cfg["mail_host"];
			if(isset($arr_cfg["mail_port"])) $this->perms["config"]["port"]=$arr_cfg["mail_port"];
			if(isset($arr_cfg["mail_from"])) $this->perms["config"]["from"]=$arr_cfg["mail_from"];
			if(isset($arr_cfg["mail_fromname"])) $this->perms["config"]["fromname"]=$arr_cfg["mail_fromname"];
			if(isset($arr_cfg["mail_username"])) $this->perms["config"]["username"]=$arr_cfg["mail_username"];
			if(isset($arr_cfg["mail_password"])) $this->perms["config"]["password"]=$arr_cfg["mail_password"];
			if($this->perms["config"]["fromname"]=="") $this->perms["config"]["fromname"]=$this->perms["from"];
		}
    }
	function get_autopath($msgname){
		return get_real_path("",$this->perms["path"]."/".$msgname);
	}
	function on_auto_send(){
		$arr_return=array();
		$arr_config=get_autoconfig();
		$arr_email=get_autoemail($arr_config["id"].".txt");
		$lng_count=count($arr_email);
		if($lng_count<1){
			$arr_return["err"]="邮件文件不存在！";
			return $arr_return;
		}
		$arr_config["num"]=$lng_count;
		$to_mail=$arr_email[$arr_config["item"]];
		$arr_config["item"]++;
		self::save_autoconfig($arr_config);
		$str_content=file_get_contents(self::get_autopath($this->perms["auto"]["filename"]));
		self::on_send($to_mail,$msg_title,$str_content,$msg_attachment=array(),$msg_replyto="",$msg_replytoname="");
		return $arr_return;
	}
	function get_autoconfig(){
		$str_path=self::get_autopath("config.txt");
		if(isset($str_path)){
			$arr_config=unserialize(file_get_contents($str_path));
			$arr_config["item"]++;
			if($arr_config["num"]>0 && $arr_config["num"]<=$arr_config["item"]){
				$arr_config["sendids"][]=$arr_config["id"];
				$arr_config["id"]++;
				$arr_config["num"]=0;
				$arr_config["item"]=0;
			}
		}else{
			$arr_config=array(
				"id"=>1,//当前发送文件编号
				"num"=>0,//当前发送文件总邮件数
				"item"=>0,//当前发到第几个邮件
				"updatetime"=>date("Y-m-d H:i:s"),//最后发布邮件时间
				"starttime"=>date("Y-m-d H:i:s"),//起始发布邮件时间
				"sendids"=>array()//已发送邮件文件号
			);
		}
		$arr_config["time"]=date("Y-m-d H:i:s");
		return $arr_config;
	}
	function get_autoemail($msgfile){
		$arr_email=array();
		$str_path=self::get_autopath($msgfile);
		if(!is_file($str_path)){
			return $arr_email;
		}
		$str_content=str_replace(chr(13),chr(10),file_get_contents($str_path));
		$str_content=str_replace(chr(10).chr(10),chr(10),$str_content);
		$arr_email=explode(chr(10),$str_content);
		return $arr_email;
	}
	function on_send($to_mail,$msg_title,$msg_content,$msg_attachment=array(),$msg_replyto="",$msg_replytoname=""){
		$arr_return=array("err"=>"");
		foreach($this->perms["config"] as $item=>$key){
			if($key==""){
				$arr_return["err"]="系统邮件服务功能未设置，请联系管理员!";
				return $arr_return;
			}
		}
		if(self::chk_email($this->perms["config"]["from"])!=""){
			$arr_return["err"]="系统邮件服务发件地址有问题，请联系管理员!";
			return $arr_return;
		}
		if($to_mail=="" || self::chk_email($to_mail)!=""){
			$arr_return["err"]="收件邮箱地址有误!";
			return $arr_return;
		}
		if($msg_title==""){
			$arr_return["err"]="信件主题不能为空！";
			return $arr_return;
		}
		if($msg_content=="" && count($msg_attachment)<1){
			$arr_return["err"]="信件内容不能为空！";
			return $arr_return;
		}

		$mail = new PHPMailer();
		$mail->CharSet = "gb2312";		 //设置采用gb2312中文编码
		$mail->IsSMTP();				 //设置采用SMTP方式发送邮件
		$mail->Host = $this->perms["config"]["host"];    //设置邮件服务器的地址
		$mail->Port = $this->perms["config"]["port"];                           //设置邮件服务器的端口，默认为25
		$mail->From     = $this->perms["config"]["from"]; //设置发件人的邮箱地址
		$mail->FromName = $this->perms["config"]["fromname"];                       //设置发件人的姓名
		$mail->SMTPAuth = true;                                    //设置SMTP是否需要密码验证，true表示需要
		$mail->Username=$this->perms["config"]["username"];
		$mail->Password = $this->perms["config"]["password"];
		$mail->Subject = $msg_title;                                 //设置邮件的标题
		$mail->AltBody = "text/html";                                // optional, comment out and test
		$mail->Body = $msg_content;                    
		$mail->IsHTML(true);                                        //设置内容是否为html类型
		//$mail->WordWrap = 50;                                 //设置每行的字符数
		if($msg_replyto!=""){
			if($msg_replytoname=="") $msg_replytoname=$msg_replyto;
			$mail->AddReplyTo($msg_replyto,$msg_replytoname);     //设置回复的收件人的地址
		}
		$mail->AddAddress($to_mail);     //设置收件的地址
		//$mail-> Mailer       =   "smtp "; 
		//$mail-> SetLanguage( "en ",   "language/ "); 
		if(count($msg_attachment)>0){
			foreach($msg_attachment as $item){
				$mail->AddAttachment($item["path"],$item["name"]);  // optional name
			}
		}
		if(!$mail->Send()) {                    //发送邮件
			$arr_return["err"]="发送失败！";
		}else{
			//file_put_contents(self::get_autopath("cache.txt"),$to_mail);
		}
		return $arr_return;
	}
	function get_lastsend(){
		$str_path=self::get_autopath("cache.txt");
		$str_name=file_get_contents($str_path);
		return $str_name;
	}
	function chk_email($msgemail)
	{
		if(!preg_match("/^[a-zA-Z0-9]+([.a-zA-Z0-9_-])*@([a-zA-A0-9_-])+(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$msgemail)){
			return "输入邮箱不正确！";
		}
	}
}
?>