Jump to content


Photo

Email Address Validation with PHP


  • Please log in to reply
4 replies to this topic

#1 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 06 May 2006 - 09:48 PM

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
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;
    
}

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
If you're not part of the solution, you're part of the precipitate

#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 08 May 2006 - 08:52 PM

Bump
If you're not part of the solution, you're part of the precipitate

#3 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 09 May 2006 - 01:22 AM

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

Try this (it does about the same thing)


$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!');
}
}

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

:)

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 09 May 2006 - 01:31 AM

Thanks, will give it a go tonight
If you're not part of the solution, you're part of the precipitate

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 09 May 2006 - 01:33 AM

[!--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 http://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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users