Forum Moderators: coopster
I needed to use str_replace() to insert custom information into each message, such as first name. I have a problem where as the script loops through the records the emails contain the custom information from only the first record. Yet, I do know it's looping through the recordset correctly, I echoed the variables just about the mail portion of the code for testing and on the webpage, everything loops and prints out perfectly - but the emails will only show the custom info from the first record.
So on the webpage it's correct, but in the emails it's wrong.
Can someone please take a look at this code and tell mw what I'm doing wrong?
echo '<br />First Name: '. $first_name;
echo '<br />Location: '. $nearest_location;
echo '<br />Location City: '. $nearest_city;
echo '<br />Location State: '. $nearest_state;
echo '<br><br>';
// Begin 'Send Email'
$message = str_replace("%first_name%", $first_name, $message);
$message = str_replace("%location%", $nearest_location, $message);
$message = str_replace("%city%", $nearest_city, $message);
$message = str_replace("%state%", $nearest_state, $message);
$msg = "". $message."\n";
mail($recipient, $subject, $msg,
"From: me@myemail.com\r\n" .
"Reply-To: me@myemail.com\r\n" .
"X-Mailer: PHP/" . phpversion());
as always, thank you!
I tried that but with no luck, it stills shows in the email the values from the first recordset.
I forgot too to say that the variables are getting created from mysql_fetch_array.Just above the that snippet. Does that matter? I wonder if this is some weird array index/pointer problem.. I don't know... I'm feeling totally lost and frustrated.
If on the webpage as a test, I echo $msg to see what the email message will look like, I see the error there too.
thanks again
$message_copy = $message;
After each loop, unset the $message variable, then assign a new message variable using $message_copy.
So:
$message_copy = $message;
while ($row = mysql_fetch_array($result))
{
//code hereunset($message);
$message = $message_copy;
}
Think that should do the trick.
dc