hot_mail.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
use Model\listsSql;
/**
* 只有黑格 在使用此业务
* 预热 邮件 数据处理
* 主要功能:
* 在 https://fob.ai.cc/api/mail/preheat 拉取 预热邮件
* 在 https://oa.shopk.com/api/mail/preheat 拉取 预热邮件
* 在 lists表中找到这些邮箱发出的邮件
* 然后在 hot_mail表中记录起来
* @author:dc
* @time 2024/7/18 13:50
* Class HotMail
*/
class HotMail {
public function __construct(){
$this->db = db();
$this->start();
}
/**
* shopk那边的预热邮箱
* @var array
*/
private $shopkHotEmail = [];
/**
* @var \Lib\Db|\Lib\DbPool
*/
private $db;
/**
* @author:dc
* @time 2024/7/18 14:04
*/
private function start(){
_echo('启动预热邮件处理 '.getmypid());
if(redis()->add('hot_mail_sync',1,60)){
$fob = $this->getFobHotEmail();
// 把黑格自己的也算进去
$this->shopkHotEmail = array_merge($this->getHotEmail('e'),$this->getHotEmail('w'),$fob);
$this->shopkHotEmail = array_map('strtolower',$this->shopkHotEmail);
$this->shopkHotEmail = array_flip($this->shopkHotEmail);
$this->shopkHotEmail['job@aicc-email.com'] = 1;
foreach ($this->shopkHotEmail as $e=>$i){
try {
if(!$this->db->count("select count(*) from `hot_mail` where ".dbWhere(['email'=>$e]))){
echo '插入数据 '.$e."\n";
$this->db->throw()->insert('hot_mail',['email'=>$e],false);
}
}catch (Throwable $e){}
}
// $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('hot_check_ids',implode(',',$ids));
//
// if($id>$maxId){
// break;
// }
// }
}
// while (1){
// $ids = redis()->lPop('hot_check_ids');
// if($ids){
// $ids = explode(',',$ids);
// $this->run($ids);
// }else{
// echo '等待'.PHP_EOL;
// co::sleep(2);
// }
// }
echo '已执行完成'.PHP_EOL;
}
private $folder = [];
private function run($id){
$list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'is_hots'=>0]),'`id`,`from`,`to`,`folder_id`'));
foreach ($list as $item){
if(empty($this->folder[$item['folder_id']])){
$this->folder[$item['folder_id']] = folderAlias($this->db->value(\Model\folderSql::first($item['folder_id'],'folder')));
}
// 是否是发件箱
if($this->folder[$item['folder_id']] == '发件箱'){
$w = ['email' => explode(',',$item['to'])];
}else{
$w = ['email' =>$item['from']];
}
// 是否在 预热邮箱中
if($this->db->cache(300)->count('select count(*) from `hot_mail` where '.dbWhere($w))){
$ret = $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$item['id']]));
echo date('d H:i:s').' ==》 '.$item['id'].':'.$ret."\n";
}
}
}
/**
* 获取预热邮箱
* shopk的
* @return array
* @author:dc
* @time 2024/7/18 13:58
*/
private function getHotEmail($t='e'):array {
$url['e'] = 'https://oa.shopk.com/api/mail/preheat';
$url['w'] = 'https://oa.shopk.com/api/mail/write?iswrite=1';
$data = @file_get_contents($url[$t]);
if($data){
$data = @json_decode($data,1);
if($data && isset($data['data']) && is_array($data['data'])){
// 全部转小写
return array_map("strtolower",$data['data']);
}
}
logs('shopk 获取预热邮箱错误:'.print_r($data,1),'get_hot_oa_email.error.log')->write();
return [];
}
/**
* fob黑格的预热邮箱
* @author:dc
* @time 2024/7/18 14:21
*/
private function getFobHotEmail(){
$data = @file_get_contents('https://fob.ai.cc/api/mail/preheat/'.md5('aicc.'.date('ymdh')));
if($data){
$data = @json_decode($data,1);
if($data && isset($data['data']) && is_array($data['data'])){
return $data['data'];
}
}
logs('fob 获取预热邮箱错误:'.print_r($data,1),'get_hot_fob_email.error.log')->write();
return [];
}
}
swoole_set_process_name('hot-email-run-man');
$pm = new Swoole\Process\Manager();
$pm->addBatch(1,function (){
swoole_set_process_name('hot-email-run');
include_once "../vendor/autoload.php";
new HotMail();
// 执行完了暂停5分钟在执行
sleep(120);
},true);
$pm->start();