Skip to content

Instantly share code, notes, and snippets.

@scoutman57
Last active August 29, 2015 14:20
Show Gist options
  • Save scoutman57/1d7b73006f98816eb56e to your computer and use it in GitHub Desktop.
Save scoutman57/1d7b73006f98816eb56e to your computer and use it in GitHub Desktop.
Validate domain or email address
<?php
/**
* @param $domain
* @return mixed
*/
function validateDomain($domain)
{
$ip = filter_var(gethostbyname($domain), FILTER_VALIDATE_IP);
// Will return the IP address if or false if the domain is not valid
return $ip;
}
$emailAddress = '[email protected]';
/**
* @param $emailAddress
* @return mixed
*/
function validateEmailAddress($emailAddress)
{
$validEmail = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
// Will return the email address or false if the email is not valid
return $validEmail;
}
/**
* @var string $toemail Email address to be checked
* @var string $fromemail A valid email address to provide the server
* @var bool $getdetails Get actual details fo the connection
*
* @return ezcConsoleOptionStringNotWellformedException | string
* Will return "valid" if the email is valid
* Will return "invalid" if the email is invalid
*
* Some mail servers will silentlty reject the test message, to prevent
* spammers from checking against their users' emails and filter the
* valid emails, so this function might not work properly with all mail servers.
*/
function verifyEmail($toemail, $fromemail, $getdetails = false)
{
$email_arr = explode("@", $toemail);
$domain = array_slice($email_arr, -1);
$domain = $domain[0];
// Trim [ and ] from beginning and end of domain string, respectively
$domain = ltrim($domain, "[");
$domain = rtrim($domain, "]");
if ("IPv6:" == substr($domain, 0, strlen("IPv6:"))) {
$domain = substr($domain, strlen("IPv6") + 1);
}
$mxhosts = array();
if (filter_var($domain, FILTER_VALIDATE_IP)) {
$mx_ip = $domain;
} else {
getmxrr($domain, $mxhosts, $mxweight);
}
if (!empty($mxhosts)) {
$mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
} else {
if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$record_a = dns_get_record($domain, DNS_A);
} elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$record_a = dns_get_record($domain, DNS_AAAA);
}
if (!empty($record_a)) {
$mx_ip = $record_a[0]['ip'];
} else {
$result = "invalid";
$details .= "No suitable MX records found.";
return ((true == $getdetails) ? array($result, $details) : $result);
}
}
$connect = @fsockopen($mx_ip, 25);
if ($connect) {
if (preg_match("/^220/i", $out = fgets($connect, 1024))) {
fputs($connect, "HELO $mx_ip\r\n");
$out = fgets($connect, 1024);
$details .= $out . "\n";
fputs($connect, "MAIL FROM: <$fromemail>\r\n");
$from = fgets($connect, 1024);
$details .= $from . "\n";
fputs($connect, "RCPT TO: <$toemail>\r\n");
$to = fgets($connect, 1024);
$details .= $to . "\n";
fputs($connect, "QUIT");
fclose($connect);
if (!preg_match("/^250/i", $from) || !preg_match("/^250/i", $to)) {
$result = "invalid";
} else {
$result = "valid";
}
}
} else {
$result = "invalid";
$details .= "Could not connect to server";
}
if ($getdetails) {
return array($result, $details);
} else {
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment