1:将要发送的邮件加入到队列mailcron,mailqueue
//检查是否存在当前队列
$cid = 0;
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mailcron')." WHERE email='$email' LIMIT 1");
if($value = $_SGLOBAL['db']->fetch_array($query)) {
$cid = $value['cid'];
} else {
$cid = inserttable('mailcron', array('email'=>$email), 1);
}
if($cid) {
//插入邮件内容队列
$setarr = array(
'cid' => $cid,
'subject' => "product invite",
'message' => "product invite",
'dateline' => $_SGLOBAL['timestamp']
);
inserttable('mailqueue', $setarr);
}
2:使用source/do_sendmail.php发送邮件,ssetcookie('sendmail', '1', 300);//用户每5分钟调用本程序
ssetcookie('sendmail', '1', 300);//用户每5分钟调用本程序
$lockfile = S_ROOT.'./data/sendmail.lock';
@$filemtime = filemtime($lockfile);
if($_SGLOBAL['timestamp'] - $filemtime < 5) exit();
touch($lockfile);
//防止超时
set_time_limit(0);
//获取发送队列
$list = $sublist = $cids = $touids = array();
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mailcron')." WHERE sendtime<='$_SGLOBAL[timestamp]' ORDER BY sendtime LIMIT 0,$pernum");
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
if($value['touid']) $touids[$value['touid']] = $value['touid'];
$cids[] = $value['cid'];
$list[$value['cid']] = $value;
}
if(empty($cids)) exit();
//邮件内容
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mailqueue')." WHERE cid IN (".simplode($cids).")");
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
$sublist[$value['cid']][] = $value;
}
//开始发送
include_once(S_ROOT.'./source/function_sendmail.php');
foreach ($list as $cid => $value) {
$mlist = $sublist[$cid];
if($value['email'] && $mlist) {
$subject = getstr($mlist[0]['subject'], 80, 0, 0, 0, 0, -1);
$message = '';
foreach ($mlist as $subvalue) {
if($subvalue['message']) {
$message .= "<br><strong>$subvalue[subject]</strong><br>$subvalue[message]<br>";
} else {
$message .= $subvalue['subject'].'<br>';
}
}
if(!sendmail($value['email'], $subject, $message)) {
runlog('sendmail', "$value[email] sendmail failed.");
}
}
}