0

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?

4 Answers 4

1

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.

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

2 Comments

Thank you hgazibara for you suggestion and good information. But i don't want to use any template engine, I am looking for good solution in core PHP.
Then I'm sorry because I don't know of any other way besides using some version of str_replace. Eval could also help you, but I wouldn't recommend it.
1

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."

Comments

1

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:

  1. https://www.w3schools.com/php/func_misc_eval.asp
  2. https://www.php.net/manual/en/function.eval.php

Comments

0

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!

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.