Precisely check the Email address in PHP (including connectivity to Host)

zhaozj2021-02-16  54

/ *

* Email_validation.php

*

*

* /

Class email_validation_class

{

// var $ email_regular_expression = "^ ([A-Z0-9_] | // - | //.) @ (([A-Z0-9_] | // -) //.) [AZ] {2,4} $ ";

Var $ TIMEOUT = 0;

Var $ localhost = "";

Var $ localuser = ""

Function Getline ($ Connection)

{

For ($ line = ";;)

{

IF (Feof ($ connection))

Return (0);

$ line. = FGETS ($ Connection, 100);

$ length = Strlen ($ line);

IF ($ Length> = 2 && Substr ($ LINE, $ Length-2, 2) == "/ R / N")

Return (Substr ($ Line, 0, $ Length-2);

}

}

Function Putline ($ Connection, $ LINE)

{

Return (FPUTS ($ Connection, "$ LINE / R / N");

}

Function ValidateEmailAddress ($ email)

{

// return (EREGI ($ this-> email_regular_expression, $ email)! = 0);

Return (EREGI ("^ ([A-Z0-9_] | // - | //.) @ (([A-Z0-9_] | // -) //.) [AZ] {2 , 4} $ ", $ email)! = 0);

}

Function ValidateEmailhost ($ Email, $ Hosts = 0)

{

IF (! $ this-> ValidateEmailAddress ($ email))

Return (0);

$ USER = STRTOK ($ Email, "@");

$ domain = strtok ("");

IF ($ Domain, & $ Hosts, & $ Weights))

{

$ mxhosts = array ();

For ($ host = 0; $ host

$ MXHOSTS [$ Weights [$ Host] = $ HOSTS [$ Host];

Ksort ($ MXHOSTS);

For ($ MXHOSTS), $ Host = 0; $ Host

$ Hosts [$ host] = $ mxhosts [key ($ mxhosts)];

}

Else

{

$ hosts = array ();

IF (Strcmp (@gethostbyname, $ domain)! = 0)

$ Hosts [] = $ domain;

}

Return (count ($ hosts)! = 0);

}

Function VerifyResultLines ($ Connection, $ CODE)

{

While ($ line = $ this-> getline ($ connection)))))

{

IF (! STRCMP (STRTOK ($ LINE, "), $ code))

Return (1);

IF (STRCMP ($ Line, "-"), $ CODE)) RETURN (0);

}

Return (-1);

}

Function ValidateEmailbox ($ email)

{

IF ($ this-> ValidateEmailhost ($ EMAIL, & $ HOSTS))

Return (0);

IF (! Strcmp ($ localhost = $ this-> localhost, "") &&! strcmp ($ localhost = getenv ("Server_name"), "" "&&! strcmp ($ localhost = getenv (" Host ")," "" ))

$ localhost = "localhost";

IF (! Strcmp ($ localuser = $ this-> localuser, "") &&! strcmp ($ localuser = Getenv ("UserName", "") &&! strcmp ($ localuser = getenv ("user"), "" ))

$ localuser = "root";

For ($ host = 0; $ host

{

IF ($ connection = ($ HOSTS [$ Host], 25, & $ Errno, & $ Error, $ this-> timeout): fsockopen ($ Hosts [$ Host], 25) )))))))

{

IF ($ this-> VerifyResultLines ($ Connection, "220")> 0 && $ this-> Putline ($ Connection, "Helo $ localhost") && $ this-> VerifyResultLines ($ Connection, "250")> 0 && $ this-> Putline ($ Connection, "Mail from: <$ localuser @ $ localhost>") && $ this-> VerifyResultLines ($ Connection, "250")> 0 && $ this-> Putline ($ Connection, "RCPT To: <$ email> "&& ($ results = $ this-> verifyresultlines ($ Connection," 250 ")> = 0)

{

Fclose ($ Connection);

Return ($ Result);

}

Fclose ($ Connection);

}

}

Return (-1);

}

}

?>

============================================================================================================================================================================================================= =========================== ----------------------- -------------------------------------------------- ----

Application example:

============================================================================================================================================================================================================= ===========================

ECHO "

Check the correctness of the email address:"

$ newmail = "Test@test.com";

Require ("email_validation.php3);

$ validator = new email_validation_class;

$ Validator-> Timeout = 10;

IF ($ newemail && strcmp ($ newemail, ")))

{

IF ($ Result = $ VALIDATOR-> ValidateEmailbox ($ newemail) <0)

{

Echo "You cannot determine if your mailbox is correct. Your mailbox is too far away from here?";

Return;

}

Else

{

IF (! $ results)

{

Echo "The mail address your entered is incorrect! :);

Return;

}

Else

Echo "mailbox legal!"

}

}

转载请注明原文地址:https://www.9cbs.com/read-19186.html

New Post(0)