使用redis实现简单的邮件发送队列 这里使用的阻塞型blpop
直接上代码:
private function redisHandle(){ $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth("111111"); return $redis; } /** * @desc 添加邮件测试数据 */ public function addEmails(){ $content = array('1','2','3','4','5','6','7','8','9','10'); $redis = $this->redisHandle(); foreach($content as $k => $v){ $emailDetail = json_encode(array( 'to' => '1173240549@qq.com', 'name' => 'test', 'subject' => $v, 'body' => $v )); dump($redis->rpush("emailList", $emailDetail)); } } /** * @desc 检出队列 */ public function checkQueue(){ $lastTime = time(); $redis = $this->redisHandle(); while(1){ if($data = $redis->blPop('emailList', 5)){ $value = json_decode($data[1], true); $result = think_send_mail($value['to'], $value['name'], $value['subject'], $value['body']); if(!$result){ echo date('Y-m-d H:i:s', time()).'-->'.$value['subject']."发送失败!"; $redis->rpush("emailList", $value); }else{ echo date('Y-m-d H:i:s', time()).'-->'.$value['to'].':'.$value['subject']."发送成功\r\n"; echo '剩余'.$redis->lLen('emailList')."\r\n"; } }else{ if(time() - $lastTime >= 5){ echo date('Y-m-d H:i:s', time()).'-->'.'超时。。'."\r\n"; break; } } } } 最后根据你的需求放到定时任务里去执行