hot_mail.php
6.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* 只有黑格 在使用此业务
* 预热 邮件 数据处理
* 主要功能:
* 在 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->start();}
/**
* shopk那边的预热邮箱
* @var array
*/
private $shopkHotEmail = [];
/**
* @author:dc
* @time 2024/7/18 14:04
*/
private function start(){
_echo('启动预热邮件处理 '.getmypid());
$this->shopkHotEmail = $this->getHotEmail();
if($this->shopkHotEmail){
array_map([$this,'moveMail'],$this->getFobHotEmail());
}
}
/**
* 邮件检查及移动
* @param string $email 检查的邮箱
* @author:dc
* @time 2024/7/18 14:06
*/
private function moveMail(string $email){
// 读取邮箱在表里的id
$email_id = db()->value(sprintf("select `id` from `%s` where `email` = '%s' limit 1",\Model\emailSql::$table, $email));
if($email_id){
$this->findFolder($email_id,'收件箱','s');
$this->findFolder($email_id,'发件箱','f');
}
return true;
}
/**
* 查找文件夹
* @param int $email_id 邮箱id
* @param string $folder 那个文件夹
* @author:dc
* @time 2024/7/18 15:10
*/
private function findFolder(int $email_id, string $folder,string $type){
// 查询 文件夹
$folders = db()->all(\Model\folderSql::all($email_id,'`id`,`folder`'));
foreach ($folders as $f){
if(folderAlias($f['folder']) == $folder){
// 最后记录的id
$last_id = db()->value('select max(`lists_id`) from `fob_hot_mail` where `email_id` = '.$email_id.' limit 1');
$last_id = $last_id?$last_id:0;
$this->findList(new Data($email_id,$f['id'],$type,$last_id));
}
}
return true;
}
/**
* 查找 邮件
* @author:dc
* @time 2024/7/18 15:11
*/
private function findList(Data $data, int $p = 1){
$lists = db()->all(
sprintf(
"select `id`,`folder_id`,`email_id`,`%s` from `lists` where `id` > %d and `email_id` = %d and `folder_id` = %d and (select count(*) from `fob_hot_mail` where `lists`.`id` = `fob_hot_mail`.`lists_id`) = 0 order by `id` asc limit 1000 offset ".(($p-1)*1000),
$data->type=='s' ? 'from':'to_name', // 收件箱查询 from字段 发件箱 查询 to_name字段
$data->last_id,
$data->email_id,
$data->folder_id
)
);
if($lists){
// 处理数据
foreach ($lists as $list){
$list['folder'] = $data->type;
$this->insertData($list);
}
$lists = null;
$list = null;
// 再次查找
$this->findList($data,$p + 1);
}
return true;
}
/**
* 插入数据
* @param array $data
* @author:dc
* @time 2024/7/18 15:25
*/
private function insertData(array $data){
$isInsert = false;
if($data['folder'] == 's'){
// 收件箱 匹配 from 字段
$isInsert = in_array(strtolower($data['from']),$this->shopkHotEmail);
}else{
// 发件箱 匹配 to_name字段
$to_name = is_array($data['to_name']) ? $data['to_name'] : @json_decode($data['to_name'],1);
if($to_name){
foreach ($to_name as $item){
// 找到了就退出去
if(in_array(strtolower($item['email']),$this->shopkHotEmail)){
$isInsert = true;
break;
}
}
}
}
// 是预热 邮箱发来的邮件
if($isInsert){
// 不存在 数据
if(db()->count("select count(*) from `fob_hot_mail` where `lists_id` = ".$data['id']) == 0){
db()->insert('fob_hot_mail',[
'lists_id' => $data['id'],
'email_id' => $data['email_id'],
'hot_form' => $data['from'],
'folder' => $data['folder']
],false);
}
}
return true;
}
/**
* 获取预热邮箱
* shopk的
* @return array
* @author:dc
* @time 2024/7/18 13:58
*/
private function getHotEmail():array {
$data = @file_get_contents('https://oa.shopk.com/api/mail/preheat');
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();
}
}
class Data {
public int $email_id = 0;
public int $folder_id = 0;
public string $type = '';
public int $last_id = 0;
public function __construct(int $email_id,int $folder_id, string $type,int $last_id)
{
$this->email_id = $email_id;
$this->folder_id = $folder_id;
$this->type = $type;
$this->last_id = $last_id;
}
}
swoole_set_process_name('hot-email-run-man');
$pm = new Swoole\Process\Manager();
$pm->add(function (){
swoole_set_process_name('hot-email-run');
include_once "../vendor/autoload.php";
new HotMail();
// 执行完了暂停5分钟在执行
sleep(300);
});
$pm->start();