【发布时间】:2021-08-21 09:47:12
【问题描述】:
我有一个从 db 加载的电子邮件 (html) 模板,其中包含 foreach 循环(或者可能是其他一些 PHP 操作),用于动态加载/处理 API 响应的内容。
$content = html_entity_decode($template); //$template loaded from db
$content 的 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>
API 的响应
$response = array(
0 => array(
'name' => 'abc',
'img' => 'linkToImg',
'price'=> '12',
'qty' => 10
),
1 => array(
'name' => 'xyz',
'img' => 'linkToImg',
'price'=> '15',
'qty' => 2
),
);
在发送电子邮件之前,我使用 str_replace('{name}', $name, $content); 替换令牌,但我不知道如何执行 $content 的 foreach(或者可能是其他一些 PHP 操作)来填充 $response 的所有动态数据和然后发送电子邮件。
我的尝试
我也尝试将令牌用于 $response,但是,如果我需要 $template 的多种设计和 API 的多种响应(用于其他目的的电子邮件),这种方法并不好。例如,在这个模板中,我有 Table对于产品,但在其他模板中,我需要 <ul> <li> 和内联 style 来列出一些功能等。
请建议我如何实现这一目标。
PS:我在前端使用 TinyMCE 来设计电子邮件模板并存储在 DB 中。
【问题讨论】:
-
你昨天问过这个问题,但他们提供了更多信息,你能再补充一下吗
标签: php html html-email