在html电子邮件模板php中创建一个foreach表

时间:2017-07-08 08:34:28

标签: php html css email

我有一个电子邮件模板,我试图在他们订购了一些商品后发送给用户。发送电子邮件我使用内联css与html作为电子邮件teplate保存为.html。问题是如何将用户购买的所有项目加载到每个表格中并在发送之前将其传递给电子邮件html模板文件

<tbody style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;">
    {{ item }}
</tbody>

这是我发送邮件的php

$mail = new PHPMailer;

$mail->From = "email@email.com";
$mail->FromName = "test";
$mail->addAddress($email); //Recipient name is optional
$mail->addReplyTo("email@email.com", "Reply");
$mail->isHTML(true);
$variables = array();
$variables['name'] = 'john';
$template = file_get_contents("eorder.html");
foreach($data as $row){
$eprodname = $this->get_name_from_id('product_name','product','product_id',$row['id']);
$variables['item'] ='<tr style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;page-break-inside: avoid;">
<td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">1</td>
<td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $eprodname .'</td>
 <td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $row['quantity'] .'</td>
 <td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $row['price'] .'</td>
</tr>';

}
foreach($variables as $key => $value)
{
  $template = str_replace('{{ '.$key.' }}', $value, $template);
}
$mail->Subject = "Order Details";
$mail->Body = $template;
$mail->Send();

我的问题:我对此代码的说法是,如果用户订购了多个产品,此代码只会加载表格中的最后一个产品

2 个答案:

答案 0 :(得分:0)

您正在用{{ item }}替换$value!这是一个“关键”,你想要你的阵列替换它! 你应该合并你的项目,然后用'item'替换一次!

答案 1 :(得分:0)

您正在覆盖$variables['item']密钥。您需要将其更改为数组

foreach($data as $row){
$eprodname = $this->get_name_from_id('product_name','product','product_id',$row['id']);
$variables['item'][] ='<tr style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;page-break-inside: avoid;">
<td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">1</td>
<td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $eprodname .'</td>
 <td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $row['quantity'] .'</td>
 <td style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0;font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;font-size: 14px;vertical-align: top;padding: 8px;line-height: 1.428571429;border-top: 1px solid #ddd;background-color: #fff!important;">'. $row['price'] .'</td>
</tr>';

}