I'm making a mailing script and need some help. What I'm trying to do is, I'm sending one mail to all users and I need to use variables in this mail. For example: After form submit: $textarea = str_replace('[', '$row[', $_POST['textarea']); $body = "<html>\n"; $body .= "<body>\n"; $body .= "Dear " . $row["username"] . ", <br />"; $body .= "".$textarea."<br>"; $body .= "<br><br>"; $body .= "</body>\n"; $body .= "</html>\n"; $mail->Body = $body; $mail->AltBody = $text_body; $mail->AddAddress($row["email"], $row["username"]); if(!$mail->Send()) $msg = "There has been a mail error sending to " . $row["email"] . "<br>"; // Clear all addresses and attachments for next loop $mail->ClearAddresses(); $msg = "Mail send!<br />"; PHP: But this isn't working, I got the same text at my inbox: Hi [firstname], This is a testmail, your registration date is [regdate] Can someone help me with this?
It looks like there's some code missing. But make sure the names inside your $_POST variables match the names of the form items.
I think below will work but not sure. $textarea = str_replace('[', '[firstname]', $row["username"]); Please give us full code here.
why dont you use this: $textarea = str_replace('[username]', $row["username"], $textarea); $textarea = str_replace('[regdate]', $row["regdate"], $textarea); Code (markup): instead of : $textarea = str_replace('[', '$row[', $_POST['textarea']); Code (markup):
Thanks for reply's, the reason why i'm using Is because, it's not only [username] that can be used. Otherwise i have to make for every mysql row a str_replace. As you can see in my PHP code: $body .= "Dear " . $row["username"] . ", <br />"; PHP: Is working, but all the other str_recplaced variables from the FORM is not. $body .= "".$textarea."<br>"; PHP: I got only ".$row[varibale]." in my mail.
Not sure completely, but I don't like using the double-quote " when I'm trying to put together or concatenate a string like you are doing here. I use single quotes everywhere possible, except to begin and end the literal portions of your string (which in your case would be $body). Instead of using: I'd make it something like: That may or may not be your problem, but it keeps things simple and clean.
Using eval() will do the work, assuming that the $_POST['textarea'] does not contains double quotes " $textarea = str_replace('[', '$row[', $_POST['textarea']); $textarea = eval('echo "' . $textarea . '";'); PHP: that should work flawlessly, but if $_POST['textarea'] contains double quotes the eval() will throw an error, if this happens, you can try escaping double quotes by htmlentities or equivalent. Hope it helps.