• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/lib/smtp.php
  • Класс: BitrixMailSmtp
  • Вызов: Smtp::authenticate
public function authenticate(&$error)
{
	$error = null;

	if (!$this->connect($error))
	{
		return false;
	}

	$mech = false;

	if ($capabilities = preg_grep('/^ AUTH x20 /ix', $this->sessCapability))
	{
		if ($this->isOauth)
		{
			$mech = 'oauth';
		}
		else if (preg_grep('/ x20 PLAIN ( x20 | $ ) /ix', $capabilities))
		{
			$mech = 'plain';
		}
		else if (preg_grep('/ x20 LOGIN ( x20 | $ ) /ix', $capabilities))
		{
			$mech = 'login';
		}
	}

	if (!$mech)
	{
		$error = $this->errorMessage(array(Smtp::ERR_AUTH, Smtp::ERR_AUTH_MECH));
		return false;
	}

	if ($mech === 'oauth')
	{
		$token = HelperOAuth::getTokenByMeta($this->options['password']);
		if (empty($token))
		{
			$error = $this->errorMessage(array(Smtp::ERR_AUTH, Smtp::ERR_AUTH_MECH));
			return false;
		}
		$formatted = sprintf("user=%sx01auth=Bearer %sx01x01", $this->options['login'], $token);
		$response = $this->executeCommand(sprintf("AUTH XOAUTH2x00%s", base64_encode($formatted)), $error);
	}
	else if ($mech === 'plain')
	{
		$response = $this->executeCommand(
			sprintf(
				"AUTH PLAINx00%s",
				base64_encode(sprintf(
					"x00%sx00%s",
					Encoding::convertEncoding($this->options['login'], $this->options['encoding'], 'UTF-8'),
					Encoding::convertEncoding($this->options['password'], $this->options['encoding'], 'UTF-8')
				))
			),
			$error
		);
	}
	else
	{
		$response = $this->executeCommand(sprintf(
			"AUTH LOGINx00%sx00%s",
			base64_encode($this->options['login']),
			base64_encode($this->options['password'])
		), $error);
	}

	if ($error)
	{
		$error = $error == Smtp::ERR_COMMAND_REJECTED ? null : $error;
		$error = $this->errorMessage(array(Smtp::ERR_AUTH, $error), $response ? trim(end($response)) : null);

		return false;
	}

	return true;
}