---
title: "PHP email validation: filter_var + domain check"
url: "https://lookvalid.com/blog/tutorial/php-email-validation-api.md"
canonical_url: "https://lookvalid.com/blog/tutorial/php-email-validation-api"
last_updated: "2026-11-12"
type: "how-to"
summary: "Validate an email address in PHP in two layers: filter_var for syntax, then a cURL call to the email domain check for MX, disposable and role-mailbox signals."
related: ["https://lookvalid.com/products/email-domain-check.md", "https://lookvalid.com/tools/email-domain-checker.md", "https://lookvalid.com/docs/quickstart.md", "https://lookvalid.com/glossary/disposable-email.md"]
install: "claude mcp add --transport http lookvalid https://lookvalid.com/api/mcp"
---

# PHP email validation: filter_var + domain check

> **For AI agents:** the complete index of LookValid pages is [llms.txt](https://lookvalid.com/llms.txt). Every page is also served as markdown at its URL plus .md.

Validate an email address in PHP in two layers: filter_var for syntax, then a cURL call to the email domain check for MX, disposable and role-mailbox signals.

PHP email validation has two jobs that people often fold into one: deciding whether a string is shaped like an address, and deciding whether mail to it has anywhere to go. This tutorial does the first with the built-in `filter_var` and the second with one cURL call to the LookValid email domain check, then turns the answer into a signup decision.

## Layer one: check the syntax with filter_var

`filter_var($email, FILTER_VALIDATE_EMAIL)` returns the string when it passes and `false` when it does not. It checks the value against the `addr-spec` syntax in RFC 822, and it rejects comments, whitespace folding and dotless domain names such as `user@localhost` [1]. If your users have non-ASCII characters before the @ sign, add `FILTER_FLAG_EMAIL_UNICODE`, which PHP has offered since 7.1.0 [1].

The PHP manual is direct about the limit of this layer: email validation is complex, and the only true way to confirm that an address exists is to send mail to it [1]. A syntactically perfect address can still sit on a domain that has no mail server, so the filter is the cheap first gate and not the whole answer.

## Layer two: ask whether the domain can receive mail

The LookValid email domain check takes the address, reads the domain's public DNS and returns whether it publishes MX records, whether it is a known disposable provider, whether the local part is a role mailbox such as info@, and an overall `deliverability_signal` of `good`, `risky` or `undeliverable`. It never contacts the mailbox, so `smtp_checked` is always false. A found result costs 1 credit; an address the API cannot resolve comes back with `found: false` and costs nothing.

The request is one POST with a JSON body. In PHP the cURL extension needs a handful of options. `CURLOPT_POSTFIELDS` carries the body and `CURLOPT_RETURNTRANSFER` makes `curl_exec()` return the response as a string instead of printing it [2][3]. Set `CURLOPT_TIMEOUT` as well: its default of 0 means a transfer never times out [2]. `curl_exec()` returns `false` on a transport failure, so that case needs its own branch, separate from an HTTP error status [3].

## The full function

A `429` status means the client sent too many requests, and the server says how long to wait in the `Retry-After` header, which holds either an HTTP-date or a number of seconds [4][5]. Our API sends whole seconds. cURL hands each response header line to a `CURLOPT_HEADERFUNCTION` callback, which must return the number of bytes it handled [2], so the function below reads `Retry-After` there. The body is decoded with `JSON_THROW_ON_ERROR`, available since PHP 7.3, so a malformed response raises an exception instead of returning `null` [6].

Validate syntax, check the domain, retry on 429

```php
<?php
declare(strict_types=1);

const LOOKVALID_API = 'https://YOUR_API_HOST';

function lookupDomain(string $email, int $maxAttempts = 4): array
{
    for ($attempt = 1; ; $attempt++) {
        $retryAfter = null;
        $ch = curl_init(LOOKVALID_API . '/v1/email-domain-check');
        curl_setopt_array($ch, [
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode(['email' => $email], JSON_THROW_ON_ERROR),
            CURLOPT_HTTPHEADER => [
                'Authorization: Bearer ' . getenv('LOOKVALID_API_KEY'),
                'Content-Type: application/json',
            ],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HEADERFUNCTION => function ($handle, string $line) use (&$retryAfter): int {
                if (stripos($line, 'retry-after:') === 0) {
                    $retryAfter = (int) trim(substr($line, 12));
                }
                return strlen($line);
            },
        ]);
        $raw = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

        if ($raw === false) {
            if ($attempt >= $maxAttempts) {
                throw new RuntimeException('network error: ' . curl_error($ch));
            }
            sleep(2 ** $attempt);
            continue;
        }
        if ($status === 429 && $attempt < $maxAttempts) {
            sleep(max(1, $retryAfter ?? 1));
            continue;
        }

        $body = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
        if ($status !== 200) {
            $err = $body['error'] ?? [];
            if (($err['retryable'] ?? false) && $attempt < $maxAttempts) {
                sleep(2 ** $attempt);
                continue;
            }
            throw new RuntimeException(($err['code'] ?? 'error') . ': ' . ($err['message'] ?? 'request failed'));
        }
        return $body;
    }
}

function checkSignupEmail(string $email): array
{
    $email = trim($email);
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        return ['allow' => false, 'why' => 'not shaped like an email address'];
    }
    $result = lookupDomain($email);
    if (!$result['found']) {
        return ['allow' => false, 'why' => $result['reason']];
    }
    $d = $result['data'];
    if ($d['disposable']) {
        return ['allow' => false, 'why' => 'disposable domain'];
    }
    if ($d['deliverability_signal'] === 'undeliverable') {
        return ['allow' => false, 'why' => 'domain accepts no mail'];
    }
    return ['allow' => true, 'review' => $d['deliverability_signal'] === 'risky'];
}

print_r(checkSignupEmail('new.user@example.org'));
```

Run it with `LOOKVALID_API_KEY` exported. The sample address uses example.org, which publishes a null MX record to say it takes no mail, so the check returns `undeliverable` and the function answers `allow` false with the reason "domain accepts no mail".

## Turn the signal into a decision

Treat the three outcomes differently. `found: false` and `undeliverable` are safe to refuse, because the domain cannot receive mail. A `disposable` domain is a policy choice, and most signup forms refuse it. `risky` covers addresses that can receive mail but deserve a second look, such as a role mailbox, so the function lets them through and flags them for review.

What each result means for a signup form

| Result | What it tells you | Usual action |
| --- | --- | --- |
| filter_var returns false | The string is not an address | Ask the person to retype it; no API call is made |
| found false | The domain does not exist in DNS | Ask for another address; nothing is charged |
| deliverability_signal undeliverable | No MX, no address record, or a null MX | Refuse the address |
| disposable true | A known throwaway provider | Refuse or limit the account |
| deliverability_signal risky | A role mailbox such as info@, or a domain with no MX but an address record | Accept and mark for review |
| deliverability_signal good | MX records present and no risk check matched | Accept; the mailbox itself was not checked |

Only a confirmation email proves a mailbox is real and read by the person who typed it. The domain check removes the addresses that could never work, so your confirmation emails go to fewer dead ends. For the same flow in another language see [verify email domains in Node.js](https://lookvalid.com/blog/tutorial/nodejs-email-domain-verification.md), and for what separates a valid address from a deliverable one see [what makes an email address valid](https://lookvalid.com/blog/industry/what-makes-an-email-address-valid.md).

## Run it

1. Check that PHP 8 has the cURL extension: `php -m` lists `curl`.
2. Export `LOOKVALID_API_KEY` in your shell, then save the code as `check.php`.
3. Run `php check.php` and read the `allow` and `why` keys.

Read the [email domain check product page](https://lookvalid.com/products/email-domain-check.md) for every response field, try an address in the [email domain checker](https://lookvalid.com/tools/email-domain-checker.md) without a key, or begin with the [quickstart](https://lookvalid.com/docs/quickstart.md).

## Sources

1. [PHP manual: Validation Filters (FILTER_VALIDATE_EMAIL)](https://www.php.net/manual/en/filter.constants.validation.php), read 2026-11-05
2. [PHP manual: cURL predefined constants](https://www.php.net/manual/en/curl.constants.php), read 2026-11-05
3. [PHP manual: curl_exec](https://www.php.net/manual/en/function.curl-exec.php), read 2026-11-05
4. [RFC 9110: HTTP Semantics (section 10.2.3, Retry-After)](https://www.rfc-editor.org/rfc/rfc9110.html), read 2026-11-05
5. [RFC 6585: Additional HTTP Status Codes (section 4, 429 Too Many Requests)](https://www.rfc-editor.org/rfc/rfc6585.html), read 2026-11-05
6. [PHP manual: json_decode](https://www.php.net/manual/en/function.json-decode.php), read 2026-11-05

By LookValid engineering. Published 2026-11-06, updated 2026-11-06.

## Related

- [Email Domain Check API](https://lookvalid.com/products/email-domain-check.md)
- [Free email domain checker (MX, SPF, DMARC)](https://lookvalid.com/tools/email-domain-checker.md)
- [Quickstart](https://lookvalid.com/docs/quickstart.md)
- [What is Disposable email?](https://lookvalid.com/glossary/disposable-email.md)
