zoukankan      html  css  js  c++  java
  • [日常] 读取队列并循环发信的脚本

    <?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 
    ");
        $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("/
    To[wW]*Subject/i","
    TO: {$to}
    Subject", $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}
    ";
            fwrite($this->socket,$user);
            $this->email=$email;
    		return $this->readLine();
        }
        public function auth(){
            $pass="AUTH LOGIN
    ";
            fwrite($this->socket,$pass);
            return $this->readLine();
        }
        public function user(){
        	$pass=base64_encode($this->email)."
    ";
        	fwrite($this->socket,$pass);
            return $this->readLine();
        }
        public function pass($pwd){
        	$pass=base64_encode($pwd)."
    ";
        	fwrite($this->socket,$pass);
            return $this->readLine();
        }
        public function mailFrom($from){
        	$data="MAIL FROM:<{$from}>
    ";
        	fwrite($this->socket,$data);
            return $this->readLine();
        }
        public function rcpt($rcpt){
        	$data="RCPT TO:<{$rcpt}>
    ";
        	fwrite($this->socket,$data);
            return $this->readLine();
        }
        public function data(){
        	$email="data
    ";
        	fwrite($this->socket,$email);
            return $this->readLine();
        }
        public function send($data){
        	$email="{$data}
    ";
        	$email.=".
    ";
        	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] == "
    ") {
                    break;
                }
            }
            return $result;
        }
    
    }
    

      

  • 相关阅读:
    jmap、jhat、jstat、jstack
    jconsole、java VisualVM、jprofiler
    最长递增子序列 dp
    我的眼泪流下来
    理解二级指针的好例子
    离散数学复习————二元关系
    linux的常用命令
    初学Java
    分治法 ----归并排序
    1074 Reversing Linked List
  • 原文地址:https://www.cnblogs.com/taoshihan/p/8625573.html
Copyright © 2011-2022 走看看