1

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); 
    }
}
2
  • Can you post a portion of the actual $message that contains the hooks? Commented Sep 5, 2011 at 14:55
  • Hmm.. the code you posted here looks all right.. Can you post your 'real' code (including the working var_dump() you used to see the message in your browser)? Commented Sep 5, 2011 at 14:57

3 Answers 3

1

You've got no {email} string in your $message that could be replaced

Sign up to request clarification or add additional context in comments.

5 Comments

the HTML string is too long to post here, but it's in the variable $message
I'm pretty sure the OP's actual $message has them.
I have just tested a general string replace using an e-mail address and your hook, and it works fine.
@EvanMulawski: yes, me either
I think the OP will find the problem via debugging.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.