uchome中的发送邮件

五月 30, 2010
1:将要发送的邮件加入到队列mailcron,mailqueue

  1. //检查是否存在当前队列   
  2.     $cid = 0;   
  3.     $query = $_SGLOBAL['db']->query(”SELECT * FROM “.tname(’mailcron’).” WHERE email=’$email’ LIMIT 1″);   
  4.     if($value = $_SGLOBAL['db']->fetch_array($query)) {   
  5.         $cid = $value['cid'];   
  6.     } else {   
  7.         $cid = inserttable(’mailcron’, array(’email’=>$email), 1);   
  8.     }   
  9.       
  10.     if($cid) {   
  11.         //插入邮件内容队列   
  12.         $setarr = array(   
  13.             ’cid’ => $cid,   
  14.             ’subject’ => “product invite”,   
  15.             ’message’ => “product invite”,   
  16.             ’dateline’ => $_SGLOBAL['timestamp']   
  17.         );   
  18.         inserttable(’mailqueue’, $setarr);   
  19.     }  

复制代码

2:使用source/do_sendmail.php发送邮件,ssetcookie(’sendmail’, ‘1′, 300);//用户每5分钟调用本程序

  1. ssetcookie(’sendmail’, ‘1′, 300);//用户每5分钟调用本程序   
  2. $lockfile = S_ROOT.’./data/sendmail.lock’;   
  3. @$filemtime = filemtime($lockfile);   
  4.   
  5. if($_SGLOBAL['timestamp'] – $filemtime < 5) exit();   
  6.   
  7. touch($lockfile);   
  8.   
  9. //防止超时   
  10. set_time_limit(0);   
  11.   
  12. //获取发送队列   
  13. $list = $sublist = $cids = $touids = array();   
  14. $query = $_SGLOBAL['db']->query(”SELECT * FROM “.tname(’mailcron’).” WHERE sendtime<=’$_SGLOBAL[timestamp]‘ ORDER BY sendtime LIMIT 0,$pernum”);   
  15. while ($value = $_SGLOBAL['db']->fetch_array($query)) {   
  16.     if($value['touid']) $touids[$value['touid']] = $value['touid'];   
  17.     $cids[] = $value['cid'];   
  18.     $list[$value['cid']] = $value;   
  19. }   
  20.   
  21. if(emptyempty($cids)) exit();   
  22.   
  23. //邮件内容   
  24. $query = $_SGLOBAL['db']->query(”SELECT * FROM “.tname(’mailqueue’).” WHERE cid IN (”.simplode($cids).”)”);   
  25. while ($value = $_SGLOBAL['db']->fetch_array($query)) {   
  26.     $sublist[$value['cid']][] = $value;   
  27. }   
  28.   
  29. //开始发送   
  30. include_once(S_ROOT.’./source/function_sendmail.php’);   
  31. foreach ($list as $cid => $value) {   
  32.     $mlist = $sublist[$cid];   
  33.     if($value['email'] && $mlist) {   
  34.         $subject = getstr($mlist[0]['subject'], 80, 0, 0, 0, 0, -1);   
  35.         $message = ”;   
  36.         foreach ($mlist as $subvalue) {   
  37.             if($subvalue['message']) {   
  38.                 $message .= “<br><strong>$subvalue[subject]</strong><br>$subvalue[message]<br>”;   
  39.             } else {   
  40.                 $message .= $subvalue['subject'].’<br>’;   
  41.             }   
  42.         }   
  43.         if(!sendmail($value['email'], $subject, $message)) {   
  44.             runlog(’sendmail’, “$value[email] sendmail failed.”);   
  45.         }   
  46.     }   
  47. }  

复制代码

Leave a Reply