修复SyncMail: isUid模式n:*回落邮件误判、构造异常连接泄漏、退信body被isBody覆盖、动态属性、登录失败计数
正在显示
1 个修改的文件
包含
46 行增加
和
7 行删除
| @@ -70,6 +70,28 @@ class SyncMail { | @@ -70,6 +70,28 @@ class SyncMail { | ||
| 70 | */ | 70 | */ |
| 71 | public function __construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null) | 71 | public function __construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null) |
| 72 | { | 72 | { |
| 73 | + try { | ||
| 74 | + $this->construct($email, $imap); | ||
| 75 | + }catch (\Throwable $e){ | ||
| 76 | + // 构造函数抛出异常时 PHP 不会调用析构函数,这里必须手动释放 | ||
| 77 | + // 否则池中会残留登录失败/状态异常的连接,下次复用直接报错 | ||
| 78 | + if($this->imap instanceof Imap){ | ||
| 79 | + ImapPool::release($this->imap); | ||
| 80 | + unset($this->imap); | ||
| 81 | + } | ||
| 82 | + throw $e; | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * 构造实际逻辑(供构造函数 try/catch 包裹) | ||
| 88 | + * @param int|string|array $email | ||
| 89 | + * @param Imap|null $imap | ||
| 90 | + * @return void | ||
| 91 | + * @throws \Exception | ||
| 92 | + */ | ||
| 93 | + protected function construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null) | ||
| 94 | + { | ||
| 73 | $this->db = db(); | 95 | $this->db = db(); |
| 74 | 96 | ||
| 75 | if(!is_array($email)){ | 97 | if(!is_array($email)){ |
| @@ -162,6 +184,12 @@ class SyncMail { | @@ -162,6 +184,12 @@ class SyncMail { | ||
| 162 | public $isUid = 0; | 184 | public $isUid = 0; |
| 163 | 185 | ||
| 164 | /** | 186 | /** |
| 187 | + * 收件箱的folder_id(显式声明,避免动态属性) | ||
| 188 | + * @var int | ||
| 189 | + */ | ||
| 190 | + protected $folder_inbox_id = 0; | ||
| 191 | + | ||
| 192 | + /** | ||
| 165 | * @param $uid 2 表示没有数据 则执行下一个条件 1表示没有数据就没有数据 | 193 | * @param $uid 2 表示没有数据 则执行下一个条件 1表示没有数据就没有数据 |
| 166 | * @return $this | 194 | * @return $this |
| 167 | * @author:dc | 195 | * @author:dc |
| @@ -209,6 +237,7 @@ class SyncMail { | @@ -209,6 +237,7 @@ class SyncMail { | ||
| 209 | $login = $this->imap->login(); | 237 | $login = $this->imap->login(); |
| 210 | if(!$login->isOk()){ | 238 | if(!$login->isOk()){ |
| 211 | foreach ([ | 239 | foreach ([ |
| 240 | + | ||
| 212 | 'Invalid login or password', | 241 | 'Invalid login or password', |
| 213 | '[ALERT] Invalid credentials (Failure)',// 登录失败 | 242 | '[ALERT] Invalid credentials (Failure)',// 登录失败 |
| 214 | '[AUTHENTICATIONFAILED] Invalid credentials (Failure)',// 登录失败 | 243 | '[AUTHENTICATIONFAILED] Invalid credentials (Failure)',// 登录失败 |
| @@ -228,7 +257,7 @@ class SyncMail { | @@ -228,7 +257,7 @@ class SyncMail { | ||
| 228 | ] as $em){ | 257 | ] as $em){ |
| 229 | if(str_contains($login->getMessage(), $em)){ | 258 | if(str_contains($login->getMessage(), $em)){ |
| 230 | 259 | ||
| 231 | - if(redis()->incr('err_mail:'.$this->emailId(),86400)>=2 || strpos($this->email['email'],'@gmail.com')){ | 260 | + if(redis()->incr('err_mail:'.$this->emailId(),86400)>=2 || stripos($this->email['email'],'@gmail.com') !== false){ |
| 232 | $this->db->update( | 261 | $this->db->update( |
| 233 | \Model\emailSql::$table, | 262 | \Model\emailSql::$table, |
| 234 | ['pwd_error'=>1], | 263 | ['pwd_error'=>1], |
| @@ -241,6 +270,9 @@ class SyncMail { | @@ -241,6 +270,9 @@ class SyncMail { | ||
| 241 | 270 | ||
| 242 | abort($login->getMessage()?:'连接服务器异常'); | 271 | abort($login->getMessage()?:'连接服务器异常'); |
| 243 | } | 272 | } |
| 273 | + | ||
| 274 | + // 登录成功 清除失败计数,防止偶发失败(网络抖动)累计误标 pwd_error | ||
| 275 | + redis()->delete('err_mail:'.$this->emailId()); | ||
| 244 | } | 276 | } |
| 245 | 277 | ||
| 246 | /** | 278 | /** |
| @@ -329,6 +361,8 @@ class SyncMail { | @@ -329,6 +361,8 @@ class SyncMail { | ||
| 329 | */ | 361 | */ |
| 330 | public function sync($syncMail = true){ | 362 | public function sync($syncMail = true){ |
| 331 | $this->isStop = false; | 363 | $this->isStop = false; |
| 364 | + // 重置收件箱folder_id,防止同一实例多次sync时残留上次的值 | ||
| 365 | + $this->folder_inbox_id = 0; | ||
| 332 | /*********************************** 同步文件夹 ***************************************/ | 366 | /*********************************** 同步文件夹 ***************************************/ |
| 333 | // 获取文件夹 | 367 | // 获取文件夹 |
| 334 | $folders = $this->imap->getFolders(); | 368 | $folders = $this->imap->getFolders(); |
| @@ -479,6 +513,11 @@ class SyncMail { | @@ -479,6 +513,11 @@ class SyncMail { | ||
| 479 | if($maxUid){ | 513 | if($maxUid){ |
| 480 | $this->eLog("找到最大的uid ".$maxUid); | 514 | $this->eLog("找到最大的uid ".$maxUid); |
| 481 | $lists = $msg->uid(1)->get(($maxUid+1).':*')->all(); | 515 | $lists = $msg->uid(1)->get(($maxUid+1).':*')->all(); |
| 516 | + // IMAP 规范:UID FETCH n:* 在 n 大于服务器最大uid时,仍会返回uid最大的那封 | ||
| 517 | + // 这里必须过滤掉 <= maxUid 的,否则没有新邮件时也会返回1,导致"无数据"判定失效 | ||
| 518 | + $lists = array_values(array_filter($lists,function ($item) use ($maxUid){ | ||
| 519 | + return $item->uid > $maxUid; | ||
| 520 | + })); | ||
| 482 | if($lists){ | 521 | if($lists){ |
| 483 | $this->saveMail($folder_id,$lists,$isBody); | 522 | $this->saveMail($folder_id,$lists,$isBody); |
| 484 | return count($lists); | 523 | return count($lists); |
| @@ -669,6 +708,11 @@ class SyncMail { | @@ -669,6 +708,11 @@ class SyncMail { | ||
| 669 | 708 | ||
| 670 | } | 709 | } |
| 671 | 710 | ||
| 711 | + // 全局 isBody 优先覆盖(注意:必须先执行,后面的退信强制逻辑才能不被覆盖) | ||
| 712 | + if($this->isBody!==null){ | ||
| 713 | + $isBody = $this->isBody; | ||
| 714 | + } | ||
| 715 | + | ||
| 672 | // 同步body | 716 | // 同步body |
| 673 | if($this->isBody !== true){ | 717 | if($this->isBody !== true){ |
| 674 | // 预热邮件在第一时间不拉取 | 718 | // 预热邮件在第一时间不拉取 |
| @@ -681,7 +725,7 @@ class SyncMail { | @@ -681,7 +725,7 @@ class SyncMail { | ||
| 681 | 725 | ||
| 682 | } | 726 | } |
| 683 | if(php_sapi_name()=='cli') { | 727 | if(php_sapi_name()=='cli') { |
| 684 | - // 退信拿body | 728 | + // 退信拿body(此处强制 $isBody=true,放在全局覆盖之后才不会被打回 false) |
| 685 | if ($item->getFolderName() == 'INBOX' && stripos($data['subject'], '退信')) { | 729 | if ($item->getFolderName() == 'INBOX' && stripos($data['subject'], '退信')) { |
| 686 | $isBody = true; | 730 | $isBody = true; |
| 687 | } else { | 731 | } else { |
| @@ -693,11 +737,6 @@ class SyncMail { | @@ -693,11 +737,6 @@ class SyncMail { | ||
| 693 | } | 737 | } |
| 694 | } | 738 | } |
| 695 | 739 | ||
| 696 | - // 全局 | ||
| 697 | - if($this->isBody!==null){ | ||
| 698 | - $isBody = $this->isBody; | ||
| 699 | - } | ||
| 700 | - | ||
| 701 | //TODO 如果header 头信息里面有2段数据 第二段就作为内容解析 | 740 | //TODO 如果header 头信息里面有2段数据 第二段就作为内容解析 |
| 702 | if($item->header->body()){ | 741 | if($item->header->body()){ |
| 703 | $parseBody = $item->header->body(); | 742 | $parseBody = $item->header->body(); |
-
请 注册 或 登录 后发表评论