therealwesfoster Posted June 19, 2008 Share Posted June 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 19, 2008 Share Posted June 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
ratcateme Posted June 19, 2008 Share Posted June 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.