发送email-template.php文件时,以下代码可以正常工作。我现在想在表的电子邮件模板中包括SQL查询的结果,基本上是引号列表。我觉得str_replace
是要走的路,但是作为一个完整的新手,我不确定该怎么做。
<?php
include('connect.php');
include('functions.php');
$sql = "SELECT Quote FROM daily_quote Order By rand() Limit 0,10";
$result = $conn->query($sql);
$to = 'example@example.com';
$subject = "Test";
$htmlContent = file_get_contents("email-template.php");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: info@example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
include('footer.php');
最佳答案
在email-template.php中添加:
{place_holder}
以这种方式编辑脚本:
$sql = "SELECT Quote FROM daily_quote Order By rand() Limit 0,10";
$result = $conn->query($sql);
$template = "<table>";
while($row = $result->fetch_assoc()) {
$template .= "<tr><td>".$row['Quote']."</td></tr>";
$template .= "</table>";
然后编辑
if(mail($to,$subject,str_replace("{place_holder}",$template,$htmlContent),$headers)):