Jump to content

why does it change . to _ in email addresses?


rline101

Recommended Posts

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

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

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);
?>

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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.