I have an email (html) template loaded from db, which contains foreach
loop (or may be some other PHP operation), to dynamically load/process content of API response.
$content = html_entity_decode($template); //$template loaded from db
$content's html
<p style="text-align:center">Hi {name},</p>
<p style="text-align:center">This is header</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Qty</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php foreach($response as $item){ ?>
<tr>
<td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
<td><?php echo $item['name']; ?></td>
<td><?php echo $item['qty']; ?></td>
<td><?php echo $item['price']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Response of API
$response = array(
0 => array(
'name' => 'abc',
'img' => 'linkToImg',
'price'=> '12',
'qty' => 10
),
1 => array(
'name' => 'xyz',
'img' => 'linkToImg',
'price'=> '15',
'qty' => 2
),
);
Before sending email, I replace token using str_replace('{name}', $name, $content);
, But I have no idea, how can i execute the foreach
(or may be some other PHP operation) of $content to populate all dynamic data of $response and then send email.
My Try
I tried using token for $response as well, but,that approach is not good if I need multiple designs of $template and multiple response of API (for other purpose email).For example, in this template, I have Table for products but in other template i need <ul> <li>
with inline style
for listing some features etc.
Please suggest me how to achive this.
PS: I am using TinyMCE in frontEnd to design email template and store in DB.