Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check email address it's actual address or not

Status
Not open for further replies.

samansk

Technical User
Dec 7, 2000
44
I want to protect forum and other board by checking email address before accept message.Can anybody seggestion to me.? (Don't use send short message to that email address and wait for confirmation or subcribe method)
 
Here this will validate an email address for you. I am not 100% sure if that is what you are asking:

[tt]

<?php
function is_email_valid($email) {
if(eregi(&quot;^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z]){2,3}$&quot;, $email)) return TRUE;
else return FALSE;
}
?>
[/tt]

And you use the function as so:

[tt]
$email = &quot;vikter@epicsoftware.com&quot;;
if (is_email_valid($email)) print &quot;E-Mail OK&quot;;
else print &quot;E-Mail not valid&quot;;

[/tt]

Hope this helps.

-VIc vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Thank you every mouch vikter .My question mean aaa@ddd.com is the mail address and is it have in wolrd?
How to know it 's a real mail address?
 
Sorry I misunderstood you the first time. If you are asking if an email address belongs to a valid domain, I found this code on and it should help.

[tt]
<?php
/************************************************************************
* This function checks the format of an email address. There are five levels of
* checking:
*
* 1 - Basic format checking. Ensures that:
* There is an @ sign with something on the left and something on the right
* To the right of the @ sign, there's at least one dot, with something to the left and right.
* To the right of the last dot is either 2 or 3 letters, or the special case &quot;arpa&quot;
* 2 - The above, plus the letters to the right of the last dot are:
* com, net, org, edu, mil, gov, int, arpa or one of the two-letter country codes
* 3 - The above, plus attempts to check if there is an MX (Mail eXchange) record for the
* domain name.
* 4 - The above, plus attempt to connect to the mail server
* 5 - The above, plus check to see if there is a response from the mail server. The third
* argument to this function is optional, and sets the number of times to loop while
* waiting for a response from the mail server. The default is 15000. The actual waiting
* time, of course, depends on such things as the speed of your server.
*
* Level 1 is bulletproof: if the address fails this level, it's bad. Level 2 is still
* pretty solid, but less certain: there could be valid TLDs overlooked when writing
* this function, or new ones could be added. Level 3 is even less certain: there are
* a number of things that could prevent finding an MX record for a valid address
* at any given time. 4 and 5 are even less certain still. Ultimately, the only absolutely
* positive way to test an email address is to send something to it.
*
* The function returns 0 for a valid address, or the level at which it failed, for an
* invalid address.
*
************************************************************************/

function MailVal($Addr, $Level, $Timeout = 15000) {

// Valid Top-Level Domains
$gTLDs = &quot;com:net:eek:rg:edu:gov:mil:int:arpa:&quot;;
$CCs = &quot;ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:&quot;.
&quot;bg:bh:bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:&quot;.
&quot;ck:cl:cm:cn:co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:&quot;.
&quot;eh:er:es:et:fi:fj:fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:&quot;.
&quot;gp:gq:gr:gs:gt:gu:gw:gy:hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:&quot;.
&quot;is:it:jm:jo:jp:ke:kg:kh:ki:km:kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:&quot;.
&quot;lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:ml:mm:mn:mo:mp:mq:mr:ms:mt:&quot;.
&quot;mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:np:nr:nt:nu:nz:eek:m:pa:&quot;.
&quot;pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:rw:sa:sb:sc:sd:&quot;.
&quot;se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:tf:tg:th:&quot;.
&quot;tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:ve:&quot;.
&quot;vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:&quot;;

// The countries can have their own 'TLDs', e.g. mydomain.com.au
$cTLDs = &quot;com:net:eek:rg:edu:gov:mil:co:ne:eek:r:ed:go:mi:&quot;;

$fail = 0;

// Shift the address to lowercase to simplify checking
$Addr = strtolower($Addr);

// Split the Address into user and domain parts
$UD = explode(&quot;@&quot;, $Addr);
if (sizeof($UD) != 2 || !$UD[0]) $fail = 1;

// Split the domain part into its Levels
$Levels = explode(&quot;.&quot;, $UD[1]); $sLevels = sizeof($Levels);
if ($sLevels < 2) $fail = 1;

// Get the TLD, strip off trailing ] } ) > and check the length
$tld = $Levels[$sLevels-1];
$tld = ereg_replace(&quot;[>)}]$|]$&quot;, &quot;&quot;, $tld);
if (strlen($tld) < 2 || strlen($tld) > 3 &amp;&amp; $tld != &quot;arpa&quot;) $fail = 1;

$Level--;

// If the string after the last dot isn't in the generic TLDs or country codes, it's invalid.
if ($Level &amp;&amp; !$fail) {
$Level--;
if (!ereg($tld.&quot;:&quot;, $gTLDs) &amp;&amp; !ereg($tld.&quot;:&quot;, $CCs)) $fail = 2;
}

// If it's a country code, check for a country TLD; add on the domain name.
if ($Level &amp;&amp; !$fail) {
$cd = $sLevels - 2; $domain = $Levels[$cd].&quot;.&quot;.$tld;
if (ereg($Levels[$cd].&quot;:&quot;, $cTLDs)) { $cd--; $domain = $Levels[$cd].&quot;.&quot;.$domain; }
}

// See if there's an MX record for the domain
if ($Level &amp;&amp; !$fail) {
$Level--;
if (!getmxrr($domain, $mxhosts, $weight)) $fail = 3;
}

// Attempt to connect to port 25 on an MX host
if ($Level &amp;&amp; !$fail) {
$Level--;
while (!$sh &amp;&amp; list($nul, $mxhost) = each($mxhosts))
$sh = fsockopen($mxhost, 25);
if (!$sh) $fail = 4;
}

// See if anyone answers
if ($Level &amp;&amp; !$fail) {
$Level--;
set_socket_blocking($sh, false);
$out = &quot;&quot;; $t = 0;
while ($t++ < $Timeout &amp;&amp; !$out)
$out = fgets($sh, 256);
if (!ereg(&quot;^220&quot;, $out)) $fail = 5;
}

if ($sh) fclose($sh);

return $fail;
} //MailVal
?>

[/tt]

If that is not it, could you perhaps be a little more specific? Thanks.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
The code that vikter has supplied should be sufficient.

I have seen various server side scripts and code snippets from time to time, that can check the domain in real time to see if the user is valid, but this is near pointless given the wide variety of email servers in existance.

Additionally, many disable or do not explicitly support VRFY or EXPN commands (SMTP) for security or other reasons.

Gmanske.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top