作者 邓超

修复SyncMail: isUid模式n:*回落邮件误判、构造异常连接泄漏、退信body被isBody覆盖、动态属性、登录失败计数

... ... @@ -70,6 +70,28 @@ class SyncMail {
*/
public function __construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null)
{
try {
$this->construct($email, $imap);
}catch (\Throwable $e){
// 构造函数抛出异常时 PHP 不会调用析构函数,这里必须手动释放
// 否则池中会残留登录失败/状态异常的连接,下次复用直接报错
if($this->imap instanceof Imap){
ImapPool::release($this->imap);
unset($this->imap);
}
throw $e;
}
}
/**
* 构造实际逻辑(供构造函数 try/catch 包裹)
* @param int|string|array $email
* @param Imap|null $imap
* @return void
* @throws \Exception
*/
protected function construct(int|string|array $email,\Lib\Imap\Imap|null $imap = null)
{
$this->db = db();
if(!is_array($email)){
... ... @@ -162,6 +184,12 @@ class SyncMail {
public $isUid = 0;
/**
* 收件箱的folder_id(显式声明,避免动态属性)
* @var int
*/
protected $folder_inbox_id = 0;
/**
* @param $uid 2 表示没有数据 则执行下一个条件 1表示没有数据就没有数据
* @return $this
* @author:dc
... ... @@ -209,6 +237,7 @@ class SyncMail {
$login = $this->imap->login();
if(!$login->isOk()){
foreach ([
'Invalid login or password',
'[ALERT] Invalid credentials (Failure)',// 登录失败
'[AUTHENTICATIONFAILED] Invalid credentials (Failure)',// 登录失败
... ... @@ -227,20 +256,23 @@ class SyncMail {
'authentication failure'
] as $em){
if(str_contains($login->getMessage(), $em)){
if(redis()->incr('err_mail:'.$this->emailId(),86400)>=2 || strpos($this->email['email'],'@gmail.com')){
if(redis()->incr('err_mail:'.$this->emailId(),86400)>=2 || stripos($this->email['email'],'@gmail.com') !== false){
$this->db->update(
\Model\emailSql::$table,
['pwd_error'=>1],
dbWhere(['id'=> $this->emailId()])
);
}
}
}
abort($login->getMessage()?:'连接服务器异常');
}
// 登录成功 清除失败计数,防止偶发失败(网络抖动)累计误标 pwd_error
redis()->delete('err_mail:'.$this->emailId());
}
/**
... ... @@ -329,6 +361,8 @@ class SyncMail {
*/
public function sync($syncMail = true){
$this->isStop = false;
// 重置收件箱folder_id,防止同一实例多次sync时残留上次的值
$this->folder_inbox_id = 0;
/*********************************** 同步文件夹 ***************************************/
// 获取文件夹
$folders = $this->imap->getFolders();
... ... @@ -479,6 +513,11 @@ class SyncMail {
if($maxUid){
$this->eLog("找到最大的uid ".$maxUid);
$lists = $msg->uid(1)->get(($maxUid+1).':*')->all();
// IMAP 规范:UID FETCH n:* 在 n 大于服务器最大uid时,仍会返回uid最大的那封
// 这里必须过滤掉 <= maxUid 的,否则没有新邮件时也会返回1,导致"无数据"判定失效
$lists = array_values(array_filter($lists,function ($item) use ($maxUid){
return $item->uid > $maxUid;
}));
if($lists){
$this->saveMail($folder_id,$lists,$isBody);
return count($lists);
... ... @@ -669,6 +708,11 @@ class SyncMail {
}
// 全局 isBody 优先覆盖(注意:必须先执行,后面的退信强制逻辑才能不被覆盖)
if($this->isBody!==null){
$isBody = $this->isBody;
}
// 同步body
if($this->isBody !== true){
// 预热邮件在第一时间不拉取
... ... @@ -681,7 +725,7 @@ class SyncMail {
}
if(php_sapi_name()=='cli') {
// 退信拿body
// 退信拿body(此处强制 $isBody=true,放在全局覆盖之后才不会被打回 false)
if ($item->getFolderName() == 'INBOX' && stripos($data['subject'], '退信')) {
$isBody = true;
} else {
... ... @@ -693,11 +737,6 @@ class SyncMail {
}
}
// 全局
if($this->isBody!==null){
$isBody = $this->isBody;
}
//TODO 如果header 头信息里面有2段数据 第二段就作为内容解析
if($item->header->body()){
$parseBody = $item->header->body();
... ...