|
@@ -239,113 +239,15 @@ class Mail { |
|
@@ -239,113 +239,15 @@ class Mail { |
239
|
|
239
|
|
240
|
// 开始同步
|
240
|
// 开始同步
|
241
|
if($uids){
|
241
|
if($uids){
|
242
|
- $results = $this->client->fetchHeader($uids,true);
|
|
|
243
|
-
|
|
|
244
|
- if($results && is_array($results)){
|
|
|
245
|
- // 表示已存在新邮件
|
|
|
246
|
- if($folder == 'INBOX') redis()->incr('have_new_mail_'.$email_id,120);
|
|
|
247
|
-
|
|
|
248
|
- // 批量插入
|
|
|
249
|
- foreach ($results as $key=>$result){
|
|
|
250
|
- $header = $result['HEADER.FIELDS'];
|
|
|
251
|
-
|
|
|
252
|
- foreach ($result['FLAGS'] as $k=>$FLAG){
|
|
|
253
|
- $result['FLAGS'][$k] = strtolower(str_replace('\\','',$FLAG));
|
|
|
254
|
- }
|
|
|
255
|
- try {
|
|
|
256
|
-
|
|
|
257
|
- // 没有收件人
|
|
|
258
|
- if(!empty($header['To'])){
|
|
|
259
|
- $header['To'] = MailFun::toOrFrom($header['To']);
|
|
|
260
|
- }else{
|
|
|
261
|
- $header['To'] = [];
|
|
|
262
|
- }
|
|
|
263
|
-
|
|
|
264
|
- $header['From'] = MailFun::toOrFrom($header['From']??'');
|
|
|
265
|
- // 抄送 ,密送
|
|
|
266
|
- $cc = [];
|
|
|
267
|
- $bcc = [];
|
|
|
268
|
- if($header['Cc']??''){
|
|
|
269
|
- $cc = MailFun::toOrFrom($header['Cc']);
|
|
|
270
|
- }
|
|
|
271
|
- if($header['Bcc']??''){
|
|
|
272
|
- $bcc = MailFun::toOrFrom($header['Bcc']);
|
|
|
273
|
- }
|
|
|
274
|
-
|
|
|
275
|
-
|
|
|
276
|
- $data = [
|
|
|
277
|
- 'uid' => $result['UID'],
|
|
|
278
|
- 'subject' => $header['Subject']??'',
|
|
|
279
|
- 'cc' => $cc,
|
|
|
280
|
- 'bcc' => $bcc,
|
|
|
281
|
- 'from' => $header['From'][0]['email']??'',
|
|
|
282
|
- 'from_name' => $header['From'][0]['name']??'',
|
|
|
283
|
- 'to' => $header['To']?implode(',',array_column($header['To'],'email')):'',
|
|
|
284
|
- 'to_name' => json_encode($header['To']),
|
|
|
285
|
- 'date' => strtotime(is_array($header['Date']??'') ? $header['Date'][0] : $header['Date']??''),
|
|
|
286
|
- 'message_id' => $header['Message-ID']??'',
|
|
|
287
|
- 'udate' => strtotime($result['INTERNALDATE']),
|
|
|
288
|
- 'size' => $result['RFC822.SIZE']??0,
|
|
|
289
|
- 'recent' => in_array('recent',$result['FLAGS']) ? 1 : 0,
|
|
|
290
|
- 'seen' => in_array('seen',$result['FLAGS']) ? 1 : 0,
|
|
|
291
|
- 'draft' => in_array('draft',$result['FLAGS']) ? 1 : 0,
|
|
|
292
|
- 'flagged' => in_array('flagged',$result['FLAGS']) ? 1 : 0,
|
|
|
293
|
- 'answered' => in_array('answered',$result['FLAGS']) ? 1 : 0,
|
|
|
294
|
- 'folder_id' => $folder_id,
|
|
|
295
|
- 'email_id' => $email_id,
|
|
|
296
|
- 'is_file' => MailFun::isFile($result['BODYSTRUCTURE']??'') ? 1: 0 //是否附件
|
|
|
297
|
- ];
|
|
|
298
|
- $data['date'] = $data['date'] ? : 0;
|
|
|
299
|
-
|
|
|
300
|
- // 验证是否存在黑名单中
|
|
|
301
|
- if($blacklist){
|
|
|
302
|
- // 邮箱是否在黑名单中
|
|
|
303
|
- $isBlacklist = false;
|
|
|
304
|
- if (!empty($blacklist['emails']) && is_array($blacklist['emails']) && in_array($data['from'],$blacklist['emails'])){
|
|
|
305
|
- $isBlacklist = true;
|
|
|
306
|
- }
|
|
|
307
|
- // 域是否存在
|
|
|
308
|
- if (!empty($blacklist['domain']) && is_array($blacklist['domain']) && in_array(explode('@',$data['from'])[1],$blacklist['domain'])){
|
|
|
309
|
- $isBlacklist = true;
|
|
|
310
|
- }
|
|
|
311
|
-
|
|
|
312
|
- if($isBlacklist && $blackFolder){
|
|
|
313
|
- // 移入垃圾箱
|
|
|
314
|
- $this->client->move($result['UID'],$blackFolder);
|
|
|
315
|
- continue;
|
|
|
316
|
- }
|
|
|
317
|
- }
|
|
|
318
|
-
|
|
|
319
|
-
|
|
|
320
|
- }catch (\Throwable $e){
|
|
|
321
|
- logs(
|
|
|
322
|
- '邮件解析失败:'.PHP_EOL.$e->getMessage().PHP_EOL.print_r($result,true),
|
|
|
323
|
- LOG_PATH.'/imap/mail/'.$email_id.'/'.$result['UID'].'.log'
|
|
|
324
|
- );
|
|
|
325
|
- unset($results[$key]);
|
|
|
326
|
- continue;
|
|
|
327
|
- }
|
|
|
328
|
-
|
|
|
329
|
- // 插入数据库
|
|
|
330
|
- try {
|
|
|
331
|
- $id = $db->insert(listsSql::$table,$data);
|
|
|
332
|
- if($id){
|
|
|
333
|
- // 同步body内容
|
|
|
334
|
- redis()->rPush('sync_email_body', [
|
|
|
335
|
- 'lists_id' => $id,
|
|
|
336
|
- 'email_id' => $email_id,
|
|
|
337
|
- 'folder_id' => $folder_id,
|
|
|
338
|
- 'folder' => $folder,
|
|
|
339
|
- 'uid' => $data['uid'],
|
|
|
340
|
- ]);
|
|
|
341
|
- }
|
|
|
342
|
- }catch (\Throwable $e){
|
|
|
343
|
-
|
|
|
344
|
- }
|
|
|
345
|
-
|
|
|
346
|
- $results[$key] = [];
|
|
|
347
|
- }
|
|
|
348
|
- }
|
242
|
+ $this->syncUidEmail(
|
|
|
243
|
+ $uids,
|
|
|
244
|
+ $email_id,
|
|
|
245
|
+ $folder,
|
|
|
246
|
+ $folder_id,
|
|
|
247
|
+ $blacklist,
|
|
|
248
|
+ $blackFolder,
|
|
|
249
|
+ $db
|
|
|
250
|
+ );
|
349
|
|
251
|
|
350
|
}
|
252
|
}
|
351
|
|
253
|
|
|
@@ -363,6 +265,129 @@ class Mail { |
|
@@ -363,6 +265,129 @@ class Mail { |
363
|
|
265
|
|
364
|
}
|
266
|
}
|
365
|
|
267
|
|
|
|
268
|
+ /**
|
|
|
269
|
+ * 同步邮件 只通过 uid获取
|
|
|
270
|
+ * @param array $uids
|
|
|
271
|
+ * @param $email_id
|
|
|
272
|
+ * @param $folder
|
|
|
273
|
+ * @param $folder_id
|
|
|
274
|
+ * @param $blacklist
|
|
|
275
|
+ * @param $blackFolder
|
|
|
276
|
+ * @param $db
|
|
|
277
|
+ * @throws \Exception
|
|
|
278
|
+ * @author:dc
|
|
|
279
|
+ * @time 2023/8/2 15:35
|
|
|
280
|
+ */
|
|
|
281
|
+ public function syncUidEmail(array $uids,$email_id,$folder,$folder_id,$blacklist,$blackFolder,$db){
|
|
|
282
|
+ $results = $this->client->fetchHeader($uids,true);
|
|
|
283
|
+
|
|
|
284
|
+ if($results && is_array($results)){
|
|
|
285
|
+ // 表示已存在新邮件
|
|
|
286
|
+ if($folder == 'INBOX') redis()->incr('have_new_mail_'.$email_id,120);
|
|
|
287
|
+
|
|
|
288
|
+ // 批量插入
|
|
|
289
|
+ foreach ($results as $key=>$result){
|
|
|
290
|
+ $header = $result['HEADER.FIELDS'];
|
|
|
291
|
+
|
|
|
292
|
+ foreach ($result['FLAGS'] as $k=>$FLAG){
|
|
|
293
|
+ $result['FLAGS'][$k] = strtolower(str_replace('\\','',$FLAG));
|
|
|
294
|
+ }
|
|
|
295
|
+ try {
|
|
|
296
|
+
|
|
|
297
|
+ // 没有收件人
|
|
|
298
|
+ if(!empty($header['To'])){
|
|
|
299
|
+ $header['To'] = MailFun::toOrFrom($header['To']);
|
|
|
300
|
+ }else{
|
|
|
301
|
+ $header['To'] = [];
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ $header['From'] = MailFun::toOrFrom($header['From']??'');
|
|
|
305
|
+ // 抄送 ,密送
|
|
|
306
|
+ $cc = [];
|
|
|
307
|
+ $bcc = [];
|
|
|
308
|
+ if($header['Cc']??''){
|
|
|
309
|
+ $cc = MailFun::toOrFrom($header['Cc']);
|
|
|
310
|
+ }
|
|
|
311
|
+ if($header['Bcc']??''){
|
|
|
312
|
+ $bcc = MailFun::toOrFrom($header['Bcc']);
|
|
|
313
|
+ }
|
|
|
314
|
+
|
|
|
315
|
+
|
|
|
316
|
+ $data = [
|
|
|
317
|
+ 'uid' => $result['UID'],
|
|
|
318
|
+ 'subject' => $header['Subject']??'',
|
|
|
319
|
+ 'cc' => $cc,
|
|
|
320
|
+ 'bcc' => $bcc,
|
|
|
321
|
+ 'from' => $header['From'][0]['email']??'',
|
|
|
322
|
+ 'from_name' => $header['From'][0]['name']??'',
|
|
|
323
|
+ 'to' => $header['To']?implode(',',array_column($header['To'],'email')):'',
|
|
|
324
|
+ 'to_name' => json_encode($header['To']),
|
|
|
325
|
+ 'date' => strtotime(is_array($header['Date']??'') ? $header['Date'][0] : $header['Date']??''),
|
|
|
326
|
+ 'message_id' => $header['Message-ID']??'',
|
|
|
327
|
+ 'udate' => strtotime($result['INTERNALDATE']),
|
|
|
328
|
+ 'size' => $result['RFC822.SIZE']??0,
|
|
|
329
|
+ 'recent' => in_array('recent',$result['FLAGS']) ? 1 : 0,
|
|
|
330
|
+ 'seen' => in_array('seen',$result['FLAGS']) ? 1 : 0,
|
|
|
331
|
+ 'draft' => in_array('draft',$result['FLAGS']) ? 1 : 0,
|
|
|
332
|
+ 'flagged' => in_array('flagged',$result['FLAGS']) ? 1 : 0,
|
|
|
333
|
+ 'answered' => in_array('answered',$result['FLAGS']) ? 1 : 0,
|
|
|
334
|
+ 'folder_id' => $folder_id,
|
|
|
335
|
+ 'email_id' => $email_id,
|
|
|
336
|
+ 'is_file' => MailFun::isFile($result['BODYSTRUCTURE']??'') ? 1: 0 //是否附件
|
|
|
337
|
+ ];
|
|
|
338
|
+ $data['date'] = $data['date'] ? : 0;
|
|
|
339
|
+
|
|
|
340
|
+ // 验证是否存在黑名单中
|
|
|
341
|
+ if($blacklist){
|
|
|
342
|
+ // 邮箱是否在黑名单中
|
|
|
343
|
+ $isBlacklist = false;
|
|
|
344
|
+ if (!empty($blacklist['emails']) && is_array($blacklist['emails']) && in_array($data['from'],$blacklist['emails'])){
|
|
|
345
|
+ $isBlacklist = true;
|
|
|
346
|
+ }
|
|
|
347
|
+ // 域是否存在
|
|
|
348
|
+ if (!empty($blacklist['domain']) && is_array($blacklist['domain']) && in_array(explode('@',$data['from'])[1],$blacklist['domain'])){
|
|
|
349
|
+ $isBlacklist = true;
|
|
|
350
|
+ }
|
|
|
351
|
+
|
|
|
352
|
+ if($isBlacklist && $blackFolder){
|
|
|
353
|
+ // 移入垃圾箱
|
|
|
354
|
+ $this->client->move($result['UID'],$blackFolder);
|
|
|
355
|
+ continue;
|
|
|
356
|
+ }
|
|
|
357
|
+ }
|
|
|
358
|
+
|
|
|
359
|
+
|
|
|
360
|
+ }catch (\Throwable $e){
|
|
|
361
|
+ logs(
|
|
|
362
|
+ '邮件解析失败:'.PHP_EOL.$e->getMessage().PHP_EOL.print_r($result,true),
|
|
|
363
|
+ LOG_PATH.'/imap/mail/'.$email_id.'/'.$result['UID'].'.log'
|
|
|
364
|
+ );
|
|
|
365
|
+ unset($results[$key]);
|
|
|
366
|
+ continue;
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ // 插入数据库
|
|
|
370
|
+ try {
|
|
|
371
|
+ $id = $db->insert(listsSql::$table,$data);
|
|
|
372
|
+ if($id){
|
|
|
373
|
+ // 同步body内容
|
|
|
374
|
+ redis()->rPush('sync_email_body', [
|
|
|
375
|
+ 'lists_id' => $id,
|
|
|
376
|
+ 'email_id' => $email_id,
|
|
|
377
|
+ 'folder_id' => $folder_id,
|
|
|
378
|
+ 'folder' => $folder,
|
|
|
379
|
+ 'uid' => $data['uid'],
|
|
|
380
|
+ ]);
|
|
|
381
|
+ }
|
|
|
382
|
+ }catch (\Throwable $e){
|
|
|
383
|
+
|
|
|
384
|
+ }
|
|
|
385
|
+
|
|
|
386
|
+ $results[$key] = [];
|
|
|
387
|
+ }
|
|
|
388
|
+ }
|
|
|
389
|
+ }
|
|
|
390
|
+
|
366
|
|
391
|
|
367
|
/**
|
392
|
/**
|
368
|
* 同步 邮件 内容 body
|
393
|
* 同步 邮件 内容 body
|