55 if(!array_key_exists(
'FIELDS', $arMessageParams))
56 throw new \Bitrix\Main\ArgumentTypeException(
"FIELDS");
57 if(!array_key_exists(
'MESSAGE', $arMessageParams))
58 throw new \Bitrix\Main\ArgumentTypeException(
"MESSAGE");
59 if(!array_key_exists(
'SITE', $arMessageParams))
60 throw new \Bitrix\Main\ArgumentTypeException(
"SITE");
61 if(!array_key_exists(
'CHARSET', $arMessageParams))
62 throw new \Bitrix\Main\ArgumentTypeException(
"CHARSET");
65 $this->eventFields = $arMessageParams[
'FIELDS'];
66 if(array_key_exists(
'EVENT', $arMessageParams))
67 $this->
event = $arMessageParams[
'EVENT'];
69 $this->eventMessageFields = $arMessageParams[
'MESSAGE'];
70 if(array_key_exists(
'ID', $arMessageParams[
'MESSAGE']))
71 $this->eventMessageId = $arMessageParams[
'MESSAGE'][
'ID'];
74 is_array($arMessageParams[
'SITE'])
75 ? $arMessageParams[
'SITE']
76 : [$arMessageParams[
'SITE']]
78 $this->eventSiteFields = array_merge($this->siteFields, $this->eventFields);
79 foreach($this->eventSiteFields as $k => $v) $this->eventSiteFields[$k] = static::getFieldFlatValue($v);
111 $isHtml = $this->eventMessageFields[
"BODY_TYPE"] ==
"html";
114 $message = $this->eventMessageFields[
"MESSAGE_PHP"];
115 if(empty($message) && !empty($this->eventMessageFields[
"MESSAGE"]))
117 $message = MailInternal\EventMessageTable::replaceTemplateToPhp($this->eventMessageFields[
"MESSAGE"]);
118 if($this->eventMessageFields[
"ID"]>0)
119 MailInternal\EventMessageTable::update($this->eventMessageFields[
"ID"], array(
'MESSAGE_PHP' => $message));
122 if(!empty($this->eventMessageFields[
'SITE_TEMPLATE_ID']))
123 $siteTemplateId = $this->eventMessageFields[
'SITE_TEMPLATE_ID'];
125 $siteTemplateId =
null;
129 if(empty($siteTemplateId))
130 $siteTemplateId =
".default";
133 $themeCompiler->setSiteTemplateId($siteTemplateId);
134 $themeCompiler->setSiteId($this->siteId);
135 $themeCompiler->setLanguageId($this->languageId);
142 foreach ($this->eventSiteFields as $fieldKey => $fieldValue)
144 $eventSiteFields[
"HTML_".$fieldKey] = nl2br(htmlspecialcharsbx($fieldValue, ENT_COMPAT,
false));
146 if (strpos($fieldValue,
"<") ===
false)
155 'CODE' => mb_strtolower(trim(explode(
',', $this->
getMailTo())[0])),
156 'EVENT_NAME' => $this->eventMessageFields[
"EVENT_NAME"]
161 $themeCompiler->execute();
163 $message = $themeCompiler->getResult();
165 $this->mailBody = $message;
213 $eventMessageAttachment = [];
214 $eventFilesContent = [];
217 if(array_key_exists(
'FILE', $this->eventMessageFields))
219 $eventMessageAttachment = $this->eventMessageFields[
"FILE"];
222 if(array_key_exists(
'FILES_CONTENT', $this->event))
224 $eventFilesContent = $this->
event[
"FILES_CONTENT"];
228 if(isset($this->event[
"FILE"]) && is_array($this->event[
"FILE"]))
230 $eventFileList = array();
231 foreach($this->event[
"FILE"] as $fileId)
233 if(is_numeric($fileId))
235 $eventFileList[] = $fileId;
239 $eventMessageAttachment = array_merge($eventMessageAttachment, $eventFileList);
243 if(!empty($eventMessageAttachment))
245 $attachFileList = array();
246 $eventMessageAttachment = array_unique($eventMessageAttachment);
248 $strId = implode(
',', $eventMessageAttachment);
249 $conn = \Bitrix\Main\Application::getConnection();
250 $strSql =
"SELECT * FROM b_file WHERE ID IN(".$strId.
")";
251 $resultDb = $conn->query($strSql);
252 while($file = $resultDb->fetch())
254 $tempFile = \CFile::MakeFileArray($file[
"ID"]);
255 $attachFileList[] = array(
256 'PATH' => $tempFile[
'tmp_name'],
258 'CONTENT_TYPE' => $file[
'CONTENT_TYPE'],
259 'NAME' => ($file[
'ORIGINAL_NAME'] <>
"" ? $file[
'ORIGINAL_NAME'] : $file[
'FILE_NAME']),
263 $this->mailAttachment = $attachFileList;
266 if (!empty($eventFilesContent))
268 foreach ($eventFilesContent as $item)
270 $this->mailAttachment[] = [
271 'CONTENT_TYPE' => $item[
'CONTENT_TYPE'],
272 'NAME' => $item[
'NAME'],
273 'CONTENT' => $item[
'CONTENT'],
275 'CHARSET' => $item[
'CHARSET'],
276 'METHOD' => $item[
'METHOD'],
295 $arMailFields = array();
300 $arMailFields[
"From"] = $this->
replaceTemplate($messageFields[
"EMAIL_FROM"], $arFields);
302 if(isset($messageFields[
"BCC"]) && $messageFields[
"BCC"]!=
'')
305 if(strpos($bcc,
"@") !==
false)
306 $arMailFields[
"BCC"] = $bcc;
309 if(isset($messageFields[
"CC"]) && $messageFields[
"CC"]!=
'')
310 $arMailFields[
"CC"] = $this->
replaceTemplate($messageFields[
"CC"], $arFields);
312 if(isset($messageFields[
"REPLY_TO"]) && $messageFields[
"REPLY_TO"]!=
'')
313 $arMailFields[
"Reply-To"] = $this->
replaceTemplate($messageFields[
"REPLY_TO"], $arFields);
315 $arMailFields[
"Reply-To"] = preg_replace(
"/(.*)\\<(.*)\\>/i",
'$2', $arMailFields[
"From"]);
317 if(isset($messageFields[
"IN_REPLY_TO"]) && $messageFields[
"IN_REPLY_TO"]!=
'')
318 $arMailFields[
"In-Reply-To"] = $this->
replaceTemplate($messageFields[
"IN_REPLY_TO"], $arFields);
320 if(isset($messageFields[
"ADDITIONAL_FIELD"]) && is_array($messageFields[
'ADDITIONAL_FIELD']))
322 foreach($messageFields[
'ADDITIONAL_FIELD'] as $additionalField)
323 $arMailFields[$additionalField[
'NAME']] = static::replaceTemplate($additionalField[
'VALUE'], $arFields);
326 if(isset($messageFields[
"PRIORITY"]) && $messageFields[
"PRIORITY"]!=
'')
327 $arMailFields[
"X-Priority"] = $this->
replaceTemplate($messageFields[
"PRIORITY"], $arFields);
329 foreach($arFields as $f=>$v)
331 if(mb_substr($f, 0, 1) ==
"=")
332 $arMailFields[mb_substr($f, 1)] = $v;
335 foreach($arMailFields as $k=>$v)
336 $arMailFields[$k] = trim($v,
"\r\n");
339 if(isset($this->event[
"DUPLICATE"]) && $this->event[
"DUPLICATE"]==
"Y")
341 $all_bcc = Config\Option::get(
"main",
"all_bcc");
342 if(strpos($all_bcc,
"@") !==
false)
343 $arMailFields[
"BCC"] .= ($all_bcc <>
''?($arMailFields[
"BCC"] <>
''?
",":
"").$all_bcc:
"");
346 if(isset($this->event[
"EVENT_NAME"]))
347 $arMailFields[
'X-EVENT_NAME'] = $this->event[
"EVENT_NAME"];
350 $this->mailHeaders = $arMailFields;
454 $site_id = $sites[0];
456 if(!empty($this->eventMessageId))
458 $messageSiteDb = MailInternal\EventMessageSiteTable::getList(array(
459 'select' => array(
'SITE_ID'),
461 '=EVENT_MESSAGE_ID' => $this->eventMessageId,
465 if ($arMessageSite = $messageSiteDb->Fetch())
466 $site_id = $arMessageSite[
'SITE_ID'];
469 $globalName =
$GLOBALS[
"SERVER_NAME"] ??
'';
470 $SITE_NAME = Config\Option::get(
"main",
"site_name", $globalName);
471 $SERVER_NAME = Config\Option::get(
"main",
"server_name", $globalName);
472 $DEFAULT_EMAIL_FROM = Config\Option::get(
"main",
"email_from",
"admin@" . $globalName);
476 $result = \Bitrix\Main\SiteTable::getById($site_id);
477 if($arSite = $result->fetch())
479 $this->siteId = $arSite[
'LID'];
480 $this->languageId = $arSite[
'LANGUAGE_ID'];
482 \CEvent::$EVENT_SITE_PARAMS[$site_id] = array(
483 "SITE_NAME" => ($arSite[
"SITE_NAME"]<>
''? $arSite[
"SITE_NAME"] : $SITE_NAME),
484 "SERVER_NAME" => ($arSite[
"SERVER_NAME"]<>
''? $arSite[
"SERVER_NAME"] : $SERVER_NAME),
485 "DEFAULT_EMAIL_FROM" => ($arSite[
"EMAIL"]<>
''? $arSite[
"EMAIL"] : $DEFAULT_EMAIL_FROM),
486 "LANGUAGE_ID" => $arSite[
'LANGUAGE_ID'],
487 "SITE_ID" => $arSite[
'LID'],
488 "SITE_DIR" => $arSite[
'DIR'],
490 return \CEvent::$EVENT_SITE_PARAMS[$site_id];
495 "SITE_NAME" => $SITE_NAME,
496 "SERVER_NAME" => $SERVER_NAME,
497 "DEFAULT_EMAIL_FROM" => $DEFAULT_EMAIL_FROM