I am trying to make a foreach loop inside a foreach loop.
I have a form in which the user enters some text and sends the package. On the server site, I scan the array with email addresses and send a text message.
Now I also want the user to be able to use variables in the text box, for example $ name. So my loop should first go through emails, and then str_replace the userinput $ name variable with the names in my array.
The loop works fine with the email part ($ tlf), but does not replace the $ name part.
Can someone say what I'm doing wrong?
$message = stripslashes(strip_tags($_POST['message2'])); $tlf=array("name1","name2"); $test=array("mname1", "mname2"); $subject = "Hello world"; $from = " me@gmail.com "; $headers = "From: $from"; foreach($tlf as $name){ $to = $name. "@gmail.com"; foreach($test as $navn){ $message = str_replace('$navn', $navn, $message);} mail($to,$subject,$message,$headers); }
Thank you very much.
EDIT: An email was sent at the exit. Say the user type in "hello $ name". I want him to iterate over the $ tlf array first, in this case, create 2 letters. It starts with $ to in the first loop. It works.
Now the next loop should recognize the user input "hello $ name" and execute the loop through the $ test array, replacing the user variable $ name.
The output will be sent 2 emails.
Let me know if I need to explain better, it's hard for me to explain, sorry.