cakephp/cakephp · error · SocketException

Cannot use an empty client name.

Error message

Cannot use an empty client name.

What it means

Thrown by _connect() when the 'client' config key is present but empty. The client name is the hostname sent in the EHLO/HELO greeting and is used by some servers for SPF/reverse-DNS checks. CakePHP refuses an explicitly-set-but-empty value because it would send a malformed greeting.

Solutions

  1. Provide a real fully-qualified hostname, e.g. 'client' => 'mail.example.com'.
  2. If you have no specific client name, remove the 'client' key entirely so CakePHP falls back to env('HTTP_HOST') or 'localhost'.
  3. Sanitize env-derived values: use $client ?: null before injecting into transport config.
  4. Fix the .env/deployment secret so SMTP_CLIENT is unset rather than empty when unused.

Example fix

// before
new SmtpTransport(['host' => 'smtp.example.com', 'client' => $envClient]); // $envClient = ''
// after
new SmtpTransport(['host' => 'smtp.example.com', 'client' => ($envClient ?: null)]); // falls back to HTTP_HOST/localhost when null and key absent
Defensive patterns

Strategy: validation

Validate before calling

$client = $config['client'] ?? null;
if (array_key_exists('client', $config) && (!is_string($client) || trim($client) === '')) {
    unset($config['client']); // let CakePHP fall back to HTTP_HOST / localhost
}

Type guard

function hasNonEmptyClient(array $config): bool {
    return !array_key_exists('client', $config) || (is_string($config['client']) && $config['client'] !== '');
}

Try / catch

try {
    $email->send();
} catch (\Cake\Network\Exception\SocketException $e) {
    if ($e->getMessage() === 'Cannot use an empty client name.') {
        // rebuild transport without the client key and retry
    }
}

Prevention

When it happens

Trigger: Configuration like ['client' => ''] — e.g. env('SMTP_CLIENT') resolving to an empty string and being passed straight into the transport config — then triggering _connect() via send() or connect().

Common situations: Reading the client hostname from an env var that is set to '' in production; a YAML/dotenv parser converting an unset value to empty string instead of null; copy-pasted config with client => '' left in place.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/1cf25bd7ae06147f. Report an issue: GitHub.

Appendix: source

Thrown at src/Mailer/Transport/SmtpTransport.php:317

     * Connect to SMTP Server
     *
     * @return void
     * @throws \Cake\Network\Exception\SocketException
     */
    protected function _connect(): void
    {
        $this->_generateSocket();
        if (!$this->_socket->connect()) {
            throw new SocketException('Unable to connect to SMTP server.');
        }
        $this->_smtpSend(null, '220');

        $config = $this->_config;

        $host = 'localhost';
        if (isset($config['client'])) {
            if (empty($config['client'])) {
                throw new SocketException('Cannot use an empty client name.');
            }
            $host = $config['client'];
        } else {
            $httpHost = env('HTTP_HOST');
            if (is_string($httpHost) && strlen($httpHost)) {
                [$host] = explode(':', $httpHost);
            }
        }

        try {
            $this->_smtpSend("EHLO {$host}", '250');
            if ($config['tls']) {
                $this->_smtpSend('STARTTLS', '220');
                $this->_socket->enableCrypto('tls');
                $this->_smtpSend("EHLO {$host}", '250');
            }
        } catch (SocketException $e) {
            if ($config['tls']) {

View on GitHub (pinned to 1128eba9b0)