This project is a simple PHP script to check if an email address is valid for a given domain. It verifies the existence of the email address using DNS and SMTP protocols.
- HTML form for user input of email address.
- Checks the MX records of the email domain.
- Verifies the validity of the email address via SMTP.
- PHP 5.6 or higher
- A web server (Apache, Nginx, etc.)
- Download or copy the project files.
- Place the files in the root directory of your web server.
- Open the
index.php
file in your browser.
- Open the project in your browser.
- Enter the email address you want to verify in the form.
- Click the "Check" button.
- See the message indicating whether the email address is valid or not.
Below is the main PHP code of the project:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Email Validation</title>
</head>
<body>
<form method="post" action="">
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required>
<input type="submit" value="Check">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
function checkEmail($email) {
list($user, $domain) = explode('@', $email);
if (getmxrr($domain, $mxHosts, $mxWeights)) {
foreach ($mxHosts as $host) {
$connection = fsockopen($host, 25, $errno, $errstr, 5);
if ($connection) {
fgets($connection, 1024);
fputs($connection, "HELO " . $domain . "\r\n");
fgets($connection, 1024);
fputs($connection, "MAIL FROM: <test@" . $domain . ">\r\n");
fgets($connection, 1024);
fputs($connection, "RCPT TO: <" . $email . ">\r\n");
$response = fgets($connection, 1024);
fputs($connection, "QUIT\r\n");
fclose($connection);
if (strpos($response, '250') !== false) {
return true;
} else {
return false;
}
}
}
} else {
return false;
}
}
if (checkEmail($email)) {
echo "<p>Email address is valid.</p>";
} else {
echo "<p>Email address is invalid.</p>";
}
}
?>
</body>
</html>