Jump to content

Validating an email address


Recommended Posts

How would one go about validating an email address? I'm not talking about validating it with regex, but actually checking to see if it is a valid and working email address.

 

I found this script (Script 1.a) online, but it only checks the email's pattern (regex) and the domain (for example: [email protected] would check to see if gmail.com was a real domain). Those are both good checks, but [email protected] would still return as valid.

 

Script 1.a

<?php
function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }

   list($Username, $Domain) = split("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}
?>

 

 

So I'm wondering if there is a way to check if an email address is valid and working?

 

Thanks,

  Wes

Link to comment
https://forums.phpfreaks.com/topic/110855-validating-an-email-address/
Share on other sites

You would have to open up a socket to the SMTP port of the MX host for the domain of the email address you want to verify, then send the correct commands to verify the address. The SMTP port is usually 25, but can be some other port.  To get the correct syntax, you should read the SMTP RFC's 821 & 2821.

 

Ken

got this from http://php.net/getmxrr

 

<?php
//...
foreach ($mx_records as $mx_host) {
    $code = CheckMX($mx_host, $eMail);
    if ($code == 0)    continue; // host not found
    if ($code == 451)    $code = CheckMX($mx_host, $eMail); // Greylisting
    if ($code == 250)    {
        $ok = true;
        break;
    }
}
//...

function CheckMX($mx_host, $eMail)
{
    $code = 0;
    $fp = @fsockopen($mx_host, 25, $errno, $errstr, 2);
    if ($fp)    {
        send_command($fp, 'HELO microsoft.com');
        send_command($fp, 'MAIL FROM:<[email protected]>');
        $erg = send_command($fp, 'RCPT TO:<'.$eMail.'>');
        fclose($fp);
        $code = intval(substr($erg, 0, 3));
    }
    return $code;
}

 

Scott.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.