Validating email (Advance)
Following is the function wich attempts to verify and email address by contacting a mail exchenager. Registered mail exchangers are requested from the domain controller first, then the exact domain itself. The error argument will contain relevant text if the address could not be verified
<?php
/**
*Email Class
*
* Permits email to be sent using Mail, Sendmail, or SMTP.
*
* @package email ystem
*/
var $fromadd="";
var $mailsubject="";
function Emailverify($address, &$error)
{
/*
Function veryEmail
** Input: String address, Referenace error
** Output: boolean
** Descrption: Attempts to verify and email address by contacting a mail exchenager. Registered mail
** exchangers are requested from the domain controller first, then the exact domain itself. The error argument will
** contain relevant text if the address could not be verified
*****/
$mxhost = array();
$mxweight = array();
list($user, $domain)=split("@",$address,2);
//make sure the domain has a mail exchanger
//should use dns_check_record insted of checkdnsrr in php5
if(checkdnsrr($domain, "MX"))
{
//get mail exchanger records
if(!getmxrr($domain,$mxhost, $mxweight)) //dns_get_mx in php5
{
$error="Could not retrieve mail exchanges !<br>\n";
return false;
}
}
else
{
//if not mail exhanger , may be the host itself
//will accept mail
$mxhost[] = $domain;
$mxweight[] = 1;
}
//create sorted array of hosts
$weighted_host = array();
for($i=0; $i<count($mxhost); $i++)
{
$weighted_host[($mxweight[$i])] = $mxhost[$i];
}
ksort($weighted_host);
//loop over each host
foreach($weighted_host as $host)
{
//connect to host on SMTP port
if(!($fp = fsockopen($host, 25)))
{
//couldn't conntect to this host, but
//the next might work
continue;
}
/*
** skip over "220" message
** give up if no response for 10 seconds
*/
stream_set_blocking($fp,FALSE);
$stopTime=time()+10;
$gotREsponse=FALSE;
while(TRUE)
{
//try to get a line from mail server
$line = fgets($fp,1024);
if(substr($line, 0, 3)=="220")
{
//reset timer
$stopTime=time() + 10;
$gotResponse = TRUE;
}
elseif(($line == "") AND ($gotResponse))
{
break;
}
elseif(time() > $stopTime)
{
break;
}
}
if(!$gotResponse)
{
//this host was unresponive but may be the next will be better
continue;
}
stream_set_blocking($fp, TRUE);
//sign in
fputs($fp, "HELO {$_SERVER['SERVER_NAME']}\r\n");
fgets($fp, 1024);
//set from
fputs($fp, "MAIL FROM: ". "<httpd@{$_SERVER['SERVER_NAME']}>\r\n");
fgets($fp, 1024);
//try address
fputs($fp,"RCPT TO: <$address>\r\n");
$line=fgets($fp,1024);
//close conntection
fputs($fp, "QUIT\r\n");
fclose($fp);
if(substr($line,0,3)!="250")
{
//mail server doesn't recognize this address so its bad
$error = $line;
return false;
}
else
{
//address recognized
return true;
}
}
$error = "unable to reach a mail exchanger !";
return false;
}//end ofEmailverify
function sendHTMLEmail($from, $to, $subject, $message)
{
/***
Function: sendHTMLEmail
Input: $from: from address, $to: to address, $message, $message to be sent
Output:
Description:
****/
//add from header
$headers = "From: {$from}\r\n";
//specify MIME version 1.0
$headers.= "MIME-Version: 1.0\r\n";
//unique boundr
$boundry=uniqid(BOUNDRY");
//tell email clint this email contais alternate versions
$headers.= "Content-Type: multipart/alternative; boundary = $boundry\r\n\r\n";
//plain text version of message
/*$headers.= "--$boundry\r\n".
"Content-Type: text/plain; charset=UTF-7\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers.= chunk_split(base64_encode("This is the plain text verion!"));*/
//HTML version of message
$headers.= "--$boundry\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n";
//"Content-Transfer-Encoding: base64\r\n\r\n";
$headers.= chunk_split(base64_encode("$message"));
//send message
if(mail("$to", "$subject","$message","$headers")) return true; else return false;
}//end of sendhtmlemail
function sendTextEmail($from, $to, $subject, $message)
{
/***
Function: sendTextEmail
Input: as above
Output:
Description:
****/
//add from header
$headers = "From: {$from}\r\n";
//specify MIME version 1.0
$headers.= "MIME-Version: 1.0\r\n";
//unique boundr
$boundry=uniqid("BOUNDRY");
//tell email clint this email contais alternate versions
$headers.= "Content-Type: multipart/alternative". "; boundary = $boundary\r\n\r\n";
//plain text version of message
$headers.= "--$boundry\r\n".
"Content-Type: text/plain; charset=UTF-7\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers.= chunk_split(base64_encode("$message"));
/*/HTML version of message
$headers.= "--$boundry\r\n" .
"Content_Type: text/html; charset=UTF-7\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers.= chunk_split(base64_encode("This is html version"));*/
//send message
if(mail("$to", "$subject","$message","$headers"))
return true;
else
return false;
}//end of sendtextemail
function sendmail($address=array())
{
//
}//end of sendemail
function getadminemail()
{
/* description: get the admin email and subject from datbase */
$sql1="select email from ews_esignupsetup Limit 1";
$result=dbQuery($sql1);
if(dbNumRows($result)>0)
{
while($row=dbFetchArray($result))
{
return $row["email"];
}
}
else
{
return "ANY@EMAILADDRESS.COM";
}
dbFreeResult($result);
} //end getadminemail
}
?>