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: wes@gmail.com would check to see if gmail.com was a real domain). Those are both good checks, but jkdlfdslkfsdfk@gmail.com 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
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

Link to comment
Share on other sites

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:<support@microsoft.com>');
        $erg = send_command($fp, 'RCPT TO:<'.$eMail.'>');
        fclose($fp);
        $code = intval(substr($erg, 0, 3));
    }
    return $code;
}

 

Scott.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.