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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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