<?php
function qunfa_emaildiy($info ,$user_id = '', $user_type = 'user')
{
	global $db, $pe;
	$setting = cache::get('email');
	pe_lead('include/class/mail/mail.class.php');

	//提取可用的用户email信息，
	if ($user_type == 'rssuser') {
		$sqlfield = "`rssuser_name` as `user_name`, `rssuser_email` as `user_email`";
		$sqlwhere = " and `rssuser_email` != '' and `rssuser_id` in({$user_id})";
		$user_list = $db->pe_selectall('rssuser', $sqlwhere, $sqlfield);	
	}
	else {
		$sqlwhere = " and `user_email` != '' and `user_id` in({$user_id})";
		$user_list = $db->pe_selectall('user', $sqlwhere);	
	}

	foreach ($user_list as $k => $v) {
		if ($k == 0) {
			$smtp = current($setting);
		}
		else {
			$smtp = next($setting);
			if ($smtp === false) {
				reset($setting);
				$smtp = current($setting);
			}
		}
		$mail = new PHPMailer();                  //创建PHPMailer实例  
		$mail->IsSMTP();                          //设置SMTP模式
		$mail->IsHTML(true);
		$mail->Host     = $smtp['email_smtp'];    //SMTP服务器地址  
		$mail->Port     = $smtp['email_port'];    //SMTP服务器端口  
		$mail->SMTPAuth = true;                   //SMTP认证  
		$mail->Username = $smtp['email_name'];   //认证用户名  
		$mail->Password = $smtp['email_pw'];      //认证密码  
		//$mail->Subject  = $info['qunfa_name'];  //邮件标题  
		//$mail->Body     = $info['qunfa_text'];
		$mail->CharSet = 'utf-8';                 // 这里指定字符集！
		$mail->Encoding = 'base64';
		$mail->SetFrom($smtp['email_name'], $smtp['email_nname']); //设置发件人
		
		$mail->Subject  = "{$v['user_name']}，{$info['qunfa_name']}";
		$email_stop_text = "<br/><br/><br/><hr/>";
		$email_stop_text .= "<p>1.如您不想再继续接收我们邮件，请点击<a href='{$pe['host_root']}user.php?mod=do&act=email_stop&user_name={$v['user_name']}&user_email={$v['user_email']}'>这里退订。</a></p>";
		$email_stop_text .= "<p>2.任何疑问可联系我们的<a href='{$pe['host_root']}' target='_blank'>在线客服</a>。</p>";
		$mail->Body     = "<p>{$v['user_name']}，</p>{$info['qunfa_text']}{$email_stop_text}";
		$mail->AddAddress($v['user_email'], $v['user_name']);  //添加收件人  
		$mail->Send();
		$mail->ClearAddresses();
	}
}
function qunfa_emailtpl()
{
	global $db, $pe;
	$setting = cache::get('email');
	//判断是否有emailtpl任务可执行
	$sql = "select a.*,b.`emailtpl_name`,b.`emailtpl_text` from `".dbpre."emailtask` a left join `".dbpre."emailtpl` b on a.`emailtpl_id` = b.`emailtpl_id` where a.`emailtask_date` <= '".date('Y-m-d')."'";
	$emailtask_list = $db->sql_selectall($sql);
	if (!count($emailtask_list))return;
	pe_lead('include/class/mail/mail.class.php');
	foreach ($emailtask_list as $k => $v) {
		if ($k == 0) {
			$smtp = current($setting);
		}
		else {
			$smtp = next($setting);
			if ($smtp === false) {
				reset($setting);
				$smtp = current($setting);
			}
		}
		//$smtp = current($setting);
		$mail = new PHPMailer();                  //创建PHPMailer实例  
		$mail->IsSMTP();                          //设置SMTP模式
		$mail->IsHTML(true);
		$mail->Host     = $smtp['email_smtp'];    //SMTP服务器地址  
		$mail->Port     = $smtp['email_port'];    //SMTP服务器端口  
		$mail->SMTPAuth = true;                   //SMTP认证  
		$mail->Username = $smtp['email_name'];;   //认证用户名  
		$mail->Password = $smtp['email_pw'];      //认证密码  
		//$mail->Subject  = $info['qunfa_name'];  //邮件标题  
		//$mail->Body     = $info['qunfa_text'];
		$mail->CharSet = 'utf-8';                 // 这里指定字符集！
		$mail->Encoding = 'base64';
		$mail->SetFrom($smtp['email_name'], $smtp['email_nname']); //设置发件人

		$mail->Subject  = "{$v['user_name']}，{$v['emailtpl_name']}";
		$email_stop_text = "<br/><br/><br/><hr/>";
		$email_stop_text .= "<p>如您不想再继续接收到我们的邮件，<a href='{$pe['host_root']}user.php?mod=do&act=email_stop&user_name={$v['user_name']}&user_email={$v['user_email']}'>可点此退订。</a></p>";
		$mail->Body     = "<p>{$v['user_name']}，</p>{$v['emailtpl_text']}{$email_stop_text}";
		$mail->AddAddress($v['user_email'], $v['user_name']);  //添加收件人  
		$mail->Send();
		$mail->ClearAddresses();
		$db->pe_delete('emailtask', array('emailtask_id'=>$v['emailtask_id']));
	}
}
function qunfa_emailone($info ,$user = array())
{
	global $db, $pe;
	$setting = cache::get('email');
	pe_lead('include/class/mail/mail.class.php');

	$smtp = $setting[array_rand($setting,1)];
	$mail = new PHPMailer();                  //创建PHPMailer实例  
	$mail->IsSMTP();                          //设置SMTP模式
	$mail->IsHTML(true);
	$mail->Host     = $smtp['email_smtp'];    //SMTP服务器地址  
	$mail->Port     = $smtp['email_port'];    //SMTP服务器端口  
	$mail->SMTPAuth = true;                   //SMTP认证  
	$mail->Username = $smtp['email_name'];;   //认证用户名  
	$mail->Password = $smtp['email_pw'];      //认证密码  
	//$mail->Subject  = $info['qunfa_name'];  //邮件标题  
	//$mail->Body     = $info['qunfa_text'];
	$mail->CharSet = 'utf-8';                 // 这里指定字符集！
	$mail->Encoding = 'base64';
	$mail->SetFrom($smtp['email_name'], $smtp['email_nname']); //设置发件人

	$mail->Subject  = "{$info['qunfa_name']}";
	$email_stop_text = "<br/><br/><br/><hr/>";
	$email_stop_text .= "<p>由于您订购了我们的产品，所以收到此邮件，如您有任何疑问可点击联系我们的<a href='{$pe['host_root']}' target='_blank'>在线客服</a>。</p>";
	$mail->Body     = "{$info['qunfa_text']}{$email_stop_text}";
	$mail->AddAddress($user['user_email'], $user['user_name']); //添加收件人  
	$mail->Send();
	$mail->ClearAddresses();
}
?>