Friday, October 28th, 2005Friday, October 28th, 2005

Live Email Validation

Here's a handy bit of PHP code that will take the email variable, lookup the mail server for the domain, log into the mail server and see if it will accept delivery for that address.
<?php
	$split = preg_split('/^(.*)<(.*)>(.*)/', $_REQUEST['email'], -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
	$email = array_pop ($split);
	$split = explode ("@", $email);
	$domain = $split[1];
	
	getmxrr ($domain, $a, $b);
	if ($a) {
		foreach ($a as $key => $val) {
			$c[$b[$key]] = $val;
		}
		ksort ($c);
	} else {
		$c[0] = $domain;
	}
	
	$fp = fsockopen (array_shift($c), 25, $errno, $errstr, 30);
	$return = fgets ($fp, 1024);
	fputs ($fp, "helo none.com\r\n");
	$return = fgets ($fp, 1024);
	fputs ($fp, "mail from: <you@none.com>\r\n");
	$return = fgets ($fp, 1024);
	fputs ($fp, "rcpt to:<" . $email . ">\r\n");
	$return = fgets ($fp, 1024);
	fputs ($fp, "quit");
	
	fclose($fp);
	
	if (substr($return, 0, 3) > 250) {
		echo "Bad email";
	} else {
		echo "Good email";
	}
?>
Okay, okay... the truth is I just got done adapting the GeSHi syntax highlighter into a WordPress plug-in and wanted to make sure it worked, so I needed to post some code of some sort. :)
UpdateSeems to work pretty well if I do say so myself...

2 Responses to “Live Email Validation”

  1. Help Desk Says:

    Page looks a little funny in IE. Side menu seems to be messing it up.

  2. andre Says:

    i made a test site and the script say yes to every email i put or it does not work if i put the wrong domain.

    http://www.yourfreefax.com/testcheckemail.html

    what did i do wrong?

Leave a Reply