9 private $currentUserId;
13 $this->currentUserId = $currentUserId;
18 $parts = explode(
",", $string);
19 $firstContact = trim($parts[0]);
22 if ($address->validate())
24 return $address->getEmail();
34 foreach ($filedFromList as $filed)
44 $mailsNames = $this->getEmailsNames($messages);
49 return $item[
'email'];
53 $mailContacts = array_combine(array_column($mailContacts,
'EMAIL'), array_values($mailContacts));
54 $mailContacts = $this->fillFileIdColumn($mailContacts);
56 foreach ($mailsNames as $email => $data)
58 if (!empty($mailContacts[$email]))
60 if ((!$data[
'name'] || $data[
'name'] === $data[
'email']) &&
61 ($mailContacts[$email][
'NAME'] && $mailContacts[$email][
'NAME'] !== $mailContacts[$email][
'EMAIL']))
63 $mailsNames[$email][
'name'] = $mailContacts[$email][
'NAME'];
65 $mailsNames[$email][
'mailContact'] = $mailContacts[$email];
69 $mailsNames[$email][
'mailContact'] = [
70 'EMAIL' => $data[
'email'],
71 'NAME' => $data[
'name'],
78 private function getEmailsNames($messages, $fieldList =
false)
84 $messages = $fieldList;
87 foreach ($messages as $index => $message)
89 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message[
'FIELD_FROM']));
90 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message[
'FIELD_TO']));
91 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message[
'FIELD_CC']));
92 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message[
'FIELD_BCC']));
94 $emailNames = $this->getBestNameChoices($emailNames);
99 private function getBestNameChoices($emailNames)
103 foreach ($emailNames as $index => $data)
105 if (!isset($bestNames[$data[
'email']]))
107 $bestNames[$data[
'email']] = $data[
'name'];
108 $results[$data[
'email']] = [
109 'email' => $data[
'email'],
110 'name' => $data[
'name'],
114 $newName = $data[
'name'];
115 $oldName = $bestNames[$data[
'email']];
116 if (!$oldName || $oldName == $data[
'email'])
118 $bestNames[$data[
'email']] = $newName;
119 $results[$data[
'email']] = [
120 'email' => $data[
'email'],
129 private function extractMailsNamesFrom($parsedListOfEmails)
133 if ($parsedListOfEmails)
135 foreach (\
Bitrix\
Mail\Helper\Message::parseAddressList($parsedListOfEmails) as $mailCopy)
137 $avatarKey = static::getAvatarKeyByString($mailCopy);
140 $address =
new Main\Mail\Address($avatarKey);
141 if ($address->validate())
144 'email' => $address->getEmail(),
145 'name' => $address->getName() ?: $address->getEmail(),
154 private function fillFileIdColumn($mailContacts)
156 foreach ($mailContacts as $mail => $mailContact)
158 if (!empty($mailContacts[$mail][
'AVATAR_ID']))
160 $mailContacts[$mail][
'FILE_ID'] = $mailContacts[$mail][
'AVATAR_ID'];
163 return $mailContacts;
173 return MailContactTable::getList(array(
177 Main\UserTable::class,
179 '=this.EMAIL' =>
'ref.EMAIL',
185 'NAME',
'EMAIL',
'ICON',
'FILE_ID',
186 'AVATAR_ID' =>
'USER.PERSONAL_PHOTO',
189 '=USER_ID' => $this->currentUserId,