Jump to content

PHP: Validate Email Address

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
26 replies to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
This function actually validates an email address against an MX record. I found this somewhere and modified it slightly.

Name: validateemail.php
Description: Validates an email against MX records

Usage:
include validateemail.php
Pass an email to validateEmail();
Example:

 if (validateEmail("somename@domain.com")) {
//code block
}

Attached Files



#2
skilletsteve

skilletsteve

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
what exactly are MX records?
do you need to have your own set of MX records for it to check against?

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
MX records are records added in the DNS file. MX records are for email inside of the DNS system. The script I provided above will actually check to see if the mail/domain exists.

#4
Guest_moguai_*

Guest_moguai_*
  • Guests
This looks great, thanks :)

#5
Guest_lambert1969_*

Guest_lambert1969_*
  • Guests
Looks great!, thanks!

#6
Guest_yosutanto_*

Guest_yosutanto_*
  • Guests
Thanks a lot! will try it now.

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Im working on a whole validation class, here is my Email Validation

     /**

     *@param $Email is the email address you wish to validate

     *@return $result[0] will either be true or false. True

     *    meaning the email is valid - False meaning the email

     *    meaning the email is invalid.

     *@return $result[1] is a brief message explaining the result.

     */

    function VerifyEmail ($Email)

    {


        $result = array();


        #Use a regular expression to make sure the $Email string is in proper format

        #<username>@<domain>.<suffix> || <username>@<subnet>.<domain>.<suffix> 

        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {

            $result[0] = false;

            $result[1] = "$email is not properly formatted.";

            return $result;

        } 



        list ($Username, $Domain) = split ("@", $Email);


        #Verify the MX record.

        if (getmxrr($Domain, $MXHost)) {

            $result[0] = true;

            $result[1] = "$email appears to be valid.";

            return $result;

        } else {

            #Establish a connection to the server.

            if (@fsockopen($Domain, 25, $errno, $errstr, 30)) {

                $result[0] = true;

                $result[1] = "$email appears to be valid.";

                return $result;

            } else  {

                $result[0] = false;

                $result[1] = "Cannot connect to the server.";

                return $result;

            }

        }

    } //End VerifyEmail

Usage:

    $result =  VerifyEmail ($Email);

    if($result[0]) {

        //email is valid

    }



#8
sh44n

sh44n

    Newbie

  • Members
  • Pip
  • 1 posts
Nice one

#9
tmarsha4

tmarsha4

    Newbie

  • Members
  • Pip
  • 1 posts
very cool, ill be using this for sure. thanks!

#10
matthewk

matthewk

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks, wanting to check it out.

#11
codelifter

codelifter

    Newbie

  • Members
  • Pip
  • 1 posts
Will check it out.

#12
jenie_penny

jenie_penny

    Newbie

  • Members
  • Pip
  • 1 posts
that is a great script, thank you for posting it!