16 public static function add(array $fields)
18 if (empty($fields[
'OPTIONS']) || !is_array($fields[
'OPTIONS']))
20 $fields[
'OPTIONS'] = array();
24 if ($error || $errors)
26 return array(
'error' => $error,
'errors' => $errors);
29 if (empty($fields[
'IS_CONFIRMED']))
31 $fields[
'OPTIONS'][
'confirm_code'] = \Bitrix\Main\Security\Random::getStringByCharsets(5,
'0123456789abcdefghjklmnpqrstuvwxyz');
32 $fields[
'OPTIONS'][
'confirm_time'] = time();
36 $result = Internal\SenderTable::add($fields);
37 if ($result->isSuccess())
39 $senderId = $result->getId();
42 if (empty($fields[
'IS_CONFIRMED']))
44 $mailEventFields = array(
45 'DEFAULT_EMAIL_FROM' => $fields[
'EMAIL'],
46 'EMAIL_TO' => $fields[
'EMAIL'],
47 'MESSAGE_SUBJECT' => getMessage(
'MAIN_MAIL_CONFIRM_MESSAGE_SUBJECT'),
48 'CONFIRM_CODE' => mb_strtoupper($fields[
'OPTIONS'][
'confirm_code']),
51 if (!empty($smtpConfig))
53 \Bitrix\Main\EventManager::getInstance()->addEventHandlerCompatible(
56 function (&$eventFields, &$message, $context) use (&$smtpConfig)
58 $context->setSmtp($smtpConfig);
63 \CEvent::sendImmediate(
'MAIN_MAIL_CONFIRM_CODE', SITE_ID, $mailEventFields);
67 if (isset($fields[
'OPTIONS'][
'__replaces']) && $fields[
'OPTIONS'][
'__replaces'] > 0)
69 Internal\SenderTable::delete(
70 (
int) $fields[
'OPTIONS'][
'__replaces']
75 return [
'senderId' => $senderId,
'confirmed' => !empty($fields[
'IS_CONFIRMED'])];
139 public static function delete($ids)
141 $userId = CurrentUser::get()->getId();
153 $senders = SenderTable::getList([
158 '=USER_ID' => $userId,
160 'IS_CONFIRMED' =>
true]
164 $userFormattedName = CurrentUser::get()->getFormattedName();
165 foreach ($senders as $sender)
169 $senderName = sprintf(
171 empty($sender[
'NAME']) ? $userFormattedName : $sender[
'NAME'],
175 $signatures = UserSignatureTable::getList([
178 '=USER_ID' => $userId,
179 'SENDER' => $senderName
183 foreach ($signatures as $signature)
185 UserSignatureTable::delete($signature[
'ID']);
189 if(!empty($sender[
'OPTIONS'][
'smtp'][
'server']) && empty($sender[
'OPTIONS'][
'smtp'][
'encrypted']) && !isset($smtpConfigs[$sender[
'EMAIL']]))
191 $smtpConfigs[$sender[
'EMAIL']] = $sender[
'OPTIONS'][
'smtp'];
194 if(!empty($smtpConfigs))
196 $senders = SenderTable::getList([
201 '@EMAIL' => array_keys($smtpConfigs),
205 foreach($senders as $sender)
207 if(isset($smtpConfigs[$sender[
'EMAIL']]))
209 $options = $sender[
'OPTIONS'];
210 $options[
'smtp'] = $smtpConfigs[$sender[
'EMAIL']];
211 $result = SenderTable::update($sender[
'ID'], [
212 'OPTIONS' => $options,
214 if($result->isSuccess())
216 unset($smtpConfigs[$sender[
'EMAIL']]);
217 static::clearCustomSmtpCache($sender[
'EMAIL']);
219 if(empty($smtpConfigs))
226 foreach ((array) $ids as $id)
228 Internal\SenderTable::delete(
242 static $smtp = array();
244 if (!isset($smtp[$email]))
248 $cache = new \CPHPCache();
250 if ($cache->initCache(30*24*3600, $email,
'/main/mail/smtp'))
252 $config = $cache->getVars();
256 $res = Internal\SenderTable::getList(array(
258 'IS_CONFIRMED' =>
true,
265 while ($item = $res->fetch())
267 if (!empty($item[
'OPTIONS'][
'smtp'][
'server']) && empty($item[
'OPTIONS'][
'smtp'][
'encrypted']))
269 $config = $item[
'OPTIONS'][
'smtp'];
274 $cache->startDataCache();
275 $cache->endDataCache($config);
282 'host' => $config[
'server'],
283 'port' => $config[
'port'],
284 'protocol' => $config[
'protocol'],
285 'login' => $config[
'login'],
286 'password' => $config[
'password'],
287 'isOauth' => $config[
'isOauth'],
289 if ($config->getIsOauth() && \CModule::includeModule(
'mail'))
291 $expireGapSeconds = self::getOAuthTokenExpireGapSeconds();
292 $token = \Bitrix\Mail\Helper\OAuth::getTokenByMeta($config->getPassword(), $expireGapSeconds);
293 $config->setPassword($token);
297 $smtp[$email] = $config;
300 return $smtp[$email];
313 $address = new \Bitrix\Main\Mail\Address($email);
315 if (!$address->validate())
320 $email = $address->getEmail();
321 static $mailLimit = array();
323 if (!isset($mailLimit[$email]))
325 $cache = new \CPHPCache();
327 if ($cache->initCache(3600, $email,
'/main/mail/limit'))
329 $mailLimit[$email] = $cache->getVars();
333 $res = Internal\SenderTable::getList(array(
335 'IS_CONFIRMED' =>
true,
343 while ($item = $res->fetch())
345 if ($item[
'OPTIONS'][
'smtp'][
'limit'] !==
null)
347 $limit = (int)$item[
'OPTIONS'][
'smtp'][
'limit'];
352 $mailLimit[$email] = $limit;
354 $cache->startDataCache();
355 $cache->endDataCache($mailLimit[$email]);
359 return $mailLimit[$email] < 0 ? 0 : $mailLimit[$email];
373 public static function setEmailLimit(
string $email,
int $limit,
bool $quite =
true): bool
375 $address = new \Bitrix\Main\Mail\Address($email);
377 if (!$address->validate())
382 $email = $address->getEmail();
384 $cache = new \CPHPCache();
385 $cache->clean($email,
'/main/mail/limit');
387 $res = Internal\SenderTable::getList(array(
389 'IS_CONFIRMED' =>
true,
403 while ($item = $res->fetch())
405 $oldLimit = (int)($item[
'OPTIONS'][
'smtp'][
'limit'] ?? 0);
406 if ($item[
'OPTIONS'][
'smtp'] && $limit !== $oldLimit)
408 $item[
'OPTIONS'][
'smtp'][
'limit'] = $limit;
409 $updateResult = Internal\SenderTable::update($item[
'ID'], [
'OPTIONS' => $item[
'OPTIONS']]);
411 if (!$quite && ($limit < $oldLimit || $oldLimit <= 0) && $updateResult->isSuccess())
413 $event =
new Event(
'main', self::MAIN_SENDER_SMTP_LIMIT_DECREASE, [
'EMAIL'=>$email]);
432 $address = new \Bitrix\Main\Mail\Address($email);
434 if (!$address->validate())
439 $email = $address->getEmail();
440 $cache = new \CPHPCache();
441 $cache->clean($email,
'/main/mail/limit');
443 $res = Internal\SenderTable::getList(array(
445 'IS_CONFIRMED' =>
true,
453 while ($item = $res->fetch())
455 if (isset($item[
'OPTIONS'][
'smtp'][
'limit']))
457 unset($item[
'OPTIONS'][
'smtp'][
'limit']);
458 Internal\SenderTable::update($item[
'ID'], [
'OPTIONS' => $item[
'OPTIONS']]);
467 $headers = $event->getParameter(
'arguments')->additional_headers;
468 $context = $event->getParameter(
'arguments')->context;
470 if (empty($context) || !($context instanceof
Context))
475 if ($context->getSmtp() && $context->getSmtp()->getHost())
480 if (preg_match(
'/X-Bitrix-Mail-SMTP-Host:/i', $headers))
485 $eol = Mail::getMailEol();
486 $eolh = preg_replace(
'/([a-f0-9]{2})/i',
'\x\1', bin2hex($eol));
488 if (preg_match(sprintf(
'/(^|%1$s)From:(.+?)(%1$s([^\s]|$)|$)/is', $eolh), $headers, $matches))
490 $address =
new Address(preg_replace(sprintf(
'/%s\s+/', $eolh),
'', $matches[2]));
491 if ($address->validate())
493 if ($customSmtp = static::getCustomSmtp($address->getEmail()))
495 $context->setSmtp($customSmtp);
505 static $mailboxes = array();
509 if (is_object($USER) && $USER->isAuthorized())
511 $userId = $USER->getId();
520 if (array_key_exists($userId, $mailboxes))
522 return $mailboxes[$userId];
525 $mailboxes[$userId] = array();
527 if (is_object($USER) && $USER->isAuthorized() && $USER->getId() == $userId)
530 'ID' => $USER->getId(),
531 'TITLE' => $USER->getParam(
"TITLE"),
532 'NAME' => $USER->getFirstName(),
533 'SECOND_NAME' => $USER->getSecondName(),
534 'LAST_NAME' => $USER->getLastName(),
535 'LOGIN' => $USER->getLogin(),
536 'EMAIL' => $USER->getEmail(),
539 $isAdmin = in_array(1, $USER->getUserGroupArray());
543 $userData = Main\UserTable::getList(array(
544 'select' => array(
'ID',
'TITLE',
'NAME',
'SECOND_NAME',
'LAST_NAME',
'LOGIN',
'EMAIL'),
545 'filter' => array(
'=ID' => $userId),
548 $isAdmin = in_array(1, \CUser::getUserGroup($userId));
551 $userNameFormated = \CUser::formatName(\CSite::getNameFormat(), $userData,
true,
false);
553 if (\CModule::includeModule(
'mail'))
555 foreach (\
Bitrix\
Mail\MailboxTable::getUserMailboxes($userId) as $mailbox)
557 if (!empty($mailbox[
'EMAIL']))
559 $mailboxName = trim($mailbox[
'USERNAME']) ?: trim($mailbox[
'OPTIONS'][
'name']) ?: $userNameFormated;
561 $key = hash(
'crc32b', mb_strtolower($mailboxName).$mailbox[
'EMAIL']);
562 $mailboxes[$userId][$key] = array(
563 'name' => $mailboxName,
564 'email' => $mailbox[
'EMAIL'],
565 'showEditHint' =>
true,
572 $crmAddress =
new Address(Main\
Config\Option::get(
'crm',
'mail',
''));
573 if ($crmAddress->validate())
575 $key = hash(
'crc32b', mb_strtolower($userNameFormated).$crmAddress->getEmail());
577 $mailboxes[$userId][$key] = [
578 'name' => $crmAddress->getName() ?: $userNameFormated,
579 'email' => $crmAddress->getEmail(),
583 $res = SenderTable::getList(array(
585 'IS_CONFIRMED' =>
true,
588 '=USER_ID' => $userId,
596 while ($item = $res->fetch())
598 $item[
'NAME'] = trim($item[
'NAME']) ?: $userNameFormated;
599 $item[
'EMAIL'] = mb_strtolower($item[
'EMAIL']);
600 $key = hash(
'crc32b', mb_strtolower($item[
'NAME']).$item[
'EMAIL']);
602 if (!isset($mailboxes[$userId][$key]))
604 $mailboxes[$userId][$key] = [
606 'name' => $item[
'NAME'],
607 'email' => $item[
'EMAIL'],
608 'can_delete' => $userId == $item[
'USER_ID'] || $item[
'IS_PUBLIC'] && $isAdmin,
611 else if (!isset($mailboxes[$userId][$key][
'id']))
613 $mailboxes[$userId][$key][
'id'] = $item[
'ID'];
614 $mailboxes[$userId][$key][
'can_delete'] = $userId == $item[
'USER_ID'] || $item[
'IS_PUBLIC'] && $isAdmin;
618 foreach ($mailboxes[$userId] as $key => $item)
620 $mailboxes[$userId][$key][
'formated'] = sprintf(
621 $item[
'name'] ?
'%s <%s>' :
'%s%s',
622 $item[
'name'], $item[
'email']
625 $mailboxes[$userId][$key][
'userId'] = $userId;
628 $mailboxes[$userId] = array_values($mailboxes[$userId]);
630 return $mailboxes[$userId];