• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/mail/mail.php
  • Класс: BitrixMainMailMail
  • Вызов: Mail::setAttachment
public function setAttachment()
{
	$files = $this->attachment;
	if(is_array($this->filesReplacedFromBody))
	{
		$files = array_merge($files, array_values($this->filesReplacedFromBody));
	}

	$summarySize = 0;
	if(!empty($files))
	{
		foreach($files as $attachment)
		{
			$isLimitExceeded = $this->isFileLimitExceeded(
				!empty($attachment["SIZE"]) ? $attachment["SIZE"] : strlen($attachment["CONTENT"] ?? ''),
				$summarySize
			);

			if (!$isLimitExceeded)
			{
				try
				{
					$fileContent = $attachment["CONTENT"] ?? File::getFileContents($attachment["PATH"]);
				}
				catch (Exception $exception)
				{
					$fileContent = '';
				}
			}
			else
			{
				$fileContent = '';
			}

			$isLimitExceeded = $this->isFileLimitExceeded(
				strlen($fileContent),
				$summarySize
			);
			if ($isLimitExceeded)
			{
				$attachment["NAME"] = $attachment["NAME"] . '.txt';
				$attachment['CONTENT_TYPE'] = 'text/plain';
				$fileContent = str_replace(
					['%name%', '%limit%'],
					[
						$attachment["NAME"],
						round($this->settingMaxFileSize / 1024 / 1024, 1),
					],
					'This is not the original file. The size of the original file `%name%` exceeded the limit of %limit% MB.'
				);
			}

			if(isset($attachment['METHOD']))
			{
				$name = $this->encodeSubject($attachment["NAME"], $attachment['CHARSET']);
				$part = (new Part())
					->addHeader('Content-Type', $attachment['CONTENT_TYPE'] .
							"; name="$name"; method=".$attachment['METHOD']."; charset=".$attachment['CHARSET'])
					->addHeader('Content-Disposition', "attachment; filename="$name"")
					->addHeader('Content-Transfer-Encoding', 'base64')
					->addHeader('Content-ID', "<{$attachment['ID']}>")
					->setBody($fileContent);
			}
			else
			{
				$name = $this->encodeSubject($attachment["NAME"], $this->charset);
				$part = (new Part())
					->addHeader('Content-Type', $attachment['CONTENT_TYPE'] . "; name="$name"")
					->addHeader('Content-Disposition', "attachment; filename="$name"")
					->addHeader('Content-Transfer-Encoding', 'base64')
					->addHeader('Content-ID', "<{$attachment['ID']}>")
					->setBody($fileContent);
			}

			if ($this->multipartRelated && $this->isAttachmentImage($attachment, true))
			{
				$this->multipartRelated->addPart($part);
			}
			else
			{
				$this->multipart->addPart($part);
			}
		}
	}
}