[日常] 读取队列并循环发信的脚本

<?php
/**
 * 发送群发邮件脚本
 * 查询队列内列表,循环调用smtp发送邮件
 */
$emlDir="/mnt/entmail/mass_mail/";
//读取redis数据
$socket=fsockopen("tcp://xxx",6379,$errno, $errstr);
$smtp=new Smtp();
$second=0;
$delSecond=0;
while(true){
    fwrite($socket,"rpop send_mass_mail \r\n");
    $queue='';
    $i=0;
    while (!feof($socket)) {
        $i++;
        $ln=trim(fgets($socket));
        $queue=($ln=='$-1') ? '' : $ln;
        if($i>1 || $ln=='$-1'){
            break;
        }
    }
    if(empty($queue)){
        $delSecond++;
        var_dump($second);
        sleep(1);
        //发送完之后删掉邮件文件,延迟几十秒再删
        if(is_dir($emlDir) && $delSecond>=50){ 
            rrmdir($emlDir);
            var_dump($emlDir);
        }
    }else{
        var_dump($queue);
        send($queue); 
        $delSecond=0;
    }
    $second++;
}
fclose($socket);
die;
/**
 * 取出队列内容后发送邮件
 * @param type $row
 */
function send($row){
    if(empty($row)) return;
    global $smtp; 
    list($from,$to,$eml)=  explode('|', $row);
    if(!is_file($eml)){
        return;
    }
    $to=trim($to);
    if(empty($to)){
        return;
    }
    
    $data='';
    //$eml="/tmp/2.eml";var_dump($eml);
    $file=fopen($eml,"r");
    while(!feof($file)){
        $line=fgets($file);
        $tmp=preg_replace("/^To(.*)/i","TO: {$to}", $line);
        $data.=$tmp;
    }
    //var_dump($data);die;
    //$data=file_get_contents("/tmp/2.eml");
    //$tmp=preg_replace("/\nTo[\w\W]*Subject/i","\nTO: {$to}\r\nSubject", $data);var_dump($tmp);die;
    $res=$smtp->connect("intraxxx",2025);
    $res.=$smtp->helo($from);
    $res.=$smtp->auth();
    $res.=$smtp->user();
    $res.=$smtp->pass("xxx");
    $res.=$smtp->mailFrom($from);
    $res.=$smtp->rcpt($to);
    $res.=$smtp->data();
    $res.=$smtp->send($data);
    //var_dump(error_get_last());
    //echo $res;
}
/**
 * 删除邮件文件目录
 * @param type $src
 */
function rrmdir($src) {
    $dir = opendir($src);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            $full = $src . '/' . $file;
            if ( is_dir($full) ) {
                rrmdir($full);
            }
            else {
                unlink($full);
            }
        }
    }
    closedir($dir);
    rmdir($src);
}
class Smtp{
    private $socket;
    private $email;
    public function __construct(){
    	ini_set('memory_limit', '1020M');
    	ini_set("auto_detect_line_endings", true);
    }
    public function connect($smtpServer,$smtpPort){
        $res=@fsockopen("tcp://".$smtpServer,$smtpPort,$errno, $errstr,30);
        if(!$res){
            throw new Exception($errstr, $errno);
        }
        $this->socket=$res;
        return $this->readLine();
    }
    public function helo($email){
        $user="HELO {$email}\r\n";
        fwrite($this->socket,$user);
        $this->email=$email;
		return $this->readLine();
    }
    public function auth(){
        $pass="AUTH LOGIN\r\n";
        fwrite($this->socket,$pass);
        return $this->readLine();
    }
    public function user(){
    	$pass=base64_encode($this->email)."\r\n";
    	fwrite($this->socket,$pass);
        return $this->readLine();
    }
    public function pass($pwd){
    	$pass=base64_encode($pwd)."\r\n";
    	fwrite($this->socket,$pass);
        return $this->readLine();
    }
    public function mailFrom($from){
    	$data="MAIL FROM:<{$from}>\r\n";
    	fwrite($this->socket,$data);
        return $this->readLine();
    }
    public function rcpt($rcpt){
    	$data="RCPT TO:<{$rcpt}>\r\n";
    	fwrite($this->socket,$data);
        return $this->readLine();
    }
    public function data(){
    	$email="data\r\n";
    	fwrite($this->socket,$email);
        return $this->readLine();
    }
    public function send($data){
    	$email="{$data}\r\n";
    	$email.=".\r\n";
    	fwrite($this->socket,$email);
        return $this->readLine();
    }
    public function read() {
    	$buf="";
		while ($ln = $this->readLine()) {
            if (trim($ln) == '.') {
                break;
            }
            $buf .= $ln;
        }
        return $buf;
    }
    public function readLine(){
        $result="";
        while(true){
            $buffer=@fgets($this->socket,128);
            $n = strlen($buffer);
            $result.=$buffer;
            if (!$n) {
                break;
            }
            if ($buffer[$n - 1] == "\n") {
                break;
            }
        }
        return $result;
    }

}

  

posted @ 2018-03-22 18:31  唯一客服系统开发笔记  阅读(222)  评论(0编辑  收藏  举报