<?
$sub_menu = "200300";
include_once("./_common.php");

auth_check($auth[$sub_menu], "w");

$html_title = "会员群发邮件";

check_demo();

check_token();

include_once("./admin.head.php");
include_once("$g4[path]/lib/mailer.lib.php");

$countgap = 10; // 一次发送邮件数量设定
$maxscreen = 500; // 显示多少邮件信息
$sleepsec = 200;  // 间隔

echo "<span style='font-size:9pt;'>";
echo "<p>邮件正在发送中 ...<p><font color=crimson>在显示<b>[发送完成]</b></font>前请勿关闭浏览器或离开此页面<p>";
echo "</span>";
?>

<span id="cont"></span>

<?
include_once("./admin.tail.php");
?>

<?
flush();
ob_flush();

$ma_id = trim($_POST[ma_id]); 
$select_member_list = addslashes(trim($_POST[ma_list])); 

//print_r2($_POST); EXIT;
$member_list = explode("\n", $select_member_list);

// 获取邮件内容
$sql = "select ma_subject, ma_content from $g4[mail_table] where ma_id = '$ma_id' ";
$ma = sql_fetch($sql);

$subject = $ma[ma_subject];

$cnt = 0;
for ($i=0; $i<count($member_list); $i++) 
{
    list($email, $mb_id, $name, $nick, $birth, $datetime) = explode("||", trim($member_list[$i]));

    $sw = preg_match("/[0-9a-zA-Z_]+(\.[0-9a-zA-Z_]+)*@[0-9a-zA-Z_]+(\.[0-9a-zA-Z_]+)*/", $email);
    // 检查邮件地址
    if ($sw == true) 
    {
        $cnt++;

        $mb_md5 = md5($mb_id.$email.$datetime);

        $content = $ma[ma_content];
        $content = preg_replace("/{姓名}/", $name, $content);
        $content = preg_replace("/{昵称}/", $nick, $content);
        $content = preg_replace("/{会员ID}/", $mb_id, $content);
        $content = preg_replace("/{邮件地址}/", $email, $content);
        $content = preg_replace("/{生日}/", (int)substr($birth,4,2).'月 '.(int)substr($birth,6,2).'日', $content);

        $content = $content . "<hr size=0><p><span style='font-size:9pt; font-familye:宋体'>▶ 如果您不想收到此站点发来的邮件请点击 [<a href='$g4[url]/$g4[bbs]/email_stop.php?mb_id=$mb_id&mb_md5=$mb_md5' target='_blank'>拒绝</a>] 此站受理您的请求后将不会给您再次发送类似邮件</span></p>";

        /*
        ob_start();
        include "$mail_skin/mail.skin.php";
        $content = ob_get_contents();
        ob_end_clean();
        */

        //mailer($default[de_subject], $default[de_admin_email], $email, $subject, $content, 1);
        mailer($config[cf_title], $member[mb_email], $email, $subject, $content, 1);

        echo "<script> document.all.cont.innerHTML += '$cnt. $email ($mb_id : $name)<br>'; </script>\n";
        //echo "+";
        flush();
        ob_flush();
        ob_end_flush();
        usleep($sleepsec);
        if ($cnt % $countgap == 0) 
        {
            echo "<script> document.all.cont.innerHTML += '<br>'; document.body.scrollTop += 1000; </script>\n";
        }

        // 清空画面
        if ($cnt % $maxscreen == 0)
            echo "<script> document.all.cont.innerHTML = ''; document.body.scrollTop += 1000; </script>\n";
    }
}
?>
<script> document.all.cont.innerHTML += "<br><br>合计处理 <?=number_format($cnt)?>封 邮件<br><br><font color=crimson><b>[发送完成]</b></font>"; document.body.scrollTop += 1000; </script>
