作者 邓超

x

<?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 $black_emails = [];
/**
* @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');
$this->black_emails = $this->db->all("select * from `ai_black_email`");
$this->black_emails = array_column($this->black_emails,'email');
if(redis()->add('black_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);
redis()->rPush('black_check_ids',implode(',',$ids));
if($id>$maxId){
break;
}
}
echo '计算完成';
}
while (1){
$ids = redis()->lPop('black_check_ids');
if($ids){
$ids = explode(',',$ids);
$this->run($ids);
}else{
echo '等待'.PHP_EOL;
co::sleep(2);
}
}
}
private $filter = [];
private function run($id){
$list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'deleted'=>0]),'`id`,`from`,`folder_id`,`email_id`'));
foreach ($list as $item){
// 必须是收件箱
if(in_array($item['folder_id'],$this->fids)){
// 是否在黑名单中
if(in_array($item['from'],$this->black_emails)){
// 是否是ai邮件
if($this->db->count("select count(*) from `hot_mail` where ".dbWhere([
'email'=> $this->db->value(listsSql::first(dbWhere(['id'=>$item['email_id']]),'email'))
]))){
echo '删除 '.$item['id'].'===>'.$this->db->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>$item['id']]));
}
}
}
}
}
}
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();
... ...
... ... @@ -220,9 +220,16 @@ class syncMail {
private function black_mail($id,$data){
if(!empty($data['from'])){
if($this->db->count("select count(*) from `ai_black_email` where ".dbWhere(['email'=>$data['from']]))){
// 是否是ai邮件
if($this->db->count("select count(*) from `hot_mail` where ".dbWhere([
'email'=> $this->db->value(listsSql::first(dbWhere(['id'=>$data['email_id']]),'email'))
]))){
// 是否在黑名单中
if($this->db->count("select count(*) from `ai_black_email` where ".dbWhere(['email'=>$data['from']]))){
$this->db->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>$id]));
}
$this->db->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>$id]));
}
}
... ...