• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/classes/general/mail.php
  • Класс: CAllMailMessage
  • Вызов: CAllMailMessage::getTextHtmlBlock
static function getTextHtmlBlock(string &$body): array
{
	$messageBody = '';
	$messageBodyHtml = '';

	$boundaryMatches = [];
	preg_match('/content-type: multipart/mixed; [snt]*boundary="(?[w+-]+)"/i', $body, $boundaryMatches);
	if (is_string($boundaryMatches['boundary']))
	{
		$parts = explode('--'. $boundaryMatches['boundary'][0], $body);
		if (count($parts) > 1)
		{
			foreach ($parts as $part)
			{
				preg_match('/content-type: (?text/[html|plain]+); *charset="(?w+-d)"/i', $part, $contentTypeMatches);
				if (isset($contentTypeMatches['contentType']))
				{
					preg_match('/content-transfer-encoding: (?[w]+)/i', $part, $encodeMatches);

					$partBody = self::getPartBody($part);
					if ($partBody === null)
					{
						continue;
					}

					if ($encodeMatches['encode'] === 'base64')
					{
						$partBody = base64_decode($partBody);
					}

					if ($contentTypeMatches['contentType'] === 'text/plain')
					{
						$messageBody = $partBody;
					}

					if ($contentTypeMatches['contentType'] === 'text/html')
					{
						$messageBodyHtml = $partBody;
					}
				}
			}
		}
	}

	return [$messageBody, $messageBodyHtml];
}