Reputation: 504
I have a question and try searching on Google and Stakoverflow and could't find any perfect solution. I am using phpmailer and want to let the user to customize it's body message. So I have given and option to edit the email body text by providing a texarea input box in back end where user will input the email text with some my own provided php variable.
For example body text is "Dear {$username}, thank you for signup.". Once I store this input into database and want to retrieve this in php:
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
echo $message;
OUTPUT DATA: "Dear {$username}, thank you for signup."
I want to get it like this "Dear ABC, thank you for signup.". How is it possible or is there any other option I can use it with php varialbe or defined type?
Upvotes: 0
Views: 2782
Reputation: 11
You can use PHP eval() function.
$msg = "$cust_dt->cus_name, Your order placed successfully. ID: $sms_oid Total: $sms_ord_tot";
echo $msg;//output1
eval("\$msg= \"$msg\";");
echo $msg;//output2
Output 1: $cust_dt->cus_name, Your order placed successfully. ID: $sms_oid Total: $sms_ord_tot
Output 2: John Doe, Your order placed successfully. ID: 454554545 Total: 1600
You can store a strings like $msg in a database and use eval() function to evaluate string into php code.
For More:
Upvotes: 1
Reputation: 63
Just wanted to say THANK YOU!!!!
This also works by putting ((name)), and using that inside str_replace - instead of just using "user_name"
Which is a little more user-friendly - and keeps from accidentally replacing things if someone were to type "user_name", for instance lol
Plus it'll make it look cool when having your "insert" legend at the top saying:
Choose one of the following: ((name)) = Clients Name
You saved me so much time!
Upvotes: 0
Reputation: 504
I have found a solution in PHP with str_replace function. This function is really very helpful. I have tried with modified input message "Dear user_name, thank you for signup.":
$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
$message_new = str_replace("user_name", $username, $message);
echo $message_new;
OUTPUT DATA: "Dear ABC, thank you for signup."
Upvotes: 1
Reputation: 1832
You're not getting an answer you're expecting because contents of $row["message"]
don't get evaluated again. And that's actually a good thing because users could use any variable name they want, which would create a security threat.
There is a quality solution, though. You should check out template engines for PHP, e.g. Smarty and Twig. They allow you to create templates with placeholders, for example something like:
Dear {{username}}, thank you for sign-up!
which you could store in a file or let your users define their own templates. Template engine will then read the template and, using the values you send it, replace those placeholders with actual values.
To see how this is done in practice, choose a template engine and check its documentation.
Upvotes: 1