PHPMailer\PHPMailer

SMTP::sendCommand()protectedWP 1.0

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

Method of the class: SMTP{}

No Hooks.

Return

true|false. True on success

Usage

// protected - for code of main (parent) or child class
$result = $this->sendCommand( $command, $commandstring, $expect );
$command(string) (required)
The command name - not sent to the server
$commandstring(string) (required)
The actual command to send
$expect(int|array) (required)
One or more expected integer success codes

SMTP::sendCommand() code WP 6.5.2

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) || (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('/^([\d]{3})[ -](?:([\d]\\.[\d]\\.[\d]{1,2}) )?/', $this->last_reply, $matches)) {
        $code = (int) $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 = (int) 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, true)) {
        $this->setError(
            "$command command failed",
            $detail,
            $code,
            $code_ex
        );
        $this->edebug(
            'SMTP ERROR: ' . $this->error['error'] . ': ' . $this->last_reply,
            self::DEBUG_CLIENT
        );

        return false;
    }

    //Don't clear the error store when using keepalive
    if ($command !== 'RSET') {
        $this->setError('');
    }

    return true;
}