SyncMail.php
16.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
namespace Service;
use Event\Event;
use Lib\Imap\Fun;
use Lib\Imap\Imap;
use Lib\Imap\ImapConfig;
use Lib\Imap\ImapPool;
use Lib\Imap\ImapSearch;
use Lib\Imap\Parse\Folder\Folder;
use Lib\Imap\Parse\MessageItem;
use Model\bodySql;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;
/**
* 同步邮件
* @author:dc
* @time 2024/9/26 9:31
* Class SyncMail
* @package Service
*/
class SyncMail {
/**
* @var \Lib\Db|\Lib\DbPool
*/
protected $db;
/**
* @var \Lib\Imap\Imap
*/
protected $imap;
/**
* @var array
*/
protected $email;
protected $isStop = false;
/**
* @var 搜索规则
*/
protected $search = null;
/**
* SyncMail constructor.
* @param int|string|array $email
* @throws \Exception
*/
public function __construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null)
{
$this->db = db();
if(!is_array($email)){
$email = $this->db->cache(3600)->first(emailSql::first($email));
if(!$email){
abort('未查询到邮箱');
}
}
$this->email = $email;
// 实例一个imap类
if($imap instanceof \Lib\Imap\Imap){
$this->imap = $imap;
}else{
$this->imap = new Imap(
(new ImapConfig())
->setHost($email['imap'])
->setEmail($email['email'])
->setPassword(base64_decode($email['password']))
// ->debug()
);
$this->login();
}
}
public function stop(){
$this->isStop = true;
}
/**
* 搜索
* @param ImapSearch $search
* @return $this
* @author:dc
* @time 2024/11/11 17:29
*/
public function search(ImapSearch $search){
$this->search = $search;
return $this;
}
protected function emailId(){
return $this->email['id'];
}
/**
* 登录imap
* @throws \Exception
* @author:dc
* @time 2024/9/26 9:58
*/
private function login(){
$login = $this->imap->login();
if(!$login->isOk()){
foreach ([
'[ALERT] Invalid credentials (Failure)',// 登录失败
'[AUTHENTICATIONFAILED] Invalid credentials (Failure)',// 登录失败
'[AUTHENTICATIONFAILED] Authentication failed.',// 登录失败 权限
'LOGIN Login error',// 登录失败
'LOGIN auth error',// 登录失败
'ERR.LOGIN.PASSERR',// 登录失败 密码错误
'Login fail.',// 登录失败
'LOGIN failed.', // 登录失败
// 'NO ERR.LOGIN.REQCODE', // 未知错误
'[ALERT] Application-specific password', // 这个错误是没有提供特定的授权码
'LOGIN Login error, user name or password error'
] as $em){
if(str_contains($login->getMessage(), $em)){
$this->db->update(
\Model\emailSql::$table,
['pwd_error'=>1],
dbWhere(['id'=> $this->emailId()])
);
}
}
abort($login->getMessage()?:'连接服务器异常');
}
}
/**
* @param $folder
* @param int $pid
* @return array
* @throws \Exception
* @author:dc
* @time 2024/9/26 10:46
*/
protected function folder($folder,$pid = 0){
$uuids = [];
foreach ($folder as $item){
/** @var Folder $item*/
$uuid = md5($this->emailId().$item->folder);
$uuids[$uuid] = $uuid;
$folder_name = '';
if($item->flags){
// 有些邮箱是把公共的文件夹标记在flag里面的,识别出来
foreach ($item->flags as $flag){
if(in_array($flag,['Send','Drafts','Junk','Trash'])){
$folder_name = folderAlias($flag);
}
}
}
if(!$folder_name){
$fn = explode('/',$item->getParseFolder());
$folder_name = folderAlias(end($fn));
}
// 是否存在
$id = $this->db->value(folderSql::has(['email_id'=>$this->emailId(),'uuid'=>$uuid]));
$data = [
'email_id' => $this->emailId(),
'folder' => $folder_name,
'origin_folder' => $item->folder,
'uuid' => $uuid,
'pid' => $pid
];
if ($id){
$this->db->update(folderSql::$table,$data,dbWhere(['id'=>$id]),false);
}else{
$id = $this->db->insert(folderSql::$table,$data,false);
if(!$id) abort('文件夹写入异常 '.json_encode($data,JSON_UNESCAPED_UNICODE));
}
// 是否有子级目录
if($id && $item->getChild()){
$uuids = array_merge($uuids,$this->folder($item->getChild(),$id));
}
}
return $uuids;
}
/**
* @param bool $syncMail
* @return bool|void|array
* @throws \Exception
* @author:dc
* @time 2024/10/18 17:53
*/
public function sync($syncMail = true){
$this->isStop = false;
/*********************************** 同步文件夹 ***************************************/
// 获取文件夹
$folders = $this->imap->getFolders();
$uuids = $this->folder($folders->getTopFolder());
if($uuids){
// 删除以前的
$this->db->delete(folderSql::$table,['uuid.notin'=>$uuids,'email_id'=>$this->emailId()]);
}
if (!$syncMail) return true;
// _echo($this->emailId().' ===> 文件夹同步成功');
if($this->isStop) return;
/********************* 同步邮件 **********************/
$syncNum = [];
// 循环文件夹
foreach ($folders->all() as $f){
if($this->isStop) return;
if($f->isSelect){ // 是否可以选择 只有可以选中的文件夹才有邮件
$folder = $this->imap->folder($f); // 选择文件夹后,有状态
// 是否有邮件 有邮件才继续
if ($folder->getTotal()){
$num = $this->mail($folder);
if($num){
$syncNum[$folder->getName()] = $num;
_echo($this->emailId().' ===> '.$folder->getName().' ===> '.$num);
}
}
// 更新数量
// $this->db->update(folderSql::$table,[
// 'exsts' => $this->db->count(listsSql::listCount(
// dbWhere(
// [
// 'folder_id'=>$this->getFolderId($folder->getName()),
// 'deleted' => 0,
// ]
// )
// )),
// 'unseen' => $this->db->count(listsSql::listCount(
// dbWhere(
// [
// 'folder_id'=>$this->getFolderId($folder->getName()),
// 'seen' => 0,
// 'deleted' => 0,
// ]
// )
// )),
// 'last_sync_time' => time()
// ],dbWhere(['email_id'=>$this->emailId(),'uuid'=>md5($this->emailId().$folder->getName())]),false);
}
}
return $syncNum;
}
/**
* 当前 目录的id
* @param string $name
* @return mixed|null
* @author:dc
* @time 2024/10/12 17:44
*/
private function getFolderId(string $name){
return $this->db->cache(120)->value(folderSql::first([
'email_id'=>$this->emailId(),
'uuid' => md5($this->emailId().$name)
],'`id`'));
}
/**同步邮件
*
* @param string|\Lib\Imap\Request\Folder $folder
* @param array $uids 固定的uid
* @param false $isBody 是否同时同步body
* @author:dc
* @time 2024/9/26 11:10
*/
public function mail(string|\Lib\Imap\Request\Folder $folder, array $uids = [],$isBody = false):int {
$sync_number = 0;
if(is_string($folder)){
$folder = $this->imap->folder($folder)->exec();
}
$folder_id = $this->getFolderId($folder->getName());
// 选择成功
if($folder->isOk()){
$msg = $folder->msg();
// 是否搜索
if ($this->search instanceof ImapSearch){
$uids = $msg->search($this->search)->getUids();
}
if($uids){
$this->saveMail($folder_id,$msg->uid($uids)->get()->all(),$isBody);
}else{
$p=1;
while (1){
if($this->isStop) return $sync_number;
$uids = $msg->forPage($p)->getOriginUids();
if($uids){
$p++;
foreach ($uids as $k=>$uid){
$has = redis()->get('h_'.$folder_id.'_'.$uid,function () use ($folder_id,$uid){
$num = $this->db->value(listsSql::first(dbWhere(['email_id'=>$this->emailId(),'folder_id'=>$folder_id,'uid'=>$uid]),'count(*) as c'));
if($num){
redis()->set('h_'.$folder_id.'_'.$uid,1,86400);
}
return $num;
});
if($has){
unset($uids[$k]);
}
}
if(!$uids) continue;
$lists = $msg->uid($uids)->get()->all();
$sync_number += count($lists);
// 没有数据就跳出
if($lists){
$this->saveMail($folder_id,$lists,$isBody);
}
}else{
break;
}
}
}
}
return $sync_number;
}
/**
* 保存邮件列表
* @param int $folder_id
* @param MessageItem[] $lists
* @param bool $isBody
* @author:dc
* @time 2024/9/29 15:14
*/
protected function saveMail(int $folder_id, array $lists, bool $isBody=false){
foreach ($lists as $item){
try {
$data = [
'uid' => $item->uid,
'subject' => mb_substr($item->header->getSubject(),0,1000),// 控制下,有的蛋疼,整tm多长
'cc' => $item->header->getCc(true),
'bcc' => $item->header->getBcc(true),
'from' => $item->header->getFrom()->email,
'from_name' => $item->header->getFrom()->name,
'to' => implode(',',array_column($item->header->getTo(true),'email')),
'to_name' => $item->header->getTo(true),
// 这个是 邮件的时间 就是header里面带的 一般情况就是发件时间
// 'date' => strtotime($item->header->getDate()),
'udate' => strtotime($item->date), // 有这个时间就够了,内部时间,就是收到邮件的时间
'size' => $item->size,
'recent' => $item->isRecent() ? 1 : 0,
'seen' => $item->isSeen() ? 1 : 0,
'draft' => $item->isDraft() ? 1 : 0,
'flagged' => $item->isFlagged() ? 1 : 0,
'answered' => $item->isAnswered() ? 1 : 0,
'folder_id' => $folder_id,
'email_id' => $this->emailId(),
'is_file' => $item->isAttachment() ? 1: 0 //是否附件
];
}catch (\Throwable $e){
logs([$e->getMessage(),$folder_id,$this->emailId(),$item->uid]);
continue;
}
$data['from'] = mb_substr($data['from'],0,120);
// 不知道为什么 有些邮件标题有下划线,但是发件那边并没有添加下划线
$data['subject'] = str_replace('_',' ',$data['subject']);
// 查询是否存在
$id = $this->db->value(listsSql::first(dbWhere([
'email_id'=> $data['email_id'],
'folder_id' => $data['folder_id'],
'uid' => $data['uid']
]),'`id`'));
if(!$id){
$id = $this->insert($data);
if(!$id){
continue;
}
// 新邮件标记
if($item->getFolderName() == 'INBOX')
redis()->incr('have_new_mail_'.$this->emailId(),120);
// 执行事件
$data['Aicc-Hot-Mail'] = $item->header->get('Aicc-Hot-Mail');
Event::call('mail_sync_list',$id, $data);
}else{
$this->db->update(listsSql::$table,$data,dbWhere(['id'=> $id]));
}
// 是否同步body内容
if($isBody && $item->body->getRaw()){
$body = [
'lists_id' => $id,
'text_html' => []
];
$body['text_html'][] = [
'body' => base64_encode($item->getBody()->getHtml() ? : $item->getBody()->getText()),
'type' => 'text/html',
'charset' => 'utf-8',
'encode' => 'base64',
];
// 处理附件
foreach ($item->getBody()->getAttachment() as $itemBody){
$tmp = [
'body' => '',
'type' => $itemBody->getFileType(),
'charset' => 'binary',
'encode' => $itemBody->data->get('content-transfer-encoding'),
'name' => $itemBody->getFilename(),
'filename' => $itemBody->getFilename(),
'path' => $itemBody->save(MAIL_ATTACHMENT_PATH)
];
if(!$tmp){
throw new \Exception('请检查附件是否有写入权限');
}
if($itemBody->getContentId()){
$tmp['content-id'] = $itemBody->getContentId();
}
if($itemBody->data->get('Content-Disposition')){
$tmp['content-disposition'] = $itemBody->data->get('Content-Disposition');
}
$tmp['signName'] = explode('/',$tmp['path']);
$tmp['signName'] = end($tmp['signName']);
$body['text_html'][] = $tmp;
}
if($this->db->count(bodySql::has($id))){
$this->db->update(bodySql::$table,$body,'`lists_id` = '.$id,false);
}else{
$this->db->insert(bodySql::$table,$body,false);
}
// 更新描述
$this->db->update(listsSql::$table,['description'=>mb_substr($item->getBody()->getText(),0,150)],dbWhere(['id'=> $id]));
}
}
}
/**
* 查询数据 并重试
* @param array $data
* @param int $num
* @return int
* @author:dc
* @time 2024/10/12 15:32
*/
protected function insert(array $data, int $num = 0){
if($num>2){
return 0;
}
try {
$id = $this->db->throw()->insert(listsSql::$table,$data);
}catch (\Throwable $e){
// 字符串编码异常
if(stripos($e->getMessage(),'Incorrect string value:')!==false){
// 编码异常的 字段
preg_match("/for column '([a-z0-9_]{2,})' at/",$e->getMessage(),$filed);
if(!empty($filed[1]) && isset($data[$filed[1]])){
// 进行编码转换 大概率会失败
$data[$filed[1]] = Fun::mb_convert_encoding($data[$filed[1]],'UTF-8');
}
$id = $this->insert($data,$num+1);
}
logs([$data,$e->getMessage()]);
}
return $id??0;
}
public function __destruct()
{
// TODO: Implement __destruct() method.
ImapPool::release($this->imap);
unset($this->imap);
}
}