<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function mail_template_list(){
	static $res = NULL;
	if ($res === NULL){
		$data = read_static_cache('mail_template');
		if($data == false){
			$query  = $GLOBALS['db'] -> query("SELECT * FROM `{$GLOBALS['db_mymps']}mail_template` ORDER BY template_id DESC");
			while($row = $GLOBALS['db'] -> fetchRow($query)){
				$re[$row['template_code']]['template_id'] 		= $row['template_id'];
				$re[$row['template_code']]['is_sys'] 			= $row['is_sys'];
				$re[$row['template_code']]['template_code'] 	= $row['template_code'];
				$re[$row['template_code']]['is_html'] 			= $row['is_html'];
				$re[$row['template_code']]['template_subject'] 	= $row['template_subject'];
				$re[$row['template_code']]['template_content'] 	= $row['template_content'];
				$re[$row['template_code']]['last_modify'] 		= $row['last_modify'];
				$re[$row['template_code']]['last_send']		 	= $row['last_send'];
				$re[$row['template_code']]['is_html']		 	= $row['is_html'];
				$res = $re;
			}
			write_static_cache('mail_template', $res);
		} else {
			$res = $data;
		}
	}
	return $res;
}

//获得邮箱服务器初始配置
function get_mail_setting(){
	global $db,$db_mymps;
	static $res = NULL;
	if($res === NULL){
		$data = read_static_cache('mail_config');
		if($data == false){
			$re = $db -> query("SELECT * FROM `{$db_mymps}config` WHERE type='mail'");
			while($row = $db -> fetchRow($re)){
				$res[$row['description']] = $row['value'];
			}
			write_static_cache('mail_config', $res);
		} else {
			$res = $data;
		}
	}
	return $res;
}

//写入邮件发送记录
function write_mail_sendrecord($email_to,$email_subject,$email_content,$error,$template_id){
	global $timestamp;
	$GLOBALS['db'] -> query("INSERT INTO `{$GLOBALS['db_mymps']}mail_sendlist` (email,email_subject,email_content,error,last_send,template_id)VALUES('$email_to','$email_subject','$email_content','$error','$timestamp','$template_id')");
}

//发送电子邮件
function send_email( $email_to, $mail_subject, $mail_body, $mail_type = "html", $template_id = "" )

{

	require MYMPS_INC.'/PHPMailer/Exception.php';

	require MYMPS_INC.'/PHPMailer/PHPMailer.php';

	require MYMPS_INC.'/PHPMailer/SMTP.php';

	$mail_cfg = get_mail_setting();

	$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

	try {

		//服务器配置

		$mail->CharSet ="UTF-8";                     //设定邮件编码

		$mail->SMTPDebug = 0;                        // 调试模式输出

		$mail->isSMTP();                             // 使用SMTP

		$mail->Host = 'smtp.qq.com';                // SMTP服务器

		$mail->SMTPAuth = true;                      // 允许 SMTP 认证

		$mail->Username = $mail_cfg['mail_user'];                // SMTP 用户名  即邮箱的用户名

		$mail->Password = $mail_cfg['mail_pass'];             // SMTP 密码  部分邮箱是授权码(例如163邮箱)

		$mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议

		$mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持

	

		$mail->setFrom($mail_cfg['smtp_mail'], $mymps_global['SiteName']);  //发件人

		$mail->addAddress($email_to, $email_to);  // 收件人

		//$mail->addAddress('ellen@example.com');  // 可添加多个收件人

		$mail->addReplyTo($mail_cfg['smtp_mail'], 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致

		//$mail->addCC('cc@example.com');                    //抄送

		//$mail->addBCC('bcc@example.com');                    //密送

	

		//发送附件

		// $mail->addAttachment('../xy.zip');         // 添加附件

		// $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

	

		//Content

		$mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容

		$mail->Subject = $mail_subject;

		$mail->Body    = $mail_body;

		$mail->AltBody = '您有一封新邮件';

	

		$mail->send();

		return true;

	} catch (Exception $e) {

		return false;

	}

}

function send_pwd_email($uid,$userid,$email,$code){
	global $db,$db_mymps,$mymps_global,$timestamp;
	$mailtmp = $db ->getRow("SELECT * FROM `{$db_mymps}mail_template` WHERE template_id = '1'");
	if(empty($mailtmp)){
		return false;
	}else{
		$mail_subject = $mymps_global['SiteName'].'密码修改';
		$code = $mymps_global['SiteUrl'].'/'.$mymps_global['cfg_member_logfile'].'?mod=forgetpass&code='.$code;
		$mail_body = $mailtmp['template_content'];
		$mail_body = str_replace(array('{$user_name}','{$reset_email}','{$site_name}','{$send_date}'),array($userid,$code,$mymps_global['SiteName'],GetTime($timestamp)),$mail_body);
		return send_email($email,$mail_subject,$mail_body,'html',1);
	}
}

function send_validate_email($uid,$userid,$email,$code){
	global $db,$db_mymps,$mymps_global,$timestamp;
	$mailtmp = $db ->getRow("SELECT * FROM `{$db_mymps}mail_template` WHERE template_id = '2'");
	if(empty($mailtmp)){
		return false;
	}else{
		$mail_subject = $mymps_global['SiteName'].'用户验证';
		$code = $mymps_global['SiteUrl'].'/'.$mymps_global['cfg_member_logfile'].'?mod=validate&code='.$code;
		$mail_body = $mailtmp['template_content'];
		$mail_body = str_replace(array('{$user_name}','{$validate_email}','{$site_name}','{$send_date}'),array($userid,$code,$mymps_global['SiteName'],GetTime($timestamp)),$mail_body);
		return send_email($email,$mail_subject,$mail_body,'html',1);
	}
}
?>