ellite/Wallos · error · Exception

Invalid address (From)

Error message

Invalid address (From): %s

What it means

setFrom() throws 'Invalid address (From): %s' when the From address fails static::validateAddress(). The From address becomes the envelope sender and the From header, so PHPMailer refuses invalid values early. Thrown only when $this->exceptions is true; otherwise returns false after setError.

Solutions

  1. Always call setFrom() with a validated, explicit address: if (PHPMailer::validateAddress($from)) $mail->setFrom($from);
  2. If relying on auto-detection ($auto=true), set an explicit From in CLI/cron contexts where SERVER_NAME is undefined
  3. Check validator strictness: PHPMailer::$validator can be set to 'pcre', 'php', 'html5', or 'eai', or a callable
  4. Verify the address has no trailing punctuation, spaces, or embedded newlines from config files

Example fix

// before
$mail->setFrom($config['from_email'] ?? ''); // empty fails
// after
$from = $config['from_email'] ?? '';
if (!PHPMailer::validateAddress($from)) {
    throw new RuntimeException('Sender address not configured');
}
$mail->setFrom($from, $config['from_name'] ?? '');
Defensive patterns

Strategy: validation

Validate before calling

if (!PHPMailer::validateAddress($from)) { throw new InvalidArgumentException("Invalid From: $from"); } $mail->setFrom($from, $fromName);

Try / catch

try { $mail->send(); } catch (PHPMailer\PHPMailer\Exception $e) { if (str_contains($e->getMessage(), 'From')) { /* fix sender config */ } throw $e; }

Prevention

When it happens

Trigger: $mail->setFrom($addr) or setFrom($addr, $name, $auto) with an empty or malformed $addr; $auto=true also attempts to derive a From address from the server (SERVER_NAME) and validate it, which can fail on CLI or odd hostnames.

Common situations: Hardcoded placeholder From addresses ('noreply@localhost') rejected by a strict validator, missing From entirely while $auto default relies on SERVER_NAME being unset in CLI scripts, corporate domains with unusual but valid characters rejected by the default regex.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of ellite/Wallos@52820e87ca (2026-09-13). Data as JSON: /api/errors/3701608f2197284b. Report an issue: GitHub.

Appendix: source

Thrown at libs/PHPMailer/PHPMailer.php:1324

    {
        $address = trim((string)$address);
        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
        //Don't validate now addresses with IDN. Will be done in send().
        $pos = strrpos($address, '@');
        if (
            (false === $pos)
            || ((!$this->has8bitChars(substr($address, ++$pos)) || !static::idnSupported())
            && !static::validateAddress($address))
        ) {
            $error_message = sprintf(
                '%s (From): %s',
                $this->lang('invalid_address'),
                $address
            );
            $this->setError($error_message);
            $this->edebug($error_message);
            if ($this->exceptions) {
                throw new Exception($error_message);
            }

            return false;
        }
        $this->From = $address;
        $this->FromName = $name;
        if ($auto && empty($this->Sender)) {
            $this->Sender = $address;
        }

        return true;
    }

    /**
     * Return the Message-ID header of the last email.
     * Technically this is the value from the last time the headers were created,
     * but it's also the message ID of the last sent message except in
     * pathological cases.

View on GitHub (pinned to 52820e87ca)