• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/lib/helper/mailbox/imap.php
  • Класс: BitrixMailHelperMailboxImap
  • Вызов: Imap::checkMessagesForExistence
public function checkMessagesForExistence($dirPath ='INBOX',$UIDs = [])
{
	if(!empty($UIDs))
	{
		/*
			If a non-existing id gets among the existing ones,
			some mailers may issue an error (instead of issuing existing messages),
			then we will think that the letters disappeared on the mail service,
			although in fact there were existing messages among them.
			But the messages can be deleted legally,
			it's just that the mail has not been resynchronized for a long time.
			In this case, small samples are needed in order to catch existing messages in any of them.
		*/
		$chunks = array_chunk($UIDs, 5);

		$existingMessage = NULL;

		foreach ($chunks as $chunk)
		{
			$messages = $this->client->fetch(
				true,
				$dirPath,
				join(',', $chunk),
				'(UID FLAGS)',
				$error,
				'list'
			);

			if(!($messages === false || empty($messages)))
			{
				foreach ($messages as $item)
				{
					if(!isset($item['FLAGS']))
					{
						continue;
					}

					$messageDeleted = preg_grep('/^ x5c Deleted $/ix', $item['FLAGS']) ? true : false;

					if(!$messageDeleted)
					{
						$existingMessage = $item;
						break;
					}
				}
			}
		}

		if(!is_null($existingMessage))
		{
			if(isset($existingMessage['UID']))
			{
				return $existingMessage['UID'];
			}
		}
	}

	return false;
}