auto_reply_mail.php 3.8 KB
<?php

use Model\listsSql;


/**
 * 自动回复的邮件
 * @author:dc
 * @time 2024/9/6 17:09
 * Class HotMail
 */
class AutoMail {

    public function __construct(){
        $this->db = db();
        $this->start();
    }

    /**
     * shopk那边的预热邮箱
     * @var array
     */
    private $shopkHotEmail = [];

    /**
     * @var \Lib\Db|\Lib\DbPool
     */
    private $db;


    private $fids = [];

    /**
     * @author:dc
     * @time 2024/7/18 14:04
     */
    private function start(){

        $this->fids = $this->db->all("select `id` from `folders` where `folder` = '收件箱'");
        $this->fids = array_column($this->fids,'id');

        $filter = '2	Automatic reply
2	Delivery
2	Automatische Antwort
2	Undeliverable
2	Failure
2	Undelivered
1	noreply
1	postmaster
1	email-notifications
1	mailer-daemon
1	no-reply
2	自动回复
2	Returned mail
2	Autosvar
2	Out Of Office Re
2	Change_of_email_address
2	delivered
2	automatique
2	Reply auto
2	automatic
2	Request received
2	Automatisch
2	Unzustellbar
2	Notification
2	Invitation
2	Automatyczna
2	自動回覆
2	expired';
        $filter = explode("\n",$filter);
        foreach ($filter as $f) {
            if (!$f) continue;
            $this->filter[] = [
                intval(mb_substr($f, 0, 1)),
                trim(mb_substr($f, 1, 99)),
            ];
        }

        if(redis()->add('auto_mail_sync2',1,60)){
            echo '正在计算数据';
            $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");
            $id = 0;
            while (1){
                $ids = [];
                for ($i=0;$i<1000;$i++){
                    $ids[] = $i+$id;
                }
                $id = end($ids);
                if($id<($maxId-1000000)){
                    continue;
                }
                redis()->rPush('auto_check_ids',implode(',',$ids));

                if($id>$maxId){
                    break;
                }
            }
            echo '计算完成';
        }


        while (1){
            $ids = redis()->lPop('auto_check_ids');
            if($ids){
                $ids = explode(',',$ids);
                $this->run($ids);
            }else{
                break;
            }
        }

    }

    private $filter = [];

    private function run($id){
        $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id]),'`id`,`from`,`subject`,`folder_id`'));
        foreach ($list as $item){
            // 必须是收件箱
            if(in_array($item['folder_id'],$this->fids)){
                foreach ($this->filter as $f){
                    list($t,$str) = $f;
                    $haystack = '';
                    if($t==2){
                        $haystack = $item['subject'];
                    }elseif ($t==1){
                        $haystack = $item['from'];
                    }
                    if(stripos($haystack,$str)!==false){
                        if(!$this->db->count("select count(*) from `lists_auto` where `list_id` = ".$item['id'])){
                            echo "插入数据 ".$item['id'].'==>'.$this->db->create('lists_auto',['list_id'=>$item['id']],false).'==>'.$haystack."\n";
                        }
                        break;
                    }
                }
            }

        }
    }



}

\Co\run(function (){
    include_once "../vendor/autoload.php";

//    while(1){
    new AutoMail();
});


//swoole_set_process_name('auto-reply-run-man');
//
//$pm = new Swoole\Process\Manager();
//
//$pm->addBatch(3,function (){
//
//    swoole_set_process_name('auto-reply-email-run');
//
//    include_once "../vendor/autoload.php";
//
////    while(1){
//    new AutoMail();
//    // 执行完了暂停5分钟在执行
//    sleep(40);
////    }
//
//},true);
//
//$pm->start();