How to check if an email address is real or valid using PHP
Stefan Bogdanescu
Founder & Senior Architect
The Reality of Email Validation in PHP: What You Can and Cannot Do
Finding reliable ways to validate an email address is a perennial challenge for developers. As you’ve discovered, simply checking if a domain has MX records doesn't guarantee that the actual mailbox exists—this is the core limitation of trying to perform deep email verification using only local server-side scripting like raw PHP.
As senior developers, we need to understand the difference between syntactical validation (checking if the string looks like an email) and deliverability validation (checking if the address is active). This post will break down what can realistically be achieved with PHP and where you must look beyond it for true certainty.
The Limits of Pure PHP Validation
The attempt to verify an email address using only native PHP functions often leads to false positives or negatives because email existence is a dynamic state managed by various mail servers, not static DNS records alone.
Your initial approach—checking checkdnsrr() for MX records—is a good first step for domain legitimacy. However, as you noted, this only confirms that the domain can receive mail; it does not confirm that the specific user account exists on that domain. Trying to verify mailbox existence directly via PHP without accessing complex SMTP protocols or third-party APIs is often impractical and unreliable due to security restrictions and dynamic server configurations.
Level 1: Syntactical and Format Validation (The Essential First Step)
Before attempting deeper checks, the most crucial step is ensuring the input adheres to standard email formatting rules. This is where regular expressions (regex) shine in PHP. A properly formatted email address must contain a local part, an '@' symbol, and a domain part with a top-level domain (TLD).
Here is a more robust way to check the structure of an email using regex:
<?php
function isValidEmailFormat(string $email): bool {
// A standard, reasonably strict regex for basic email format validation.
$pattern = '/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';
return preg_match($pattern, $email) === 1;
}
$inputEmail = $_POST['email'] ?? '';
if (!empty($inputEmail) && isValidEmailFormat($inputEmail)) {
echo "The email format is syntactically correct.";
} else {
echo "Invalid email format detected.";
}
?>
This regex ensures the string conforms to established RFC standards for email structure. This level of validation handles 90% of common input errors instantly and efficiently on the server side.
Level 2: Domain Existence Check (Expanding Your Initial Logic)
To move beyond just format, you can enhance your domain check by combining DNS lookups with robust error handling. While PHP itself doesn't offer a single function for this, leveraging built-in functions like gethostbyname() or the dns_get_record() family of functions (depending on your PHP version and environment) combined with proper exception handling is key.
If you are building a complex application, frameworks like Laravel provide excellent tools for managing these types of data validations, abstracting away much of the raw system interaction, which is a great principle to adopt when architecting robust systems.
The Gold Standard: True Deliverability Verification
To check if an email address is truly real (i.e., deliverable), you must interact with the mail infrastructure itself. This requires one of two methods:
- SMTP Verification: Attempting a connection and login handshake with the recipient's mail server (e.g., using PHP's
stream_socket_clientor specialized libraries) is extremely complex, often blocked by modern security measures, and prone to being flagged as spam if done improperly. - Verification Links (The Best Practice): The industry standard for true validation is to send a confirmation email containing a unique token to the address. If the user clicks a link in that email, you have confirmed two things: the address exists and the user has access to it. This shifts the burden of proof from your server to the recipient's client interaction.
For mission-critical applications, integrating with professional email verification services (like ZeroBounce, Hunter, or Mailgun) is the most practical and reliable solution. These external APIs handle the complex, constantly changing world of mail servers, spam checks, and deliverability metrics far better than any custom PHP script ever could.
Conclusion
In summary, you cannot reliably verify the existence of a user account on an arbitrary email domain using only native PHP functions. Focus your efforts where they are most effective:
- Always validate the format first using strong regex patterns.
- Use DNS lookups (like MX record checks) to ensure the domain is reachable.
- For true deliverability, rely on established, dedicated third-party email verification APIs rather than attempting complex, fragile SMTP communication directly in your application logic.
By understanding these boundaries, you move from writing simple scripts to building resilient, production-grade systems.
Note: Blog content is currently available in English.