1. Check the format with filter_var
filter_var($email, FILTER_VALIDATE_EMAIL) is the built-in way to validate email format in PHP. It rejects malformed input such as missing @ signs, spaces, or empty local parts. Avoid hand-written regular expressions: they usually reject valid addresses (plus tags, long top-level domains) or accept invalid ones. filter_var does not accept Unicode in the local part unless you pass FILTER_FLAG_EMAIL_UNICODE (PHP 7.1+). A passing result only means the string looks like an email address.
- Trim surrounding whitespace before validating, and store the address as the user typed it.
- Do not lowercase the local part for delivery; only the domain is case-insensitive.
- Format validation is free and instant, so run it first and give the user immediate feedback.
2. Check that the domain accepts mail with checkdnsrr
A correctly formatted address is still useless if its domain cannot receive mail. checkdnsrr($domain, 'MX') returns true when the domain publishes MX records. Mail servers fall back to the domain's A or AAAA record when no MX exists, so check those too. Convert internationalized domains with idn_to_ascii (the intl extension) first. Use getmxrr() if you also need the mail host names. DNS lookups add latency and can time out, so treat a lookup failure as inconclusive, not invalid.
<?php
function email_syntax_ok(string $email): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
function email_domain_routes_mail(string $email): bool {
$at = strrpos($email, '@');
if ($at === false) {
return false;
}
$domain = substr($email, $at + 1);
if (function_exists('idn_to_ascii')) {
$ascii = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
if ($ascii === false) {
return false;
}
$domain = $ascii;
}
// A trailing dot stops the resolver from appending a search domain.
$fqdn = rtrim($domain, '.') . '.';
// No MX record: SMTP falls back to the domain's A/AAAA address.
return checkdnsrr($fqdn, 'MX') || checkdnsrr($fqdn, 'A') || checkdnsrr($fqdn, 'AAAA');
}
$email = 'person@example.com';
var_dump(email_syntax_ok($email), email_domain_routes_mail($email));
3. Why PHP alone cannot confirm the mailbox
A domain with valid MX records can still reject the specific address, and some domains accept every address (catch-all). The only way to ask a mail server about one mailbox is an SMTP conversation on port 25. Most hosting providers and clouds block outbound port 25, large mailbox providers rate-limit or block unknown senders, and probing from your web server can get its IP listed. Mailbox-level checks are why email verification services exist.
4. Verify the mailbox through the ValidatedMails API
Call the API from your PHP backend after the format check passes. It runs syntax, domain, disposable, role, and accept-all checks plus a live mailbox check, and returns valid, risky, invalid, or unknown. Keep the API key in an environment variable, never in browser code. Unknown results are not charged.
<?php
function verify_email(string $email): array {
$ch = curl_init('https://api.validatedmails.com/validate');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('VALIDATEDMAILS_API_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode(['email' => $email]),
]);
$body = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($body === false || $httpStatus !== 200) {
return ['status' => 'unknown']; // treat as inconclusive; do not retry blindly
}
return json_decode($body, true) ?? ['status' => 'unknown'];
}
$result = verify_email('person@example.com');
switch ($result['status'] ?? 'unknown') {
case 'valid': /* accept */ break;
case 'invalid': /* ask the user to correct the address */ break;
default: /* risky or unknown: review or confirm */ break;
}
Which check should you use?
Use the layers together, from cheapest to most thorough.
- Signup and contact forms: filter_var in the request handler, then an API check before creating the account.
- Importing a list: filter_var and checkdnsrr to drop obvious failures, then bulk verification for the rest.
- Ownership: none of these checks proves the person controls the inbox. Send a confirmation link for that.