我已按照How to cause a redirect to occur before php script finishes?中的解决方案使页面按照计划发送电子邮件以防止超出Gmail的发送限制。循环工作,它可以在发送特定数量的电子邮件后停止。但是在加载所有代码之前无法重定向链接。如果代码运行大约6天就可以了吗?
<?php
//Redirect to messageToUser.php
header("Location: show_message.php");
//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");
//Ignore the user's abort (which we caused with the redirect).
ignore_user_abort(true);
//Extend time limit to 30 minutes
set_time_limit(0);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();
//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");
//Send the output buffer and turn output buffering off.
ob_end_flush();
//Yes... flush again.
flush();
//Close the session.
session_write_close();
/*---function start---*/
file_put_contents("gate.cfg","Y");
include 'header.php';
$foreword = $footer = "";
$send = $_SESSION['send_template']; //get newsletter information
$news_id = $send['news_id'];
if ($conn){
$sql = "select * from newsCat as n1, newsletter as n2 left outer join schedule as s on n2.news_id = s.news_id where n1.news_id = n2.news_id and n2.news_id = $news_id"; //check newsletter stage -- P:processing / F: finish / Null: not sent
$result = mysql_query($sql,$conn);
if (mysql_num_rows($result) != 0) { //get newsletter information
$array = mysql_fetch_array($result);
$subject = $array['title'];
$parent_id = $array['cat_id'];
$filename = "filemanager/file/newsletter/".$array['locat'].".txt";
$sql = "select c.cat_id,name from category as c, parent as p where p.cat_id = c.cat_id and parent_id=$parent_id"; //get category name
$result = mysql_query($sql,$conn);
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$category[$row['cat_id']] = $row['name'];
}
}
//get newsletter information
$file_id = substr($array['locat'],4,10);
$fp = fopen ($filename,"r"); //get newsletter content
$contents = fread($fp, filesize($filename));
$content = explode("<!--!@#$%^&*end-->", $contents);
for ($i=0;$i<count($content);$i++){
$content[$i] = explode("<!--!@#$%^&*-->", $content[$i]);
if ($i ==0 || $i == count($content)-1){
$code[$content[$i][0]] = $content[$i][1];
}else if ($content[$i][1] !=''){
$code[$content[$i][0]] = "<h3 style='text-align:center;'>".$category[$content[$i][0]]."</h3>".$content[$i][1];
$full_content .= $code[$content[$i][0]];
}
}
if ($code['for']!=''){
$foreword = "<p>$code[for]</p>";
}
if ($code['fot']!=''){
$footer = "<div style='font-size: 10px;border-top: 1px dashed #999;margin-top: 6px;padding-top: 6px;'>$code[fot]</a></div>";
}
$full_content = $foreword.$full_content.$footer;
if($array['processing'] == 'F'){
header("Location: show_message.php?msg=2");
}else{
if (empty($array['processing'])){ //new schedule
$sql = "create view sch_$file_id as select id,email,lname,title,count(*) as total from subscriber as s1, subscription as s2, category as c, parent as p where s1.id = s2.user_id and p.cat_id = c.cat_id and s2.cat_id = c.cat_id and parent_id = $parent_id group by id order by total desc;";
$result = mysql_query($sql,$conn);
$sql = "insert into schedule (news_id,processing) values ($news_id,'P')";
$result = mysql_query($sql,$conn);
}
while(file_get_contents("gate.cfg")=="Y"){ //start get subscriber
$sql = "select * from sch_$file_id as s1 left outer join sentRecord as s2 on s1.id= s2.user_id where news_id is null order by id limit 40";
$result2 = mysql_query($sql,$conn);
if (mysql_num_rows($result2) != 0) {
while ($row2 = mysql_fetch_array($result2, MYSQL_BOTH)) { //40 different subscribers
$body = '';
$to = $row2['email'];
$headers = "From:Biometrics Alerts <biometrics.alerts@gmail.com>\r\nMIME-Version : 1.0\r\nContent-type : text/html; charset=iso-8859-1\r\nReply-To:biometrics.alerts@gmail.com";
if ($row2['title']!='' && $row2['lname'] !=''){
$body ="<p style='text-align:right'><a href='http://www4.comp.polyu.edu.hk/~bc/preview.php?id=$location&user=$row2[id]' target='_blank'>View this email in your browser</a></p><p>Dear ".ucfirst($row2['title']).". ".ucfirst($row2['lname']).",</p>";
}
$sentppl = false;
if ($row2['total']==12){ //full subscribe
$body .= $full_content;
$sentppl = true;
}else{
$body .= $foreword;
$sql = "select cat_id from subscription where user_id = $row2[id]";
$result3 = mysql_query($sql,$conn);
while ($row3 = mysql_fetch_array($result3, MYSQL_BOTH)) {
if($code[$row3['cat_id']] !=''){
$body .= $code[$row3['cat_id']];
$sentppl = true;
$total++;
}
}
}
$body .= "$footer<hr/>If you wish to unsubscribe to <a href='http://www4.comp.polyu.edu.hk/~bc/' target='_blank'>IEEE Biometrics Compendium Alerts</a>. Please click <a href='http://www4.comp.polyu.edu.hk/~bc/unsubscribe.php?user=$row2[id]&cat=$parent_id' target='_blank'>here</a>.";
if ($sentppl){
/*send code*/
if (@mail($to,$subject,$body,$headers)){
$sql = "insert into sentRecord values ($news_id,$row2[id],'T')";
$result3 = mysql_query($sql,$conn);
}else{
$error = error_get_last();
$sql = "insert into sentRecord values ($news_id,$row2[id],'E')";
$result3 = mysql_query($sql,$conn);
$sql = "insert into errorRepord values ($news_id,$row2[id],'$error[message]')";
$result = mysql_query($sql,$conn);
}
}else{
$sql = "insert into errorRepord values ($news_id,$row2[id],'Subscriber does not subscribe in that category')";
$result = mysql_query($sql,$conn);
}
}
$sql = "select count(*) as total from sch_$file_id as s1 left outer join sentRecord as s2 on s1.id= s2.user_id where news_id is null order by id";
$result = mysql_query($sql,$conn);
$array = mysql_fetch_array($result);
if ($array['total'] < 2900){ //control number
file_put_contents("gate.cfg","N");
$sql = "drop view sch_$file_id";
$result = mysql_query($sql,$conn); //delete view
$sql = "delete from sentRecord where news_id = $news_id";
$result = mysql_query($sql,$conn); //sent record
$sql = "update schedule set processing = 'F' where news_id = $news_id";
$result = mysql_query($sql,$conn); //update record
$date = date("Y-m-d H:i:s");
$sql = "update schedule set sent_date = '$date' where news_id = $news_id";
$result = mysql_query($sql,$conn); //update record
}
}
sleep(3); // set time period
}
}
}else{
header("Location: show_message.php?msg=1");//newsletter not find
}
}else{
// database error
header("Location: show_message.php?msg=3");
}//end?>