<?php
	require_once dirname(__FILE__)."/../../dal/registryDAL.php";
	require_once dirname(__FILE__)."/../PHPMailer/class.phpmailer.php";
	require_once dirname(__FILE__)."/../PHPMailer/class.smtp.php";
	require_once dirname(__FILE__)."/../PHPMailer/class.pop3.php";
	class smtp_mail{
		public $toAry = array();
		public $name;
		public $subject;
		public $body;
		public function send(){
			if(registryDAL::getRegistryValue("sendMailType") == "1"){
				$smtpSecure = "";
				$host = "smtp.hibox.biz";
				$port = "25";
				$smtpAuth = "1";
				$username = "mail@cd.com.tw";
				$password = "az147588";
			}else{
				$smtpSecure = registryDAL::getRegistryValue("sendMailSMTPSecure");
				$host = registryDAL::getRegistryValue("sendMailHost");
				$port = registryDAL::getRegistryValue("sendMailPort");
				$smtpAuth = registryDAL::getRegistryValue("sendMailSMTPAuth");
				$username = registryDAL::getRegistryValue("sendMailAddress");
				$password = registryDAL::getRegistryValue("sendMailPassword");
			}
	
			//收件者mail,mail標體,mail內容,寄件者mail,寄件者名稱
			//宣告一個PHPMailer物件
			$mail = new PHPMailer();
			//設定使用SMTP發送
			$mail->IsSMTP();
			//SSL
			if($smtpSecure != ""){
				$mail->SMTPSecure = $smtpSecure;
			}
			//指定SMTP的服務器位址
			$mail->Host = $host;
			//$mail->Host = "smtp.gmail.com";
			//設定SMTP服務的POST
			$mail->Port = $port; //default is 25, gmail is 465 or 587
			//$mail->Port = 465; //default is 25, gmail is 465 or 587
			//設定為安全驗證方式
			$mail->SMTPAuth = $smtpAuth;
			//SMTP的帳號
			$mail->Username = $username;
			//SMTP的密碼
			$mail->Password = $password;
			//寄件人Email	//寄件人名稱
			$mail->SetFrom($username,$this->name);
			//收件人Email
			$mail->AddAddress($this->toAry[0]);
			for($i=1;$i<count($this->toAry);$i++){
				$mail->AddCC(trim($this->toAry[$i]));
			}
	
			//回信Email及名稱
			$mail->AddReplyTo($username,$this->name);
			//設定信件字元編碼
			$mail->CharSet="utf-8";
			//設定信件編碼，大部分郵件工具都支援此編碼方式
			$mail->Encoding = "base64";
			//
			$mail->SMTPKeepAlive = TRUE;
			$mail->SMTPDebug = 1; // 1: message, 2: full result
			//$mail->SMTPDebug = 2; // 1: message, 2: full result
			//設置郵件格式為HTML
			$mail->IsHTML(true);
			//每50自斷行
			$mail->WordWrap = 50;
			//郵件標題
			$mail->Subject = $this->subject;
			//郵件內容
			$mail->Body = $this->body;
			//寄送郵件
			if(!$mail->Send()){
				$status = "Mailer Error: " . $mail->ErrorInfo;
			}else{
				$status = "1";
			}
			return $status;
		}
	}
?>