作者 邓超

x

  1 +<?php
  2 +
  3 +use Model\listsSql;
  4 +
  5 +
  6 +/**
  7 + * 自动回复的邮件
  8 + * @author:dc
  9 + * @time 2024/9/6 17:09
  10 + * Class HotMail
  11 + */
  12 +class AutoMail {
  13 +
  14 + public function __construct(){
  15 + $this->db = db();
  16 + $this->start();
  17 + }
  18 +
  19 + /**
  20 + * shopk那边的预热邮箱
  21 + * @var array
  22 + */
  23 + private $shopkHotEmail = [];
  24 +
  25 + /**
  26 + * @var \Lib\Db|\Lib\DbPool
  27 + */
  28 + private $db;
  29 +
  30 +
  31 + private $fids = [];
  32 +
  33 + /**
  34 + * @author:dc
  35 + * @time 2024/7/18 14:04
  36 + */
  37 + private function start(){
  38 +
  39 + $this->fids = $this->db->all("select `id` from `folders` where `folder` = '收件箱'");
  40 + $this->fids = array_column($this->fids,'id');
  41 +
  42 +
  43 + if(redis()->add('auto_mail_sync2',1,60)){
  44 + echo '正在计算数据';
  45 + $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");
  46 + $id = 0;
  47 + while (1){
  48 + $ids = [];
  49 + for ($i=0;$i<1000;$i++){
  50 + $ids[] = $i+$id;
  51 + }
  52 + $id = end($ids);
  53 +
  54 + redis()->rPush('auto_check_ids',implode(',',$ids));
  55 +
  56 + if($id>$maxId){
  57 + break;
  58 + }
  59 + }
  60 + echo '计算完成';
  61 + }
  62 +
  63 +
  64 + while (1){
  65 + $ids = redis()->lPop('auto_check_ids');
  66 + if($ids){
  67 + $ids = explode(',',$ids);
  68 + $this->run($ids);
  69 + }else{
  70 + echo '等待'.PHP_EOL;
  71 + co::sleep(2);
  72 + }
  73 + }
  74 +
  75 + }
  76 +
  77 + private $filter = [
  78 + ['2','Automatic reply'],
  79 + ['2','Delivery'],
  80 + ['2','Automatische Antwort:'],
  81 + ['2','Automatic_reply'],
  82 + ['2','Undeliverable'],
  83 + ['2','Failure'],
  84 + ['2','Undelivered'],
  85 + ['1','noreply'],
  86 + ['1','postmaster'],
  87 + ['1','email-notifications'],
  88 + ['1','mailer-daemon'],
  89 + ['1','no-reply'],
  90 + ['2','自动回复'],
  91 + ['2','Returned mail'],
  92 + ['2','Autosvar'],
  93 + ['2','Out Of Office Re'],
  94 + ['2','Change_of_email_address'],
  95 + ['2','delivered']
  96 + ];
  97 +
  98 + private function run($id){
  99 + $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id]),'`id`,`from`,`subject`,`folder_id`'));
  100 + foreach ($list as $item){
  101 + // 必须是收件箱
  102 + if(in_array($item['folder_id'],$this->fids)){
  103 + foreach ($this->filter as $f){
  104 + list($t,$str) = $f;
  105 + $haystack = '';
  106 + if($t==2){
  107 + $haystack = $item['subject'];
  108 + }elseif ($t==1){
  109 + $haystack = $item['from'];
  110 + }
  111 + if(stripos($haystack,$str)!==false){
  112 + if(!$this->db->count("select count(*) from `lists_auto` where `list_id` = ".$item['id'])){
  113 + $this->db->insert('`lists_auto`',['list_id'=>$item['id']],false);
  114 + echo "插入数据 ".$item['id']."\n";
  115 + }
  116 + }
  117 + }
  118 + }
  119 +
  120 + }
  121 + }
  122 +
  123 +
  124 +
  125 +}
  126 +
  127 +
  128 +swoole_set_process_name('auto-reply-run-man');
  129 +
  130 +$pm = new Swoole\Process\Manager();
  131 +
  132 +$pm->addBatch(2,function (){
  133 +
  134 + swoole_set_process_name('auto-reply-email-run');
  135 +
  136 + include_once "../vendor/autoload.php";
  137 +
  138 +// while(1){
  139 + new AutoMail();
  140 + // 执行完了暂停5分钟在执行
  141 + sleep(40);
  142 +// }
  143 +
  144 +},true);
  145 +
  146 +$pm->start();
  147 +
  148 +