I have a minor problem with this script. It works well, but I need to add one more component to it. This script will verify a users email address. I would like for it to try to validate the first time, and if the email is invalid, then display an error message. It currently does this... But with the second attempt, I would like to bypass this script all together. What would I need to do to bypass this script on the second try?
Thanks in advance!
Here is the script:
function validate_email($email) {
global $vbulletin;
$b_user_valid = false;
// validate email address syntax
if (eregi($vbulletin->options['os_email_exp'], $email) or $vbulletin->options['os_email_exp'] == '') {
// get mx addresses by getmxrr
$mailparts = explode("@",$email);
$hostname = $mailparts[1];
if (getmxrr($hostname,$mx_records,$mx_weight)) {
// does the host support fsockopen, if not, no point in going futher
if (!$vbulletin->options['os_email_fsockopen_support']) {
$b_user_valid = true;
} else {
// copy mx records and weight into array $mxs
$mxs=array();
for($i = 0;$i < count($mx_records);$i++){
$mxs[$mx_records[$i]] = $mx_weight[$i];
}
asort($mxs); // sort by MX weight
reset($mxs);
$b_user_valid = $vbulletin->options['os_email_host_timeout_valid'];
$b_server_found = false;
// loop through the mx records and look for the user
while (list($mx_host, $mx_weight) = each($mxs)) {
if (!$b_server_found) {
//try connection on port 25
$fp = @fsockopen($mx_host,25, $errno, $errstr, $vbulletin->options['os_email_host_timeout']);
if ($fp) {
$b_server_found = true;
$ms_resp = '';
// say HELO to mailserver, use our server, some mailhosts won't like a fake
$ms_resp.=send_command($fp, 'HELO '.$_SERVER['SERVER_NAME']);
// initialize sending mail, use our mail address
$ms_resp.=send_command($fp, 'MAIL FROM:<'.$vbulletin->options['webmasteremail'].'>');
// try receipent address, will return 250 when ok..
$rcpt_text=send_command($fp, 'RCPT TO:<'.$email.'>');
$ms_resp.=$rcpt_text;
$b_user_valid = substr( $rcpt_text, 0, 3) == '250';
// quit mail server connection
$ms_resp.=send_command($fp, 'QUIT');
fclose($fp);
} else {
// echo "ERROR: $errno - $errstr<br />\n";
}
}
}
}
}
}
return $b_user_valid;
}
function send_command($fp, $out){
fwrite($fp, $out . "\r\n");
return get_data($fp);
}
function get_data($fp){
$s="";
stream_set_timeout($fp, 2);
for($i=0;$i<2;$i++)
$s.=fgets($fp, 1024);
return $s;
}
// support windows platforms
if (!function_exists ('getmxrr') ) {
function getmxrr($hostname, &$mxhosts, &$mxweight) {
if (!is_array ($mxhosts) ) {
$mxhosts = array ();
}
if (!empty ($hostname) ) {
$output = "";
@exec ("nslookup.exe -type=MX ".escapeshellarg($hostname), $output);
$imx=-1;
foreach ($output as $line) {
$imx++;
$parts = "";
if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
$mxweight[$imx] = $parts[1];
$mxhosts[$imx] = $parts[2];
}
}
return ($imx!=-1);
}
return false;
}
}
if (!validate_email($vbulletin->GPC['email'])) {
$userdata->error('os_email_notvalid');
}