I'm trying to build a system where a client can use shortcodes inside square tags when building an email template (also stored in the database) which will then be replaced with a line of PHP code. For example [order_date]
would be replaced with the value of date("l jS F Y", strtotime($OrderDate))
, with OrderDate
having been defined further up the code.
To do this I built a new database table with two fields - SubOriginal
and SubReplacement
. In the example above, [order_date]
would be in the SubOriginal
field, and date("l jS F Y", strtotime($OrderDate))
would be in the SubReplacement
field.
Then when it comes to emailing the user, I have this code:
// Set fields (StackOverflow example - data from a previous db query)
$OrderDate = $row['OrderDate'];
// Get customer order confirmation template
$sql = "SELECT DeliveryTemplateSubject, DeliveryTemplateContent FROM delivery_templates WHERE DeliveryTemplateTemplate='1'";
$set = $link->query($sql);
$row = $set->fetch_row();
$MessageBody = nl2br($row[1]);
// Make template substitutions
$sqlsub = "SELECT * FROM templates_subs";
$setsub = $link->query($sqlsub);
while($rowsub = $setsub->fetch_array(MYSQLI_ASSOC))
{
$replacement = $rowsub['SubReplacement'];
$MessageBody = str_replace($rowsub['SubOriginal'], $replacement, $MessageBody);
}
// Email customer
$subject = $row[0];
mail($CustomerEmail, $subject, $MessageBody);
However the replacements all come out as the original PHP code starting with the dollar sign.
Where am I going wrong?