...

View Full Version : Email Validation always False



ArcticFox
01-30-2007, 12:24 AM
Can someone take a look at this and give me a heads-up as to why this doesn't work?



<?php


// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "address@domain.com";
$Subject = "BSZ Contact Form";
$Name = Trim(stripslashes($_POST['Name']));
$Street1 = Trim(stripslashes($_POST['Street1']));
$Street2 = Trim(stripslashes($_POST['Street2']));
$City = Trim(stripslashes($_POST['City']));
$State = Trim(stripslashes($_POST['State']));
$PostCode = Trim(stripslashes($_POST['PostCode']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Comments = Trim(stripslashes($_POST['Comments']));


// TESTING validation


function checkEmail($EmailFrom) {
// checks proper syntax
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $EmailFrom)) {
// gets domain name
list($username,$domain)=split('@',$EmailFrom);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
return false;
}
return true;
}
return false;
}






// end TESTING validation


// prepare email body text
$Body = "";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";





$EmailFrom = trim($_POST['email']);
if(!checkEmail($EmailFrom)) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=oops.php\">";
}
else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.php\">";
}
?>



No matter what I put in my email form (http://www.basestationzero.com/contact.php), this doesn't like it. Is it a windows/linux thing?

martialtiger
01-30-2007, 12:32 AM
Do you have the right $_POST key? I noticed the top $EmailFrom came from $_POST['EmailFrom'] while the bottom $EmailFrom came from $_POST['email'].

Good luck!

ArcticFox
01-30-2007, 12:38 AM
LOL. I'm not sure what that means - it was two scripts that I tried to combine then remove parts I didn't need. :)

Everything that was $email was changed to $EmailFrom. Are you suggesting I change email to EmailFrom as well, 'cause that didn't work.

The form

<div style="text-align:justify; font-size:14px;">
<script language="Javascript">
<!--//
when = new Date

if (when.getHours() < 9) {
document.write("Good morning! What can I do for you?")
}
else if (when.getHours() < 17) {
document.write("Good afternoon! What can I do for you today?")
}
else {
document.write("Good evening! What can I do for you tonight?")
}
//-->
</script>
<form method="POST" action="submitted.php" class="button">


<p>Comments:<span style="color:red">*</span> <br>
<textarea name="Comments" cols=38 rows=5></TEXTAREA>


<p>Your Email Address:<span style="color:red">*</span><br>
<input type="text" name="EmailFrom">
<p>Your Name:<br>
<input type="text" name="Name">
<p>Phone Number:<br>
<input type="text" name="Tel">
<br><br>

<br><small style="color:red">Fields marked (*) are required</small>
<p><input type="submit" name="submit" value=" Send It ! ">
</form>
<p>
</div>

martialtiger
01-30-2007, 12:44 AM
Yes. Change this line


$EmailFrom = trim($_POST['email']);

to this


$EmailFrom = trim($_POST['EmailFrom']);

ArcticFox
01-30-2007, 01:05 AM
Nope. Still doing the same thing. :(

martialtiger
01-30-2007, 01:12 AM
Are you getting any errors? Do you have URL we can take a look at?

ArcticFox
01-30-2007, 01:19 AM
No errors, it just goes directly to the oops.php page.

All the pages are in .php - changed them to .txt so you can take a look:

http://www.basestationzero.com/contact.txt
http://www.basestationzero.com/submitted.txt
http://www.basestationzero.com/oops.txt
http://www.basestationzero.com/thanks.txt

CFMaBiSmAd
01-30-2007, 01:28 AM
Your checkEmail function contains multiple tests that all return false upon failure. There is no way to know which one failed. The proper way would be to return unique values for each test so that you can take appropriate action in the program and print out a meaningful error message.

martialtiger
01-30-2007, 01:44 AM
Try using this this function in place of your checkEmail function (found here (http://www.ilovejackdaniels.com/php/email-address-validation)):


function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

ArcticFox
01-30-2007, 03:26 AM
I looked at that, but didn't think I wanted to use it. After looking through it again, LOL, I've decided to combine that and the one I can't get to work... which I almost got to work now. :)


I get this error only sometimes, seems like at random.


Warning: fsockopen() [function.fsockopen]: unable to connect to yahoo.com:25 in /sites/basestationzero.com/htdocs/submitted.php on line 68



Located:


function checkEmail($EmailFrom) {



// checks proper syntax
if (check_email_address($EmailFrom)) {
// gets domain name
list($username,$domain)=split('@',$EmailFrom);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
echo "28<br>";ob_flush();flush();return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
echo "32<br>";ob_flush();flush();return false;
}
echo "34<br>";ob_flush();flush();return true;
}
echo "36<br>";ob_flush();flush();return false;
}




I'm guessing that sometimes port 25 is not accessible?

CFMaBiSmAd
01-30-2007, 03:59 AM
The receiving mail server(s) would be indicated by the MX records for a domain. There is no guarantee that any domain will have a mail server listening on port 25 at the same IP address as the domain.

For example, yahoo has the following MX records (receiving mail servers) -

1 g.mx.mail.yahoo.com. [TTL=7200] IP=206.190.53.191 [TTL=1800] [US]
1 a.mx.mail.yahoo.com. [TTL=7200] IP=209.191.118.103 [TTL=1800] [US]
1 b.mx.mail.yahoo.com. [TTL=7200] IP=66.196.97.250 [TTL=1800] [US]
1 c.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.3 [TTL=1800] [US]
1 d.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.2 [TTL=1800] [US]
1 e.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.1 [TTL=1800] [US]
1 f.mx.mail.yahoo.com. [TTL=7200] IP=68.142.202.247 [TTL=1800] [US]

For your code to work properly, you would need to attempt an fsockopen at each of these address in turn, until you get a successful connection to at least one.

ArcticFox
01-30-2007, 04:03 AM
Is that possible to do? Seems like a lot of work. Maybe I should just stick with what I've put up so far.

Is there a way for me to keep the php from throwing an error when it hits that point - like instead of an error, it could send a message along with the original posted email telling me it couldn't connect...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum