作者 张关杰

Merge remote-tracking branch 'origin'

Conflicts:
	cmd/sync_to_es_v2.php
1 -<?php  
2 -  
3 -  
4 -  
5 -include_once "../vendor/autoload.php";  
6 -  
7 -// 查询不是预热邮箱  
8 -$eids = db()->throw()->all('SELECT `emails`.`id` FROM `emails` LEFT JOIN `hot_mail` ON `emails`.`email` = `hot_mail`.`email` WHERE `hot_mail`.`email` is NULL AND `pwd_error` = 0');  
9 -  
10 -$eids = array_column($eids,'id');  
11 -  
12 -foreach ($eids as $eid){  
13 - // 查询文件夹  
14 - $fids = db()->all(\Model\folderSql::all($eid,'`id`'));  
15 - array_map(function ($fid){  
16 - // 总数  
17 - $folder['exsts'] = db()->count(\Model\listsSql::listCount(  
18 - dbWhere(['folder_id'=> $fid, 'deleted' => 0])  
19 - ));  
20 - // 未读  
21 - $folder['unseen'] = db()->count(\Model\listsSql::listCount(  
22 - dbWhere(['folder_id'=> $fid, 'seen' => 0, 'deleted' => 0])  
23 - ));  
24 -  
25 - _echo('fid: '.$fid.' data: '.json_encode($folder));  
26 -  
27 - db()->update(\Model\folderSql::$table,$folder,'`id` = '.$fid,false);  
28 -  
29 - },array_column($fids,'id'));  
30 -}  
31 -_echo('完成');  
32 -  
1 -<?php  
2 -  
3 -use Model\listsSql;  
4 -  
5 -  
6 -/**  
7 - * 处理预热邮箱  
8 - * @author:dc  
9 - * @time 2024/9/4 11:02  
10 - * Class HotMail  
11 - */  
12 -class HotMail {  
13 -  
14 - public function __construct(){  
15 - $this->db = db();  
16 - $this->start();  
17 - }  
18 -  
19 - /**  
20 - * shopk那边的预热邮箱  
21 - * @var array  
22 - */  
23 - private $hotEmail = [];  
24 -  
25 - /**  
26 - * @var \Lib\Db|\Lib\DbPool  
27 - */  
28 - private $db;  
29 -  
30 -  
31 - /**  
32 - * @author:dc  
33 - * @time 2024/7/18 14:04  
34 - */  
35 - private function start(){  
36 - _echo('启动预热邮件处理 ');  
37 -  
38 - if(redis()->add('hot_mail_sync2',1,60)){  
39 - _echo( '正在计算数据');  
40 - $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");  
41 - foreach (minMaxToArray($maxId-50000,$maxId) as $ids){  
42 - redis()->rPush('hot_check_ids',implode(',',$ids));  
43 - }  
44 - _echo( '计算完成');  
45 - redis()->delete('hot_mail_sync2');  
46 - }  
47 -  
48 -  
49 - while (1){  
50 - $ids = redis()->lPop('hot_check_ids');  
51 - if($ids){  
52 - $ids = explode(',',$ids);  
53 - $this->run($ids);  
54 - }else{  
55 - break;  
56 - }  
57 - }  
58 -  
59 -  
60 - }  
61 -  
62 -  
63 - private $folder = [];  
64 -  
65 - private function run($id){  
66 - $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'is_hots'=>0]),'`id`,`from`,`to`,`folder_id`,`is_hots`'));  
67 - foreach ($list as $item){  
68 - if(empty($this->folder[$item['folder_id']])){  
69 - $this->folder[$item['folder_id']] = folderAlias($this->db->value(\Model\folderSql::first($item['folder_id'],'folder')));  
70 - }  
71 -  
72 - if(!in_array($this->folder[$item['folder_id']],['收件箱','发件箱'])){  
73 - continue;  
74 - }  
75 -  
76 - // 是否是发件箱  
77 - if($this->folder[$item['folder_id']] == '发件箱'){  
78 - $w = ['email' => array_map('strtolower',explode(',',$item['to']))];  
79 - }else{  
80 - $w = ['email' =>strtolower($item['from'])];  
81 - }  
82 - // 是否在 预热邮箱中  
83 - $mkey = md5(dbWhere($w));  
84 - if(!isset($this->hotEmail[$mkey])){  
85 - $this->hotEmail[$mkey] = $this->db->count('select count(*) from `hot_mail` where '.dbWhere($w));  
86 - }  
87 -  
88 - if($this->hotEmail[$mkey]){  
89 - if(!$item['is_hots']){  
90 - $ret = $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$item['id']]));  
91 - echo date('d H:i:s').' +==》 '.$item['id'].':'.$ret."\n";  
92 - }  
93 -  
94 - }else{  
95 -// if($item['is_hots']){  
96 -// $ret = $this->db->update(listsSql::$table,['is_hots'=>0],dbWhere(['id'=>$item['id']]));  
97 -// echo date('d H:i:s').' -==》 '.$item['id'].':'.$ret."\n";  
98 -// }  
99 - }  
100 - }  
101 -  
102 - // $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'is_hots'=>1]),'`id`'));  
103 - // foreach ($list as $item){  
104 - // redis()->rPush('sync_to_es',$item['id']);  
105 - // }  
106 - }  
107 -  
108 -  
109 -}  
110 -  
111 -\Co\run(function () {  
112 - include_once "../vendor/autoload.php";  
113 -  
114 - new HotMail();  
115 -});  
116 -  
117 -//  
118 -//swoole_set_process_name('hot-email-run-man');  
119 -//  
120 -//$pm = new Swoole\Process\Manager();  
121 -//  
122 -//$pm->addBatch(3,function (){  
123 -//  
124 -// swoole_set_process_name('hot-email-run');  
125 -//  
126 -// include_once "../vendor/autoload.php";  
127 -//  
128 -// new HotMail();  
129 -//  
130 -// exit();  
131 -//},true);  
132 -//  
133 -//$pm->start();  
134 -  
135 -  
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 $black_emails = [];  
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 - $this->black_emails = $this->db->all("select * from `ai_black_email`");  
43 - $this->black_emails = array_column($this->black_emails,'email');  
44 -  
45 - $this->black_emails = array_map("strtolower",$this->black_emails);  
46 -  
47 -  
48 - if(redis()->add('black_mail_sync2',1,60)){  
49 - echo '正在计算数据';  
50 - $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");  
51 - $id = 0;  
52 - while (1){  
53 - $ids = [];  
54 - for ($i=0;$i<1000;$i++){  
55 - $ids[] = $i+$id;  
56 - }  
57 - $id = end($ids);  
58 -  
59 - redis()->rPush('black_check_ids',implode(',',$ids));  
60 -  
61 - if($id>$maxId){  
62 - break;  
63 - }  
64 - }  
65 - echo '计算完成';  
66 - }  
67 -  
68 -  
69 - while (1){  
70 - $ids = redis()->lPop('black_check_ids');  
71 - if($ids){  
72 - $ids = explode(',',$ids);  
73 - $this->run($ids);  
74 - }else{  
75 - echo '等待'.PHP_EOL;  
76 - co::sleep(2);  
77 - }  
78 - }  
79 -  
80 - }  
81 -  
82 - private $filter = [];  
83 -  
84 - private function run($id){  
85 - $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'deleted'=>0]),'`id`,`from`,`folder_id`,`email_id`'));  
86 - foreach ($list as $item){  
87 - // 必须是收件箱  
88 - if(in_array($item['folder_id'],$this->fids)){  
89 - // 是否在黑名单中  
90 - if(in_array(strtolower($item['from']),$this->black_emails)){  
91 - if (!isset($this->filter[$item['email_id']])){  
92 - // 找到邮箱  
93 - $e = $this->db->value(\Model\emailSql::first($item['email_id'],'`email`'));  
94 - // 是否是ai邮件  
95 - $this->filter[$item['email_id']] =  
96 - $this->db->count("select count(*) from `hot_mail` where ".dbWhere(['email'=> $e]));  
97 - }  
98 -  
99 - // 是否是ai邮件  
100 - if($this->filter[$item['email_id']]){  
101 -  
102 - echo '删除 '.$item['id'].'===>'.$this->db->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>$item['id']]))."\n";  
103 -  
104 - }  
105 -  
106 -  
107 - }  
108 -  
109 - }  
110 -  
111 - }  
112 - }  
113 -  
114 -  
115 -  
116 -}  
117 -  
118 -  
119 -  
120 -$pm = new Swoole\Process\Manager();  
121 -  
122 -$pm->addBatch(3,function (){  
123 -  
124 -  
125 - include_once "../vendor/autoload.php";  
126 -  
127 -// while(1){  
128 - new AutoMail();  
129 - // 执行完了暂停5分钟在执行  
130 - sleep(40);  
131 -// }  
132 -  
133 -},true);  
134 -  
135 -$pm->start();  
136 -  
137 -  
@@ -63,7 +63,7 @@ class SyncToEsCmd { @@ -63,7 +63,7 @@ class SyncToEsCmd {
63 63
64 if($is_auto) return 1; 64 if($is_auto) return 1;
65 65
66 - return isAiAutoMail($data['from'],$data['subject']) === 1 ? 1 : 0; 66 + return isAiAutoMail($data['from'],$data['subject'],$data['body']??'') === 1 ? 1 : 0;
67 67
68 } 68 }
69 69
@@ -94,11 +94,20 @@ class SyncToEsCmd { @@ -94,11 +94,20 @@ class SyncToEsCmd {
94 $id = redis()->lPop('sync_to_es'); 94 $id = redis()->lPop('sync_to_es');
95 $code = 500; 95 $code = 500;
96 if($id){ 96 if($id){
97 - // redis()->rPush('sync_to_es2',$id); 97 +
  98 + $is_check_body = false;
  99 + if(str_contains($id, '.')){
  100 + $id = explode('.',$id)[0];
  101 + $is_check_body = true;
  102 + }
  103 +
98 104
99 $doc_id = ''; 105 $doc_id = '';
100 try { 106 try {
101 $data = $this->db->throw()->first(\Model\listsSql::first('`id` = '.$id)); 107 $data = $this->db->throw()->first(\Model\listsSql::first('`id` = '.$id));
  108 + if(!$data){
  109 + $data = $this->db->throw()->first(\Model\listsSql::firstHot('`id` = '.$id));
  110 + }
102 }catch (Throwable $e){ 111 }catch (Throwable $e){
103 redis()->rPush('sync_to_es',$id); 112 redis()->rPush('sync_to_es',$id);
104 _echo('sync to es '.$e->getMessage()); 113 _echo('sync to es '.$e->getMessage());
@@ -117,6 +126,14 @@ class SyncToEsCmd { @@ -117,6 +126,14 @@ class SyncToEsCmd {
117 126
118 // 是否是自动回复 127 // 是否是自动回复
119 if($data['folder_as_int'] === 1){ 128 if($data['folder_as_int'] === 1){
  129 + // 是否检查body
  130 + if($is_check_body){
  131 + $body = getMailBody($data['id'],$this->db);
  132 + if($body){
  133 + $data['body'] = getBodyHtml($body);
  134 + }
  135 + }
  136 +
120 $data['is_auto'] = $this->isAuto($data); 137 $data['is_auto'] = $this->isAuto($data);
121 } 138 }
122 139
@@ -995,19 +995,7 @@ class Home extends Base { @@ -995,19 +995,7 @@ class Home extends Base {
995 // 是否再次 重新获取 995 // 是否再次 重新获取
996 $data['allowreply'] = db()->value(folderSql::first(['id'=>$data['folder_id']],'folder'))!='发件箱'?1:0; 996 $data['allowreply'] = db()->value(folderSql::first(['id'=>$data['folder_id']],'folder'))!='发件箱'?1:0;
997 997
998 - $body = db()->first(bodySql::first($id));  
999 - if(empty($body['text_html'])){  
1000 - $body = db()->first("select * from `bodies_back` where `lists_id` = ".$id." limit 1");  
1001 - if(empty($body['text_html'])){  
1002 - $body = @file_get_contents('http://172.19.0.5:9527?id='.$id);  
1003 - if($body){  
1004 - $body = ['lists_id'=>$id,'text_html'=>$body];  
1005 - }  
1006 - }  
1007 - }  
1008 - if(empty($body['text_html'])){  
1009 - $body = [];  
1010 - } 998 + $body = getMailBody($id);
1011 if($body && !$reload){ 999 if($body && !$reload){
1012 $data['body'] = json_decode($body['text_html'],true); 1000 $data['body'] = json_decode($body['text_html'],true);
1013 $htmlbody = ''; 1001 $htmlbody = '';
@@ -292,8 +292,6 @@ class MailListV2Es extends Base { @@ -292,8 +292,6 @@ class MailListV2Es extends Base {
292 $v['deleted'] = $info['deleted']; 292 $v['deleted'] = $info['deleted'];
293 } 293 }
294 294
295 - // 手动触发同步es  
296 - // if(empty($v['is_auto']) && $v['folder_name']=='收件箱') redis()->rPush('sync_to_es',$v['id']);  
297 295
298 return $v; 296 return $v;
299 },$lists?:[]); 297 },$lists?:[]);
@@ -230,7 +230,7 @@ class MailListV2Es2 extends Base { @@ -230,7 +230,7 @@ class MailListV2Es2 extends Base {
230 // 手动触发同步es 230 // 手动触发同步es
231 // 手动触发同步es 231 // 手动触发同步es
232 if(empty($v['is_auto']) && $v['folder_name']=='收件箱' && isAiAutoMail($v['from'],$v['subject'])) redis()->rPush('sync_to_es',$v['id']); 232 if(empty($v['is_auto']) && $v['folder_name']=='收件箱' && isAiAutoMail($v['from'],$v['subject'])) redis()->rPush('sync_to_es',$v['id']);
233 - // redis()->rPush('sync_to_es',$v['id']); 233 +
234 234
235 return $v; 235 return $v;
236 },$lists?:[]); 236 },$lists?:[]);
1 <?php 1 <?php
2 2
  3 +use Model\bodySql;
3 use Model\emailSql; 4 use Model\emailSql;
4 5
5 6
@@ -503,6 +504,8 @@ function isAiAutoMail($from,$subject,$body=''){ @@ -503,6 +504,8 @@ function isAiAutoMail($from,$subject,$body=''){
503 $haystack = $subject; 504 $haystack = $subject;
504 }elseif ($t==1){ 505 }elseif ($t==1){
505 $haystack = $from; 506 $haystack = $from;
  507 + }elseif ($t==3&&$body){
  508 + $haystack = $body;
506 } 509 }
507 if($haystack && $str && stripos($haystack,$str)!==false){ 510 if($haystack && $str && stripos($haystack,$str)!==false){
508 return 1; 511 return 1;
@@ -769,4 +772,58 @@ function minMaxToArray($min, $max,$len=1000) { @@ -769,4 +772,58 @@ function minMaxToArray($min, $max,$len=1000) {
769 */ 772 */
770 function es(string $index='email_lists_copy'){ 773 function es(string $index='email_lists_copy'){
771 return (new Lib\Es\Es($index,['https://ai-email:3mLbEKwDX9YjUDFm@172.19.0.56:9200'])); 774 return (new Lib\Es\Es($index,['https://ai-email:3mLbEKwDX9YjUDFm@172.19.0.56:9200']));
  775 +}
  776 +
  777 +
  778 +/**
  779 + * 读取邮件详情内容
  780 + * @param $id
  781 + * @param null $db
  782 + * @return array|false|mixed|string|null
  783 + * @author:dc
  784 + * @time 2025/6/23 11:24
  785 + */
  786 +function getMailBody($id,$db=null){
  787 + $db = $db ? $db : db();
  788 + $body = $db->first(bodySql::first($id));
  789 + if(empty($body['text_html'])){
  790 + $body = $db->first("select * from `bodies_back` where `lists_id` = ".$id." limit 1");
  791 + if(empty($body['text_html'])){
  792 + $body = @file_get_contents('http://172.19.0.5:9527?id='.$id);
  793 + if($body){
  794 + $body = ['lists_id'=>$id,'text_html'=>$body];
  795 + }
  796 + }
  797 + }
  798 + if(empty($body['text_html'])){
  799 + $body = [];
  800 + }
  801 + return $body;
  802 +}
  803 +
  804 +/**
  805 + * 获取邮件内容body体
  806 + * @param $body 邮件数据 表里面的
  807 + * @return false|string
  808 + * @author:dc
  809 + * @time 2025/6/23 11:33
  810 + */
  811 +function getBodyHtml($body){
  812 + if(!empty($body['text_html'])){
  813 + $body = $body['text_html'];
  814 + }
  815 + $body = is_array($body) ? $body : json_decode($body,true);
  816 + $html = '';
  817 + $text = '';
  818 + foreach ($body as $bd){
  819 + if(($bd['type']??'') == 'text/html'){
  820 + $html = @base64_decode($bd['body']??'');
  821 + }
  822 + if(($bd['type']??'') == 'text/plain'){
  823 + $text = @base64_decode($bd['body']??'');
  824 + }
  825 + }
  826 +
  827 + return $html ? $html : $text;
  828 +
772 } 829 }