function SMTP::sendCommand

5.x SMTP.php protected SMTP::sendCommand($command, $commandstring, $expect)

Send a command to an SMTP server and check its return code.

Parameters

string $command The command name - not sent to the server:

string $commandstring The actual command to send:

int|array $expect One or more expected integer success codes:

Return value

bool True on success

File

inc/PHPMailer/src/SMTP.php, line 910

Class

SMTP
PHPMailer RFC821 SMTP email transport class. Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.

Code

protected function sendCommand($command, $commandstring, $expect) {
  if (!$this->connected()) {
    $this->setError("Called $command without being connected");

    return false;
  }
  //Reject line breaks in all commands
  if (strpos($commandstring, "\n") !== false or strpos($commandstring, "\r") !== false) {
    $this->setError("Command '$command' contained line breaks");

    return false;
  }
  $this->client_send($commandstring . static::LE, $command);

  $this->last_reply = $this->get_lines();
  // Fetch SMTP code and possible error code explanation
  $matches =[];
  if (preg_match('/^([0-9]{3})[ -](?:([0-9]\\.[0-9]\\.[0-9]{1,2}) )?/', $this->last_reply, $matches)) {
    $code = $matches[1];
    $code_ex = (count($matches) > 2 ? $matches[2] : null);
    // Cut off error code from each response line
    $detail = preg_replace("/{$code}[ -]" . ($code_ex ? str_replace('.', '\\.', $code_ex) . ' ' : '') . '/m', '', $this->last_reply);
  }
  else {
    // Fall back to simple parsing if regex fails
    $code = substr($this->last_reply, 0, 3);
    $code_ex = null;
    $detail = substr($this->last_reply, 4);
  }

  $this->edebug('SERVER -> CLIENT: ' . $this->last_reply, self::DEBUG_SERVER);

  if (!in_array($code, (array) $expect)) {
    $this->setError("$command command failed", $detail, $code, $code_ex);
    $this->edebug('SMTP ERROR: ' . $this->error['error'] . ': ' . $this->last_reply, self::DEBUG_CLIENT);

    return false;
  }

  $this->setError('');

  return true;
}