Forum Moderators: coopster

Message Too Old, No Replies

using str_replace in PHP mail loop

the emails show only the data from the first record

         

devitnow

1:48 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



I'm building a script that sends out emails from the database use php mail().

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!

dreamcatcher

4:16 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try unsetting the $msg variable after each loop.

unset($msg);

dc

devitnow

6:05 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



Hi DC,

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

dreamcatcher

8:40 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, try this. Before your loop make a copy of the message:

$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 here

unset($message);
$message = $message_copy;
}

Think that should do the trick.

dc

devitnow

1:28 am on Mar 5, 2005 (gmt 0)

10+ Year Member



It work!

THANK YOU so much. You really improved the quality of my life for this weekend :-) It's Friday night and I was settling in to another long night in front of the keyboard. But now my work is light and very soon, I'll be sipping on some Balvanie saying cheers to the 'ol DC.

you rock!

dreamcatcher

10:16 am on Mar 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I`m all for sipping. LOL!

Glad it worked ok.

dc