16 $userMailboxes = \Bitrix\Mail\MailboxTable::getList([
22 '=SERVER_TYPE' =>
'imap',
26 if (empty($userMailboxes))
31 $numberOfUnSynchronizedMailboxes = count($userMailboxes);
33 foreach ($userMailboxes as $mailbox)
35 $mailboxID = $mailbox[
'ID'];
36 $mailboxHelper = Helper\Mailbox::createInstance($mailboxID,
false);
37 if (empty($mailboxHelper))
39 $numberOfUnSynchronizedMailboxes--;
44 'MAILBOX_ID' => $mailboxID,
45 'ENTITY_TYPE' =>
'MAILBOX',
46 'ENTITY_ID' => $mailboxID,
47 'PROPERTY_NAME' =>
'SYNC_FIRST_DAY',
51 '=MAILBOX_ID' => $keyRow[
'MAILBOX_ID'],
52 '=ENTITY_TYPE' => $keyRow[
'ENTITY_TYPE'],
53 '=ENTITY_ID' => $keyRow[
'ENTITY_ID'],
54 '=PROPERTY_NAME' => $keyRow[
'PROPERTY_NAME'],
57 $startValue =
'started_for_id_'.$mailboxID;
59 if(\
Bitrix\
Mail\Internals\MailEntityOptionsTable::getCount($filter))
61 if(Internals\MailEntityOptionsTable::getList([
66 ])->fetchAll()[0][
'VALUE'] !==
'completed')
68 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
70 [
'VALUE' => $startValue]
73 $synchronizationSuccess = $mailboxHelper->syncFirstDay();
75 if($synchronizationSuccess)
77 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
79 [
'VALUE' =>
'completed']
81 $numberOfUnSynchronizedMailboxes--;
86 $numberOfUnSynchronizedMailboxes--;
92 $fields[
'VALUE'] = $startValue;
93 \Bitrix\Mail\Internals\MailEntityOptionsTable::add(
96 $synchronizationSuccess = $mailboxHelper->syncFirstDay();
98 if($synchronizationSuccess)
100 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
102 [
'VALUE' =>
'completed']
104 $numberOfUnSynchronizedMailboxes--;
109 if($numberOfUnSynchronizedMailboxes === 0)
115 return 'Bitrix\Mail\Helper::syncAllDirsInMailboxForTheFirstSyncDayAgent();';
121 $mailboxHelper = Helper\Mailbox::createInstance($id,
false);
123 if (empty($mailboxHelper))
128 $mailbox = $mailboxHelper->getMailbox();
130 if ($mailbox[
'OPTIONS'][
'next_sync'] <= time())
132 $mailboxHelper->sync();
134 $mailbox = $mailboxHelper->getMailbox();
139 $pPERIOD = min($pPERIOD, max($mailbox[
'OPTIONS'][
'next_sync'] - time(), 60));
141 return sprintf(
'Bitrix\Mail\Helper::syncMailboxAgent(%u);', $id);
146 $mailboxHelper = Helper\Mailbox::createInstance($id,
false);
148 $result = empty($mailboxHelper) ? false : $mailboxHelper->syncOutgoing();
155 $userMailboxes = \Bitrix\Mail\MailboxTable::getList([
161 '=SERVER_TYPE' =>
'imap',
165 if (empty($userMailboxes))
170 $numberOfUnSynchronizedMailboxes = count($userMailboxes);
172 foreach ($userMailboxes as $mailbox)
174 $mailboxID = $mailbox[
'ID'];
175 $mailboxHelper = Helper\Mailbox::createInstance($mailboxID,
false);
176 if (empty($mailboxHelper))
178 $numberOfUnSynchronizedMailboxes--;
183 'MAILBOX_ID' => $mailboxID,
184 'ENTITY_TYPE' =>
'MAILBOX',
185 'ENTITY_ID' => $mailboxID,
186 'PROPERTY_NAME' =>
'SYNC_IS_OLD_STATUS',
190 '=MAILBOX_ID' => $keyRow[
'MAILBOX_ID'],
191 '=ENTITY_TYPE' => $keyRow[
'ENTITY_TYPE'],
192 '=ENTITY_ID' => $keyRow[
'ENTITY_ID'],
193 '=PROPERTY_NAME' => $keyRow[
'PROPERTY_NAME'],
196 $startValue =
'started_for_id_'.$mailboxID;
198 if(\
Bitrix\
Mail\Internals\MailEntityOptionsTable::getCount($filter))
200 if(Internals\MailEntityOptionsTable::getList([
205 ])->fetchAll()[0][
'VALUE'] !==
'completed')
207 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
209 [
'VALUE' => $startValue]
212 $synchronizationSuccess = $mailboxHelper->resyncIsOldStatus();
214 if($synchronizationSuccess)
216 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
218 [
'VALUE' =>
'completed']
220 $numberOfUnSynchronizedMailboxes--;
225 $numberOfUnSynchronizedMailboxes--;
231 $fields[
'VALUE'] = $startValue;
232 \Bitrix\Mail\Internals\MailEntityOptionsTable::add(
235 $synchronizationSuccess = $mailboxHelper->resyncIsOldStatus();
237 if($synchronizationSuccess)
239 \Bitrix\Mail\Internals\MailEntityOptionsTable::update(
241 [
'VALUE' =>
'completed']
243 $numberOfUnSynchronizedMailboxes--;
248 if($numberOfUnSynchronizedMailboxes === 0)
254 return 'Bitrix\Mail\Helper::markOldMessagesAgent();';
260 $mailboxHelper = Helper\Mailbox::rawInstance($id,
false);
262 if (empty($mailboxHelper))
267 $mailboxHelper->setCheckpoint();
271 '=MAILBOX_ID' => $id,
276 $stage1 = $mailboxHelper->dismissOldMessages();
277 $stage2 = $mailboxHelper->dismissDeletedUidMessages();
278 $stage3 = $mailboxHelper->cleanup();
282 $pPERIOD = min($pPERIOD, max($stage1 && $stage2 && $stage3 ? $pPERIOD : 600, 60));
284 if ($pPERIOD ===
null)
289 return sprintf(
'Bitrix\Mail\Helper::cleanupMailboxAgent(%u);', $id);
297 $mailboxHelper = Helper\Mailbox::createInstance($id,
false);
299 $result = empty($mailboxHelper) ? false : $mailboxHelper->resortTree();
306 return \CMailbox::delete($id) ?
'' : sprintf(
'Bitrix\Mail\Helper::deleteMailboxAgent(%u);', $id);
311 $res = MailServicesTable::getList(array(
314 '@SERVICE_TYPE' => array(
'domain',
'crdomain'),
317 while ($item = $res->fetch())
319 if ($item[
'SERVICE_TYPE'] ==
'domain')
321 $lockName = sprintf(
'domain_users_sync_lock_%u', $item[
'ID']);
322 $syncLock = \Bitrix\Main\Config\Option::get(
'mail', $lockName, 0);
324 if ($syncLock < time()-3600)
326 \Bitrix\Main\Config\Option::set(
'mail', $lockName, time());
327 \CMailDomain2::getDomainUsers($item[
'TOKEN'], $item[
'SERVER'], $error,
true);
328 \Bitrix\Main\Config\Option::set(
'mail', $lockName, 0);
331 else if ($item[
'SERVICE_TYPE'] ==
'crdomain')
333 \CControllerClient::executeEvent(
'OnMailControllerResyncMemberUsers', array(
'DOMAIN' => $item[
'SERVER']));
337 return 'Bitrix\Mail\Helper::resyncDomainUsersAgent();';
342 $mailboxHelper = Helper\Mailbox::createInstance($id,
false);
344 return empty($mailboxHelper) ? false : $mailboxHelper->sync();
347 public static function listImapDirs($mailbox, &$error = [], &$errors =
null)
352 $client = static::createClient($mailbox);
354 $list = $client->listMailboxes(
'*', $error,
true);
355 $errors = $client->getErrors();
361 for ($i = 0; $i < $k; $i++)
366 'path' => $item[
'name'],
367 'name' => $item[
'title'],
368 'level' => $item[
'level'],
369 'disabled' => (
bool) preg_grep(
'/^ \x5c Noselect $/ix', $item[
'flags']),
370 'income' => mb_strtolower($item[
'name']) ==
'inbox',
371 'outcome' => (
bool) preg_grep(
'/^ \x5c Sent $/ix', $item[
'flags']),
383 $mailbox = Helper\Mailbox::prepareMailbox([
388 $client = static::createClient($mailbox);
390 $result = $client->getUIDsForSpecificDay($dirPath, $internalDate);
397 $firstSyncUID = MailMessageUidTable::getList(
402 'filter' => array_merge(
406 '=MAILBOX_ID' => $mailboxId,
407 '=DIR_MD5' => md5($dirPath),
412 'INTERNALDATE' =>
'DESC',
418 if(isset($firstSyncUID[0][
'INTERNALDATE']))
420 return $firstSyncUID[0][
'INTERNALDATE'];
434 '!=MESSAGE_UID.IS_OLD' =>
'Y',
435 '==MESSAGE_UID.DELETE_TIME' => 0,
436 '!@MESSAGE_UID.IS_OLD' => [
'M',
'R'],
440 $firstSyncUID = MailMessageTable::getList(
444 'MESSAGE_UID', MailMessageUidTable::class, [
445 '=this.MAILBOX_ID' =>
'ref.MAILBOX_ID',
446 '=this.ID' =>
'ref.MESSAGE_ID',
448 'join_type' =>
'INNER',
453 'INTERNALDATE' =>
'MESSAGE_UID.INTERNALDATE',
455 'filter' => array_merge(
457 '=MAILBOX_ID' => $mailboxId,
458 '=MESSAGE_UID.DIR_MD5' => md5(Emoji::encode($dirPath)),
463 'FIELD_DATE' => $order,
469 if(isset($firstSyncUID[0][
'INTERNALDATE']))
471 return $firstSyncUID[0][
'INTERNALDATE'];
482 if(is_int($mailboxID) && is_null($mailbox))
484 $mailbox = Helper\Mailbox::prepareMailbox([
490 $startInternalDate = static::getStartInternalDateForDir($mailbox[
'ID'],$dirPath);
492 if($startInternalDate)
496 return static::getImapUnseen($mailbox, $dirPath,$error,$errors, $startInternalDate);
509 'MAILBOX_ID' => $mailboxId,
510 'ENTITY_TYPE' =>
'MAILBOX',
511 'ENTITY_ID' => $mailboxId
515 '=MAILBOX_ID' => $keyRow[
'MAILBOX_ID'],
516 '=ENTITY_TYPE' => $keyRow[
'ENTITY_TYPE'],
517 '=ENTITY_ID' => $keyRow[
'ENTITY_ID']
520 $rowValue = [
'VALUE' => $count];
524 Internals\MailCounterTable::update($keyRow, $rowValue);
528 Internals\MailCounterTable::add(array_merge($rowValue,$keyRow));
531 \CPullWatch::addToStack(
532 'mail_mailbox_' .$mailboxId,
534 'module_id' =>
'mail',
536 'mailboxId' => $mailboxId,
538 'command' =>
'counters_updated',
541 \Bitrix\Pull\Event::send();
546 $count = Internals\MailCounterTable::getList([
548 'ENTITY_TYPE' =>
'DIR',
549 'MAILBOX_ID' => $mailboxId,
559 if(!is_null($count[0][
"COUNT"]))
561 static::setMailboxUnseenCounter($mailboxId,(
int)$count[0][
"COUNT"]);
567 $mailboxId = $mailbox[
'ID'];
570 $syncDirs = $directoryHelper->getSyncDirs();
574 foreach ($syncDirs as $dir)
576 if($dir->isInvisibleToCounters())
581 $dirPath = $dir->getPath();
584 \CTimeZone::Disable();
585 $dirCount = static::getImapUnseenSyncForDir($mailbox,$dirPath);
586 \CTimeZone::Enable();
588 if($dirCount !==
false)
590 $totalCount += $dirCount;
593 'MAILBOX_ID' => $mailboxId,
594 'ENTITY_TYPE' =>
'DIR',
595 'ENTITY_ID'=>$dir->getId()
599 '=MAILBOX_ID' => $keyRow[
'MAILBOX_ID'],
600 '=ENTITY_TYPE' => $keyRow[
'ENTITY_TYPE'],
601 '=ENTITY_ID' => $keyRow[
'ENTITY_ID']
604 $rowValue = [
'VALUE' => $dirCount];
608 Internals\MailCounterTable::update($keyRow, $rowValue);
612 Internals\MailCounterTable::add(array_merge($rowValue,$keyRow));
620 public static function getImapUnseen($mailbox, $dirPath =
'inbox', &$error = [], &$errors =
null, $startInternalDate =
null)
625 $client = static::createClient($mailbox);
627 $result = $client->getUnseen($dirPath, $error, $startInternalDate);
628 $errors = $client->getErrors();
637 $id = (int) (is_array($id) ? $id[
'ID'] : $id);
639 $mailbox = MailboxTable::getList(array(
640 'filter' => array(
'ID' => $id,
'ACTIVE' =>
'Y'),
641 'select' => array(
'*',
'LANG_CHARSET' =>
'SITE.CULTURE.CHARSET')
647 if (!in_array($mailbox[
'SERVER_TYPE'], array(
'imap',
'controller',
'domain',
'crdomain')))
650 if (in_array($mailbox[
'SERVER_TYPE'], array(
'controller',
'crdomain')))
653 $result = \CMailDomain2::getImapData();
655 $mailbox[
'SERVER'] = $result[
'server'];
656 $mailbox[
'PORT'] = $result[
'port'];
657 $mailbox[
'USE_TLS'] = $result[
'secure'];
659 elseif ($mailbox[
'SERVER_TYPE'] ==
'domain')
661 $result = \CMailDomain2::getImapData();
663 $mailbox[
'SERVER'] = $result[
'server'];
664 $mailbox[
'PORT'] = $result[
'port'];
665 $mailbox[
'USE_TLS'] = $result[
'secure'];
668 $client = static::createClient($mailbox, $mailbox[
'LANG_CHARSET'] ?: $mailbox[
'CHARSET']);
671 $path = $dir ? $dir->getPath() :
'INBOX';
673 return $client->addMessage($path, $data, $error);
680 $res = MailMessageUidTable::getList(array(
682 'ID',
'MAILBOX_ID',
'IS_SEEN',
683 'MAILBOX_USER_ID' =>
'MAILBOX.USER_ID',
684 'MAILBOX_OPTIONS' =>
'MAILBOX.OPTIONS',
687 '=HEADER_MD5' => $hash,
688 '==DELETE_TIME' => 0,
692 while ($item = $res->fetch())
694 $isOwner = $item[
'MAILBOX_USER_ID'] == $userId;
695 $isPublic = in_array(
'crm_public_bind', (array) $item[
'MAILBOX_OPTIONS'][
'flags']);
696 $inQueue = in_array($userId, (array) $item[
'MAILBOX_OPTIONS'][
'crm_lead_resp']);
697 if (!$isOwner && !$isPublic && !$inQueue)
702 if (in_array($item[
'IS_SEEN'], array(
'Y',
'S')) != $data[
'seen'])
704 MailMessageUidTable::update(
707 'MAILBOX_ID' => $item[
'MAILBOX_ID'],
710 'IS_SEEN' => $data[
'seen'] ?
'S' :
'U',
717 private static function createClient($mailbox, $langCharset =
null)
720 $mailbox[
'SERVER'], $mailbox[
'PORT'],
721 $mailbox[
'USE_TLS'] ==
'Y' || $mailbox[
'USE_TLS'] ==
'S',
722 $mailbox[
'USE_TLS'] ==
'Y',
723 $mailbox[
'LOGIN'], $mailbox[
'PASSWORD'],
724 $langCharset ? $langCharset : LANG_CHARSET
734 parent::initSettings();
736 $this->settingServerMsSmtp =
false;
737 $this->settingMailFillToEmail =
false;
738 $this->settingMailConvertMailHeader =
true;
739 $this->settingConvertNewLineUnixToWindows =
true;
740 $this->useBlacklist =
false;
758 foreach (
$headers as $name => $value)
760 $message->headers[$name] = $value;
static overwriteMessageHeaders(Main\Mail\Mail $message, array $headers)
static resortTreeAgent($id)
static syncMailboxAgent($id)
static resyncDomainUsersAgent()
static markOldMessagesAgent()
static syncMailbox($id, &$error)
static updateImapMessage($userId, $hash, $data, &$error)
static getLastDeletedOldMessageInternaldate($mailboxId, $dirPath, $filter=[])
static getStartInternalDateForDir( $mailboxId, $dirPath, $order='ASC', $filter=['!=MESSAGE_UID.IS_OLD'=> 'Y', '==MESSAGE_UID.DELETE_TIME'=> 0, '! @MESSAGE_UID.IS_OLD'=>['M', 'R'],])
static setMailboxUnseenCounter($mailboxId, $count)
static deleteMailboxAgent($id)
static updateMailCounters($mailbox)
static addImapMessage($id, $data, &$error)
static updateMailboxUnseenCounter($mailboxId)
static syncOutgoingAgent($id)
static getImapUnseen($mailbox, $dirPath='inbox', &$error=[], &$errors=null, $startInternalDate=null)
static syncAllDirsInMailboxForTheFirstSyncDayAgent()
static listImapDirs($mailbox, &$error=[], &$errors=null)
static cleanupMailboxAgent($id)
static getImapUnseenSyncForDir($mailbox=null, $dirPath, $mailboxID=null)
static getImapUIDsForSpecificDay($mailboxID, $dirPath='inbox', $internalDate)
static deleteList(array $filter, array $messages=[], $limit=false)
static fetchOneOutcome($mailboxId)
static getCount($filter=array(), array $cache=array())