<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_mail_set.php
 * 摘　　要：
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
$action = trim($_REQUEST['action']);
$do = trim($_REQUEST['do']);
$tpl -> assign("do",$do);
switch ($do){		
		case 'mail_tpl':
		if(!getAdminPri('1201'))exit(NO_AUTH_LOGIN);
		$tpl->assign('mail_list',get_mail_tpl());
		$tpl -> assign("admin_nav",shownav('邮件模板管理'));
		$tpl->assign('action','mail_tpl');
	break;
	case 'edit_tpl'://编辑邮件模板
		if(!getAdminPri('1201'))exit(NO_AUTH_LOGIN);
		if(is_post()){
			$id = to_int($_POST['tpl_id']);
			$title = $_POST['mail_title'];
			if(empty($id) || empty($title))exit($sp->bgmsg('参数错误'));
			$updata_data = array(
				'tpl_open'=>!empty($_POST['tpl_open'])?'1':0,
				'mail_tile'=>$title,
				'mail_content'=>$_POST['mail_content'],
				'last_modify'=>date("Y-m-d h:i:s"),
			);
			$db->update($updata_data,'mail_tpl',array('tpl_id'=>$id));
			php188StaticCache::getInstance()->remove('php188_wms_mail_config');
			$sp->bgmsg('操作成功','admincp.php?action=mail_set&do=mail_tpl');
			exit();
		}else{
			$id = to_int($_GET['id']);
			if(empty($id))exit($sp->bgmsg('参数错误.','admincp.php?action=mail_set&do=mail_tpl'));
			$data = $db->fetch("SELECT * FROM ##__mail_tpl WHERE `tpl_id` = '$id'",TRUE);
			if(empty($data))exit($sp->bgmsg('无可用数据.','admincp.php?action=mail_set&do=mail_tpl'));
			$tpl->assign('data',$data);	
			$tpl->assign('task','edit_tpl');		
		}
		break;
	case 'mail_send'://发信
		if(!getAdminPri('1203'))exit(NO_AUTH_LOGIN);
		if(is_post()){
			$_SESSION['_______test_mail'] = $_POST;
			$subject = $_POST['subject'];
			$reciver = $_POST['reciver'];
			$content = $_POST['content'];
			if(empty($subject) || empty($reciver) || empty($content))exit($sp->bgmsg('请输入打*号的内容.','admincp.php?action=mail_set&do=mail_send'));
			if(!is_email($reciver))exit($sp->bgmsg('邮件地址格式错误','admincp.php?action=mail_set&do=mail_send'));
			$do = send_mail('','','',array($reciver),$subject,$content);
			if($do){
				$_SESSION['_______test_mail'] = '';
				exit($sp->bgmsg('邮件发送成功','admincp.php?action=mail_set&do=mail_send'));
			
			}else{
				exit($sp->bgmsg('邮件发送失败,可能是您的邮件配置错误.','admincp.php?action=mail_set&do=mail_send'));
			}
		}else{
			$tpl -> assign("admin_nav",shownav('在线发邮件'));
			$tpl->assign('se',$_SESSION['_______test_mail']);
		}
		break;
	default://邮件服务器设置
		if(!getAdminPri('1201'))exit(NO_AUTH_LOGIN);
		if(is_post()){
			$is_test = $_POST['test'];
			switch ($is_test){
				case 'pub':
					$save_data = array(
						'mail_send_type'=>$_POST['mail_send_type'],
						'sender'=>$_POST['sender'],
						'smtp_host'=>$_POST['smtp_host'],
						'smtp_pass'=>$_POST['smtp_pass'],
						'smtp_user'=>$_POST['smtp_user'],
						'smtp_port'=>$_POST['smtp_port']
					);
					siteConfig('site_mail_cfg',$save_data);
					$sp->bgmsg('保存成功','admincp.php?action=mail_set');
					break;
				case 'test':
					$reciver = $_POST['test_email'];
					if(!is_email($reciver))exit('请输入正确的EMAIL地址.');
					$reciver = array($reciver);
					$from = $_POST['sender'];
					$type = $_POST['mail_send_type'];
					$sender = $_POST['sender'];
					$fromName = $from;
					if(empty($sender))exit('请填上发送人邮件地址.');
					$subject = '测试邮件';
					$body = '这是一封测试邮件,如果您收到本信,说明您的邮箱配置正确.';
					$host = $_POST['smtp_host'];
					$user = $_POST['smtp_user'];
					$pass = $_POST['smtp_pass'];
					$port = $_POST['smtp_port'];
					require PUB_CLASS.'phpmail/class.phpmailer.php';
					$mail = new PHPMailer();
					$mail->CharSet = 'GBK';//设置邮件编码
					$mail->From = $from;
					$mail->FromName = $fromName;
					//处理多个收信人
					if(is_array($reciver)&&!empty($reciver)){
						foreach ($reciver as $r){
							$mail->AddAddress($r, array_shift(explode('@',$r)));
						}
					}
					$mail->WordWrap = 50;   
					$mail->Subject = strip_tags($subject);
					$mail->AltBody = strip_tags($body);//如果邮箱不支持HTML就用他
					$mail->IsHTML(true); 
					$mail->Port = $port;
					$mail->AddReplyTo($from,$fromName);
					switch ($type){//获得站内邮箱配置
						case 'mail':
						$mail->MsgHTML($body);
							break;
						case 'sendmail':
							$mail->IsSendmail();
							$mail->MsgHTML($body);
							break;
						case 'smtp':
						$mail->IsSMTP();   
						$mail->Host = trim($host); 
						$mail->SMTPAuth = true;     
						$mail->Username = trim($user); 
						$mail->Password = trim($pass);
						$mail->MsgHTML($body);
							break;
						default:exit('发信失败,请重新配置');
					}
					$do =  $mail->Send()?TRUE:FALSE;
					if($do){
						exit('发信成功,请检查邮箱是否收到信.');
					}else{
						exit('发信失败,请重新配置');
					}
					break;
				default:exit('非法操作');
			}
			exit();
		}else{
			$tpl -> assign("admin_nav",shownav('邮件服务器设置'));
			$tpl->assign('mail_config',get_cache('site_mail_cfg'));
		}
}
$sp -> outPut('mail_set.tpl');
?>
