<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[34]!=1)
  jump("jump.php?id=25&jump=main.php");
if(isset($_GET['action']) and $_GET['action'] == "submit")
{
  if(isset($_COOKIE["emailzt"]) && isset($_COOKIE["emailnr"]))
  {
    $_POST['email_zt']=$_COOKIE["emailzt"];
	$_POST['email_nr']=$_COOKIE["emailnr"];
	$_POST['type']=1;
  }
  if($_POST['email_zt']=="" || $_POST['email_nr']=="")
    jump("jump.php?id=2&jump=email.php");
  $email_nr=$_POST['email_nr'];
  if($_POST['type']==1)
  {
    $phpcolor->table="user";
    $phpcolor->field="email";
    if(!isset($_GET['page']))
    {
      $phpcolor->where="1=1";
      $count=$phpcolor->exist(2);
      $_GET['count1']=floor($count/5);
      $_GET['page']=0;
      while(TRUE)
	  {
	    if(!isset($_COOKIE["emailzt"]) and !isset($_COOKIE["emailnr"]))
          break;
        setcookie("emailzt", "", time()-10000000);
        setcookie("emailnr", "", time()-10000000);
      }
	  setcookie("emailzt", $_POST['email_zt'], time()+$core_time*3600+36000);
	  setcookie("emailnr", $_POST['email_nr'], time()+$core_time*3600+36000);
    }
    $phpcolor->where="1=1 limit ".($_GET['page']*5).",5";
    $user=$phpcolor->assignment();
    for($i=0;$i<count($user);$i++)
    {
      $smtp = new smtp($core_stmpserver,$core_stmpport,true,$core_stmpacc,$core_stmppwd);
      $smtp->debug =  false;
      $smtp->sendmail($user[$i]['email'], $core_stmpmail, $_POST['email_zt'], $email_nr, 'HTML');
	}
    if($_GET['page']==$_GET['count1'])
      jump("jump.php?id=24&jump=email.php");
	Header("refresh:3;url=email.php?action=submit&count1=".$_GET['count1']."&page=".($_GET['page']+1));
	echo "<html><head><link rel=stylesheet type='text/css' href='../admin/images/css.css'></head><body>";
    echo "<table width='100%' height='500' border='0'><tr>";
    echo "<td align='center' valign='middle'>";
    echo "<table class='table' cellspacing='1' cellpadding='2' width='50%' height='120' align='center' border='0'>";
    echo "<tr><td height='30' class='bg_tr'>系统提示</td></tr>";
    echo "<tr><td class='td_bg' align='center'>正在向第".($_GET['page']+1)."批用户发送。(共".($_GET['count1']+1)."批)<br></td></tr>";
    echo "</table></td></tr></table></body></html>";
    exit();
  }
  elseif($_POST['type']==2)
  {
    $phpcolor->table="user";
    $phpcolor->field="email";
    $phpcolor->where="username = '".$_POST['username']."'";
    $user=$phpcolor->exist(1);
    if($user['email']=="")
      jump("jump.php?id=1&jump=email.php");
	$smtp = new smtp($core_stmpserver,$core_stmpport,true,$core_stmpacc,$core_stmppwd);
    $smtp->debug =  false;
    $smtp->sendmail($user['email'], $core_stmpmail, $_POST['email_zt'], $email_nr, 'HTML');
  }
  else
  {
    if($_POST['emaildz']==1)
      jump("jump.php?id=2&jump=email.php");
    $smtp = new smtp($core_stmpserver,$core_stmpport,true,$core_stmpacc,$core_stmppwd);
    $smtp->debug =  false;
    $smtp->sendmail($_POST['emaildz'], $core_stmpmail, $_POST['email_zt'], $email_nr, 'HTML');
  }
  jump("jump.php?id=24&jump=email.php");
}
if(isset($_GET['action']) and $_GET['action'] == "message")
{
  if($_POST['email_zt']=="" || $_POST['email_nr']=="")
    jump("jump.php?id=2&jump=email.php");
  if($_POST['type']==1)
  {  
  }
  else
  {
	$phpcolor->table="user";
    $phpcolor->field="*";
    $phpcolor->where="username = '".$_POST['username']."'";
    if(!$phpcolor->exist(2))
      jump("jump.php?id=1&jump=email.php");
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
    $phpcolor->message($_POST['email_nr'],$email_addresser,$_POST['username'],$time,2);
  }
  jump("jump.php?id=24&jump=email.php");
}
$tpl -> display("email.htm");
?>