如何在 PHP 邮件正文中包含包含 HTML 和 PHP 代码的 foreach 循环?
How to include foreach loop containing both HTML & PHP code in PHPmailer body?
我刚刚在 PHP/SQL 中构建了一个购物车,当客户下订单时,我想向他发送一封包含他的订单信息的电子邮件。我遇到的问题是如何将产品列表作为 table 包含在电子邮件中。我尝试使用包含 HTML 和 PHP 的 foreach
循环,但它导致了 HTTP 错误 500 当我尝试 'test drive' 浏览器中的模板时。
我的代码如下所示:
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . echo $item["name"]; . "</a></td>
<td><div>" . echo $item["qty"]; . "</div></td>
<td><span>" . echo "€".$item["subtotal"]; . "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
我知道 $item["#"]
变量工作正常,因为当我尝试使用 foreach
循环独立地 echo
它们时,它起作用了。当我尝试以某种方式在 $body
变量内的 HTML 内连接 PHP(即 echo $item["name"]
)时,问题就来了。
N.B。这段代码是我用来测试它是否适用于我的浏览器的代码。最终 $body
变量将是 $mail->MsgHTML($body)
.
的内容
感谢您的帮助! :)
PS:这是我第一次post在这个网站上,如果我可以改进我的问题,请告诉我!
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>" . "€".$item["subtotal"] . "</span></td></tr>";
}
从 foreach 中删除 echo
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=''>" .$item["name"]. "</a></td>
<td><div>" .$item["qty"]. "</div></td>
<td><span>€".$item["subtotal"]. "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
您可以设置变量,然后使用 foreach
添加到同一个变量
<?php <?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
";
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>€". $item["subtotal"] . "</span></td></tr>";
}
$body .= "</tbody>";
$body .= "</table>";
echo $body;
?>
改进您的代码
只使用HTML并且只做PHP中的foreach会更清楚。
<h1>We're glad to see you $firstName!</h1><br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>";
<?php foreach($cartItems as $item): ?>
<tr>
<td><a href="#"><?= $item["name"]; ?></a></td>
<td><div><?= $item["qty"]; ?></div></td>
<td><span>€ <?= $item["subtotal"]; ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
我刚刚在 PHP/SQL 中构建了一个购物车,当客户下订单时,我想向他发送一封包含他的订单信息的电子邮件。我遇到的问题是如何将产品列表作为 table 包含在电子邮件中。我尝试使用包含 HTML 和 PHP 的 foreach
循环,但它导致了 HTTP 错误 500 当我尝试 'test drive' 浏览器中的模板时。
我的代码如下所示:
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . echo $item["name"]; . "</a></td>
<td><div>" . echo $item["qty"]; . "</div></td>
<td><span>" . echo "€".$item["subtotal"]; . "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
我知道 $item["#"]
变量工作正常,因为当我尝试使用 foreach
循环独立地 echo
它们时,它起作用了。当我尝试以某种方式在 $body
变量内的 HTML 内连接 PHP(即 echo $item["name"]
)时,问题就来了。
N.B。这段代码是我用来测试它是否适用于我的浏览器的代码。最终 $body
变量将是 $mail->MsgHTML($body)
.
感谢您的帮助! :)
PS:这是我第一次post在这个网站上,如果我可以改进我的问题,请告诉我!
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>" . "€".$item["subtotal"] . "</span></td></tr>";
}
从 foreach 中删除 echo
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=''>" .$item["name"]. "</a></td>
<td><div>" .$item["qty"]. "</div></td>
<td><span>€".$item["subtotal"]. "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
您可以设置变量,然后使用 foreach
添加到同一个变量<?php <?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
";
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>€". $item["subtotal"] . "</span></td></tr>";
}
$body .= "</tbody>";
$body .= "</table>";
echo $body;
?>
改进您的代码
只使用HTML并且只做PHP中的foreach会更清楚。
<h1>We're glad to see you $firstName!</h1><br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>";
<?php foreach($cartItems as $item): ?>
<tr>
<td><a href="#"><?= $item["name"]; ?></a></td>
<td><div><?= $item["qty"]; ?></div></td>
<td><span>€ <?= $item["subtotal"]; ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>