rline101 Posted April 29, 2007 Share Posted April 29, 2007 Can anyone tell me why the following code returns the email addresses with the dots replaced by underscores? The keys are all email addresses. Instead of returning, say, someone@somewhere.com.au; another@somewhere.org.au it returns someone@somewhere_com_au; another@somewhere_org_au and therefore won't send the emails... foreach($_POST as $key => $value) { if(isset($key)) { $to=$to.$key."; "; } } $to=substr($to,0,-1); Quote Link to comment Share on other sites More sharing options...
trq Posted April 29, 2007 Share Posted April 29, 2007 Can anyone tell me why the following code returns the email addresses with the dots replaced by underscores? That piece of code isn't doing that. Any more code? Allot of that code is redundant anyway. foreach($_POST as $key => $value) { $to .= $key."; "; } $to = substr($to,0,-1); Quote Link to comment Share on other sites More sharing options...
rline101 Posted April 29, 2007 Author Share Posted April 29, 2007 The complete code follows (I can guarantee that the email addresses stored in the mysql db are correct): <?php include("opendb.php"); include("subjectshort.php"); $to=""; $emailsub=$_POST['emailsub']; $emailbod=$_POST['emailbod']; $getinfo="SELECT * from ".$subjectshort."_info"; $resultinfo = mysql_query($getinfo) or die(mysql_error()); $school=mysql_result($resultinfo,0,"school"); $assessor=mysql_result($resultinfo,0,"assessor"); $subject=mysql_result($resultinfo,0,"subject"); $assessoremail=mysql_result($resultinfo,0,"assessoremail"); foreach($_POST as $key => $value) { if(isset($key)) { $to=$to.$key."; "; } } $to=substr($to,0,-1); // SEND AN EMAIL $headers = "From: ".$assessoremail."\r\n"; if(isset($_POST['cc'])) {$headers .= "Cc: ".$assessoremail."\r\n";} mail($to,$emailsub,$emailbod,$headers); ?> Quote Link to comment Share on other sites More sharing options...
toplay Posted April 29, 2007 Share Posted April 29, 2007 I didn't look at your code but this is something everyone should know: Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it: <?php $varname.ext; /* invalid variable name */ ?> Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result. For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores. Quote Link to comment Share on other sites More sharing options...
rline101 Posted April 29, 2007 Author Share Posted April 29, 2007 Thanks for finding and posting this. I can now do something about it... Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted April 29, 2007 Share Posted April 29, 2007 I may be blind, but I didn't see any variable names with a dot in them. Quote Link to comment Share on other sites More sharing options...
trq Posted April 29, 2007 Share Posted April 29, 2007 Neither. Quote Link to comment Share on other sites More sharing options...
rline101 Posted April 29, 2007 Author Share Posted April 29, 2007 Nah trust me. toplay is absolutely spot on. The problem was that the input boxes (generated by php) had <input type=text name='someone@somewhere.com' value='yes'> and so the code was looking at the $key, which of course is the email address. I just changed the name= to name='_".$i."' and put the email address in the value instead. Then in the code you saw, I changed the test from the key to the value. Hope this explains it a little. It all works perfectly now. Thanks again toplay for picking this up. Quote Link to comment Share on other sites More sharing options...
toplay Posted April 29, 2007 Share Posted April 29, 2007 You got it...it's the name/index of the post/get variable. Well done. You're very welcome rline101. Glad to have helped. Happy coding Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.