php str_replace used in a loop with mail function
I have an html email message assigned to a variable before it is used in a loop, then in the loop I try to use str_replace
to insert custom values into hooks. So in the below example I have an {email}
hook which I'm trying to replace with the recipient's email address. My problem is that when I output $message_final
to the browser the str_replace
function seems to have done the job. But when I send the email out, all the emails are still left with the {email}
hook, and it appears that str_replace
hasn't worked. Any ideas what i'm missing here? Thanks.
$message = "HTML email message here";
while ($r = mysql_fetch_开发者_运维技巧array($emails_list)) {
$email = $r["email"];
$message_final = "";
if(!empty($email)) {
//Replace hook with something new like an email address
$message_final = str_replace('{email}', $email, $message);
mail($email, $subject, $message_final, $headers);
}
}
You've got no {email}
string in your $message
that could be replaced
Your code is valid and works flawlessly. The problem lies somewhere else.
There are few things you might want to check:
str_replace
is case sensitive, are you sure it's {email} and not {EMAIL}?does $message contains valid message? try printing it out the browser, open it's source code so you can see additional html tags parsed by browser and check whether it's correct
By the way - you should consider using mysql_fetch_assoc
instead of mysql_fetch_array
. The method you're using by default combines arrays created by mysql_fetch_row
and mysql_fetch_assoc
into one, so it takes 2 times more space
Ahhhhh! Palm to face!! It was something completely different causing the issue. In my mail header I had the message attached there, and I was calling str_replace in the wrong bit of the code. Thanks for all your replies guys :)
精彩评论