mail_del.php
4.4 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
<?php
/**
* 循环本地,验证远程是否存在 不存在则删除本地
*/
//error_reporting();
use Swoole\Process;
function start(){
// 删除停止运行的值
// redis()->delete(SYNC_RUNNING_REDIS_KEY,'email_sync_stop_num');
// 进程管理器
$pm = new Process\Manager();
// 启动业务进程
$pm->addBatch(1,function (Process\Pool $pool, int $worker_id){
swoole_set_process_name('php-email-sync-list-check-'.$worker_id);
include_once __DIR__."/../vendor/autoload.php";
_echo("业务进程({$worker_id})启动成功");
$id = 0;
// 循环阻塞
while (true){
$id = db()->value('select `id` from `'.\Model\emailSql::$table.'` where `id` > '.$id.' order by `id` asc limit 1');
if($id){
// 占用当前的id,占用2小时
// if(redis()->add('just_sync_delete_'.$id,time(),3600)){
// 启动一个协程
go(function () use ($id){
// 开始同步
try {
sync($id);
}catch (\Throwable $e){
echo $e->getMessage();
}
\Lib\Log::getInstance()->write();
});
// }
}else{
//每次都暂停1秒,防止同一时间启动太多的任务
co::sleep(300);
break;
}
}
},true);
// 启动管理器
$pm->start();
}
/**
* 开始同步, 这里是主要的业务代码
* @param $email_id
* @param $worker_id
* @return int
* @author:dc
* @time 2023/3/10 10:19
*/
function sync($email_id){
$email = db()->first(\Model\emailSql::first($email_id));
if(!$email || $email['pwd_error']){
return 0;
}
// 读取到邮箱中的文件夹
$folders = db()->all(\Model\folderSql::all($email['id']));
if(!$folders){
return 3;
}
$mailServer = new Lib\Mail\Mail($email['email'],base64_decode($email['password']),$email['imap']);
// 登录服务器
if($mailServer->login()!==1){
return 2;
}
$call = function ($email_id,$folder_id,$origin_folder) use ($mailServer){
_echo('run e '.$email_id.' fn '.$origin_folder);
// gmail 邮箱 这个是不可选的
if($origin_folder == '[Gmail]'){
return;
}
// 同步父文件夹
$mailServer->client->selectFolder($origin_folder);
$page = 0;
$db = db();
while (1){
$ids = $db->all("select `id`,`uid` from ".\Model\listsSql::$table." where `email_id` = {$email_id} and `folder_id` = {$folder_id} limit 100 offset ".($page*100));
$page++;
if($ids){
$ids = array_column($ids,'id','uid');
try {
$result = $mailServer->client->fetch(array_keys($ids),'UID',true);
$result = array_column($result,'UID');
}catch (Throwable $e){
return 0;
}
foreach ($ids as $uid=>$id){
if(!isset($result[$uid])){
_echo('删除 e '.$email_id.' f '.$folder_id.' u '.$uid.' id '.$id.' d '.$db->delete(\Model\listsSql::$table,['id'=>$id]).' fd '.$db->delete('fob_hot_mail',['lists_id'=>$id]).' body '.$db->delete(\Model\bodySql::$table,['lists_id'=>$id]));
// 删除 如果远程没有,就删除本地
}
}
}
// 结束了
if(count($ids) < 100){
break;
}
}
};
// $folders = list_to_tree($folders);
foreach ($folders as $folder){
try {
if(empty($folder['_child'])){
$call($email_id,$folder['id'],$folder['origin_folder']);
}else{
foreach ($folder['_child'] as $item){
// 同步子文件夹
$call($email_id,$item['id'],$item['origin_folder']);
}
}
}catch (\Throwable $e){
logs(
$e->getMessage().$e->getTraceAsString(),
LOG_PATH.'/imap/'.$email['email'].'.error.log'
);
}
}
$email = null;
$mailServer = null;
}
if(!function_exists("imap_8bit")){
echo '请安装imap扩展';
exit(0);
}
start();