我有一个关于通过 php 发送电子邮件的一般问题。如何将 php 数组放在邮件的消息部分?
下面你可以看到我制作的一个简单的数组,并试图把它放在电子邮件中,但这完全是错误的,因为我没有在互联网上找到它如何做到这一点(我是初学者。我刚开始使用 php 编码)。
数组:
<?php
$Array[1] = array('qualitypoint', 'technologies', 'India','Hey');
$Array[2] = array('quality', 'tech', 'Ind','He');
$Array[3] = array('q', 't', 'I','H');
?>
邮件发送者:
<?php
include "index.php";
while($row = $LevAdres->fetch_assoc()) {
$email=null;
$email= $row['Email'];
}
$to = example@hotmail.com;
$subject = "Bestelling";
$headers = "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$headers .= "From: Restaurant@test.be" . "'r'n";
$headers .= "Reply-To: people@info.be". "'r'n";
$headers .= "CC: Restaurant@test.be'r'n";
$message = "<?php
echo '<table>';
echo '<table>';
for($i=0;$i<=3;$i++)
{
echo "<tr>";
for($j=1;$j<=3;$j++)
{
echo "<td>{$Array[$j][$i]}<td>";
}
echo "</tr>";
}
echo '</table>';
?>";
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
?>
这就是表格的外观
你去...完整代码
$Array[1] = array('qualitypoint', 'technologies', 'India', 'Hey');
$Array[2] = array('quality', 'tech', 'Ind', 'He');
$Array[3] = array('q', 't', 'I', 'H');
$to = "example@hotmail.com";
$subject = "Bestelling";
$headers = "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$headers .= "From: Restaurant@test.be" . "'r'n";
$headers .= "Reply-To: your@email.com" . "'r'n";
$headers .= "CC: Restaurant@test.be'r'n";
$message = "";
$message .= '<table>';
for ($i = 0; $i <= 3; $i++) {
$message .= "<tr>";
for ($j = 1; $j <= 3; $j++) {
$message .= "<td>{$Array[$j][$i]}</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
将$message变量更改为此变量。
$message = "<table>";
for($i=0;$i<=3;$i++)
{
$message .= "<tr>";
for($j=1;$j<=3;$j++)
{
$message .= "<td>{$Array[$j][$i]}<td>";
}
$message .= "</tr>";
}
$message .= '</table>';
我可以看到您有一个双引号问题。试着这样说: echo '<td>'."{$Array[$j][$i]}".'<td>'
;
如果你想把它放在消息中,那么不要回显它,而是把它写在消息正文中
将回显代码更改为
$array = [
['qualitypoint', 'technologies', 'q'],
['technologies', 'tech', 't'],
['India', 'ind', 'i'],
];
$message = "";
$message .= '<table>';
foreach ($array as $row)
{
$message .= "<tr>";
foreach ($row as $cell)
{
$message .= "<td>$cell</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
但是,如果您想回显它,那么您可以捕获输出缓冲并使用它的内容,但我不建议这样做。使用 foreach 也使其更具可读性。还可以考虑用小驼峰大小写命名变量。