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, [email protected]; [email protected] 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); Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/ 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); Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240877 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); ?> Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240884 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. Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240889 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... Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240891 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. Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240893 Share on other sites More sharing options...
trq Posted April 29, 2007 Share Posted April 29, 2007 Neither. Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240895 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='[email protected]' 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. Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240898 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 Link to comment https://forums.phpfreaks.com/topic/49154-why-does-it-change-to-_-in-email-addresses/#findComment-240903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.