<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[34]!=1)
{
  Header("Location: jump.php?id=25&jump=main.php");
  exit();
}
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']=="")
  {
    Header("Location: jump.php?id=2&jump=email.php");
	exit();
  }
  else
  {
    $email_nr=$_POST['email_nr'];
    if($_POST['type']==1)
	{
	  $phpcolor->table=$core_mysql_prefix."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*60*60+36000);
	    setcookie("emailnr", $_POST['email_nr'], time()+$core_time*60*60+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'])
	  {
	    Header("Location: jump.php?id=24&jump=email.php");
        exit();
	  }
	  else
	  {
	    Header("refresh:3;url=email.php?action=submit&count1=".$_GET['count1']."&page=".($_GET['page']+1));
	    echo "正在向第".($_GET['page']+1)."批用户发送。(共".($_GET['count1']+1)."批)";
        exit();
	  }
	}
	elseif($_POST['type']==2)
	{
	  $phpcolor->table=$core_mysql_prefix."user";
      $phpcolor->field="email";
      $phpcolor->where="username = '".$_POST['username']."'";
	  $user=$phpcolor->exist(1);
	  if($user['email']=="")
	  {
	    Header("Location: jump.php?id=1&jump=email.php");
        exit();
	  }
	  else
	  {
	    $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)
	  {
	    Header("Location: jump.php?id=2&jump=email.php");
        exit();
	  }
	  else
	  {
	    $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');
	  }
	}
    Header("Location: jump.php?id=24&jump=email.php");
  }
}
else
{
  $tpl -> display("email.htm");
}
?>