TinyMCE中的PHP foreach
我有一个从db加载的电子邮件(html)模板,其中包含TinyMCE中的PHP foreach,php,html,html-email,Php,Html,Html Email,我有一个从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:
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>
在发送电子邮件之前,我使用str_replace({name},$name,$content)替换令牌
,但我不知道如何执行$content的foreach
(或者可能是其他一些PHP操作)来填充$response的所有动态数据,然后发送电子邮件
我的尝试
我也尝试过在$response中使用token,但是,如果我需要多个$template设计和多个API响应(用于其他用途的电子邮件),那么这种方法就不好了。例如,在这个模板中,我有产品表,但在其他模板中,我需要内联
,内联样式
来列出一些功能等
请告诉我如何做到这一点
PS:我在前端使用TinyMCE设计电子邮件模板并存储在DB中。据我所知,您希望在电子邮件模板中添加更多信息/数据
解决方案#1 假设“$content”包含与您希望包含在模板中的消息相同的消息,您可以执行以下操作:
<?php
$content = " Thank You!!!"; // some data
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>
<td><?php echo $content; ?></td> // just add the variable.
</tr>
<?php } ?>
有关多个阵列上的foreach循环的更多详细信息,请参阅文章:
希望对我有所帮助我找到了解决办法
简而言之,我需要使用输出缓冲例如ob_start()
解释
步骤1:我将模板存储在.html
文件中
步骤2:API响应后,放入ob_start()代码>
步骤3:使用Include('path/to/file.html')
步骤4:获取缓冲区的输出并作为电子邮件发送
我的代码
//Consider HTML template in question is saved as template.html
//Response from API
$response = array(
0 => array(
'name' => 'abc',
'img' => 'linkToImg',
'price'=> '12',
'qty' => 10
),
1 => array(
'name' => 'xyz',
'img' => 'linkToImg',
'price'=> '15',
'qty' => 2
),
);
ob_start();
include('path/to/template.html');
$message = ob_get_clearn();
//now use $message in sending email
$this->emnail->message($message);
ob_start()
将打开缓冲区并保存HTML的输出,在其中执行PHP,然后ob_get_clean()
将输出返回到$message并清理缓冲区
效益
通过这种方法,我可以在sendmail()
中使用任何html模板。我只需要设计电子邮件模板,其余的事情将由输出缓冲区完成,你昨天问了这个问题,但他们你包括了一些更多的信息,你能再添加一次吗
foreach (array_combine($courses, $sections) as $course => $section)
//Consider HTML template in question is saved as template.html
//Response from API
$response = array(
0 => array(
'name' => 'abc',
'img' => 'linkToImg',
'price'=> '12',
'qty' => 10
),
1 => array(
'name' => 'xyz',
'img' => 'linkToImg',
'price'=> '15',
'qty' => 2
),
);
ob_start();
include('path/to/template.html');
$message = ob_get_clearn();
//now use $message in sending email
$this->emnail->message($message);