Jump to content

Email Address Validation with PHP


sanfly

Recommended Posts

Hi, Ive been trying out this PHP email validation script i found on the [a href=\"http://www.zend.com/zend/spotlight/ev12apr.php\" target=\"_blank\"]Zend Website[/a]

The code
[code]function validateEmail($email){
    
    global $HTTP_HOST;
    
    // Check Format
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
        $result = "The email address is in an incorrect format";
        return $result;
    }
    // Find Mail Server Address
    list($username, $domain) = split("@", $email);
    if(getmxrr($domain, $MXHost)){
        $connectAddress = $MXHost[0];
    }
    else{
        $connectAddress = $domain;
    }
    // Check exists
    $connect = fsockopen($connectAddress, 25);
    if($connect){
        if(ereg("^220", $out = fgets($connect, 1024))){
            fputs($connect, "HELO $HTTP_HOST\r\n");
            $out = fgets($connect, 1024);
            fputs($connect, "MAIL FROM: <{$email}>\r\n");
            $from = fgets($connect, 1024);
            fputs ($connect, "RCPT TO: <{$email}>\r\n");
            $to = fgets($connect, 1024);
            fputs($connect, "QUIT\r\n");
            fclose($connect);
            if(!ereg ("^250", $from) || !ereg("^250", $to)){
                $result = "Mail server rejected address";
                return $result;
            }
        }
        else{
            $result = "No response from mail server";
            return $result;
        }
    }
    else{
        $result = "Cannot connect to mail server";
        return $result;
    }
    return true;
    
}[/code]

Some valid email addresses it rejects, ("Mail server rejected email address") so I changed the code to echo out the communications between me and the mail server.

The error I was getting was only for the MAIL FROM portion...

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]220-We do not authorize the use of this system to transport unsolicited, and/or bulk e-mail[/quote]

... but the RCPT TO portion was successful

What I assume this means is that they dont allow other servers to come in and send email address from that domain name, but they will recieve emails to that domain name. Can anyone verify this?

The script is only being used to confirm email address for a mailing list, therefore I will only be sending emails to that address, so if my theory is correct i should be able to ignore the MAIL FROM error, or even remove that part of the code alltogether?

Any help on this would be greatly appreciated

Cheers
Link to comment
Share on other sites

Well that seems a tad over-complicated to me...

Try this (it does about the same thing)

[code]

$email = $_POST['u_email'];

$parts = explode("@", $email);

$host = $parts[1];

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) && strlen($email) > 0) {
die('Email Address is not Valid');
}else{

if (!f open($host, 'r')){
die('Email Server is not Valid!');
}
}
[/code]

This checks to see if the addr is in the proper format and the domain host @xxxxx.yyy exists (yahoo.com, hotmail.com, etc)

:)
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]This checks to see if the addr is in the proper format and the domain host @xxxxx.yyy exists (yahoo.com, hotmail.com, etc)[/quote]
Sorry, but fopen is at least going to need a proticol eg [b]http://[/b]yahoo.com, otherwise it looks for a local file. And who's to say all mail servers have a webpage? Mine doesn't. So... what proticol to look for?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.