我试图将MySql数据库中的多行回显到电子邮件的$ message部分。附加的代码只返回一个结果。
我想在电子邮件中发送1封电子邮件,我想从SQL查询中输出10行。 实际上,我实际上会收到十次单独的电子邮件,而不是一封包含十行的电子邮件。
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$to = 'email@email.com';
$subject = 'Daily Stoic Email';
$message = $row["Maxim"]. "</br>";
$headers = 'From: email@email.com' . "\r\n" .
'Reply-To: email@email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
答案 0 :(得分:0)
将邮件调用移出循环
// don't select all (*) fields if you only need one
$sql = "SELECT Maxim FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
$message = '';
while($row = $result->fetch_assoc()) {
$message .= $row["Maxim"]. "</br>";
}
// prepare email headers
$to = 'email@email.com';
$subject = 'Daily Stoic Email';
$headers = 'From: email@email.com' . "\r\n" .
'Reply-To: email@email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// then send
mail($to, $subject, $message, $headers);
答案 1 :(得分:0)
我没有测试过代码,但试试看:
$to = 'email@email.com';
$subject = 'Daily Stoic Email';
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$message = $row["Maxim"]. "</br>";
}
$headers = 'From: email@email.com' . "\r\n" .
'Reply-To: email@email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
我已将to,subject,header和mail功能放在循环之外。
干杯