• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/lib/util.php
  • Класс: BitrixSocialnetworkUtil
  • Вызов: Util::notifyMail
static function notifyMail($fields)
{
	if (!Loader::includeModule('mail'))
	{
		return false;
	}

	if (
		!isset($fields["logEntryId"])
		|| intval($fields["logEntryId"]) <= 0
		|| !isset($fields["userId"])
		|| !isset($fields["logEntryUrl"])
		|| $fields["logEntryUrl"] == ''
	)
	{
		return false;
	}

	if (!is_array($fields["userId"]))
	{
		$fields["userId"] = array($fields["userId"]);
	}

	if (!isset($fields["siteId"]))
	{
		$fields["siteId"] = SITE_ID;
	}

	$nameTemplate = CSite::getNameFormat("", $fields["siteId"]);
	$authorName = "";

	if (!empty($fields["authorId"]))
	{
		$res = CUser::getById($fields["authorId"]);
		if ($author = $res->fetch())
		{
			$authorName = CUser::formatName(
				$nameTemplate,
				$author,
				true,
				false
			);
		}
		else
		{
			$authorName = '';
		}

		if (check_email($authorName))
		{
			$authorName = '"'.$authorName.'"';
		}

		foreach($fields["userId"] as $key => $val)
		{
			if (intval($val) == intval($fields["authorId"]))
			{
				unset($fields["userId"][$key]);
			}
		}
	}

	if (empty($fields["userId"]))
	{
		return false;
	}

	if (
		!isset($fields["type"])
		|| !in_array(mb_strtoupper($fields["type"]), array("LOG_ENTRY", "LOG_COMMENT"))
	)
	{
		$fields["type"] = "LOG_COMMENT";
	}

	$arEmail = BitrixMailUser::getUserData($fields["userId"], $nameTemplate);
	if (empty($arEmail))
	{
		return false;
	}

	$arLogEntry = CSocNetLog::getByID(intval($fields["logEntryId"]));
	if (!$arLogEntry)
	{
		return false;
	}

	$logEntryTitle = str_replace(array("rn", "n"), " ", ($arLogEntry["TITLE"] != '__EMPTY__' ? $arLogEntry["TITLE"] : $arLogEntry["MESSAGE"]));
	$logEntryTitle = truncateText($logEntryTitle, 100);

	switch(mb_strtoupper($fields["type"]))
	{
		case "LOG_COMMENT":
			$mailMessageId = "";
			$mailTemplateType = "SONET_LOG_NEW_COMMENT";
			break;
		default:
			$mailMessageId = "";
			$mailTemplateType = "SONET_LOG_NEW_ENTRY";
	}

	$mailMessageInReplyTo = "";
	$defaultEmailFrom = BitrixMailUser::getDefaultEmailFrom();

	foreach ($arEmail as $userId => $user)
	{
		$email = $user["EMAIL"];
		$nameFormatted = str_replace(array('<', '>', '"'), '', $user["NAME_FORMATTED"]);

		if (
			intval($userId) <= 0
			&& $email == ''
		)
		{
			continue;
		}

		$res = BitrixMailUser::getReplyTo(
			$fields["siteId"],
			$userId,
			'LOG_ENTRY',
			$fields["logEntryId"],
			$fields["logEntryUrl"]
		);
		if (is_array($res))
		{
			list($replyTo, $backUrl) = $res;

			if (
				$replyTo
				&& $backUrl
			)
			{
				$authorName = str_replace(array('<', '>', '"'), '', $authorName);
				CEvent::send(
					$mailTemplateType,
					$fields["siteId"],
					array(
						"=Reply-To" => $authorName.' <'.$replyTo.'>',
						"=Message-Id" => $mailMessageId,
						"=In-Reply-To" => $mailMessageInReplyTo,
						"EMAIL_FROM" => $authorName.' <'.$defaultEmailFrom.'>',
						"EMAIL_TO" => (!empty($nameFormatted) ? ''.$nameFormatted.' <'.$email.'>' : $email),
						"RECIPIENT_ID" => $userId,
						"COMMENT_ID" => (isset($fields["logCommentId"]) ? intval($fields["logCommentId"]) : false),
						"LOG_ENTRY_ID" => intval($fields["logEntryId"]),
						"LOG_ENTRY_TITLE" => $logEntryTitle,
						"URL" => $fields["logEntryUrl"]
					)
				);
			}
		}
	}

	return true;
}