我有这个脚本可以发送包含来自数据库的信息的电子邮件。用户可以在数据库中拥有其位置的1个以上项目。因此,当我清空与用户匹配的行时,发送的电子邮件数量等于他们拥有的行数。因此,如果他们在数据库中有8个项目,它将发送8封电子邮件。每个都添加一个项目。因此,第一封电子邮件有一个项目,第二封电子邮件有两个项目,依此类推。我试图找到一种简单的方法来使它在发送电子邮件之前获取所有信息,从而使客户仅收到一封电子邮件。逻辑方法是回显信息,但我无法使用php变量做到这一点。我没有在下面的代码中包含查询和数据库连接。任何帮助都会被爱。
while ($row = mysql_fetch_array($query)) {
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
$to = "example@example.com";
$from = "example@example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example@example.com";
mail($to, $subject, $message, $headers);
}
最佳答案
只需将发送功能移出周期即可:
while ($row = mysql_fetch_array($query)) {
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
}
if ($Items != '') {
$to = "example@example.com";
$from = "example@example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example@example.com";
mail($to, $subject, $message, $headers);
}
关于php - PHP Email Mysql提取数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16614630/