• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/lib/helper/mailbox/imap.php
  • Класс: BitrixMailHelperMailboxImap
  • Вызов: Imap::downloadMessageParts
public function downloadMessageParts(array &$excerpt, MailImapBodyStructure $bodystructure, $flags = Imap::MESSAGE_PARTS_ALL)
{
	if (empty($excerpt['MSG_UID']) || empty($excerpt['DIR_MD5']))
	{
		return false;
	}

	$dirPath = $this->getDirsHelper()->getDirPathByHash($excerpt['DIR_MD5']);
	if (empty($dirPath))
	{
		return false;
	}

	$rfc822Parts = array();

	$select = array_filter(
		$bodystructure->traverse(
			function (MailImapBodyStructure $item) use ($flags, &$rfc822Parts)
			{
				if ($item->isMultipart())
				{
					return;
				}

				$isTextItem = $item->isBodyText();
				if ($flags & ($isTextItem ? Imap::MESSAGE_PARTS_TEXT : Imap::MESSAGE_PARTS_ATTACHMENT))
				{
					// due to yandex bug
					if ('message' === $item->getType() && 'rfc822' === $item->getSubtype())
					{
						$rfc822Parts[] = $item;

						return sprintf('BODY.PEEK[%1$s.HEADER] BODY.PEEK[%1$s.TEXT]', $item->getNumber());
					}

					return sprintf('BODY.PEEK[%1$s.MIME] BODY.PEEK[%1$s]', $item->getNumber());
				}
			},
			true
		)
	);

	if (empty($select))
	{
		return array();
	}

	$parts = $this->client->fetch(
		true,
		$dirPath,
		$excerpt['MSG_UID'],
		sprintf('(%s)', join(' ', $select)),
		$error
	);

	if (false === $parts)
	{
		$this->errors = new MainErrorCollection($this->client->getErrors()->toArray());

		return false;
	}

	foreach ($rfc822Parts as $item)
	{
		$headerKey = sprintf('BODY[%s.HEADER]', $item->getNumber());
		$bodyKey = sprintf('BODY[%s.TEXT]', $item->getNumber());

		if (array_key_exists($headerKey, $parts) || array_key_exists($bodyKey, $parts))
		{
			$partMime = 'Content-Type: message/rfc822';
			if (!empty($item->getParams()['name']))
			{
				$partMime .= sprintf('; name="%s"', $item->getParams()['name']);
			}

			if (!empty($item->getDisposition()[0]))
			{
				$partMime .= sprintf("rnContent-Disposition: %s", $item->getDisposition()[0]);
				if (!empty($item->getDisposition()[1]) && is_array($item->getDisposition()[1]))
				{
					foreach ($item->getDisposition()[1] as $name => $value)
					{
						$partMime .= sprintf('; %s="%s"', $name, $value);
					}
				}
			}

			$parts[sprintf('BODY[%1$s.MIME]', $item->getNumber())] = $partMime;
			$parts[sprintf('BODY[%1$s]', $item->getNumber())] = sprintf(
				"%srnrn%s",
				rtrim($parts[$headerKey], "rn"),
				ltrim($parts[$bodyKey], "rn")
			);

			unset($parts[$headerKey], $parts[$bodyKey]);
		}
	}

	return $parts;
}