如何从数据库中提取信息后发送电子邮件

时间:2011-12-28 15:34:38

标签: php

这在单个页面上运行得很好。它给了我所需的所有信息。但我想发一封电子邮件。虽然我无法弄清楚我该怎么做

$db = new mysqli('localhost', 'root', '', 'panel');
$sql = "select * from detail";
$read = $db->query($sql);

<table style="border:0;width:600px;">
  <tr>
    <td width="50px">Who</td>
    <td width="150px">Time</td>
    <td width="300px">What</td>
  </tr>
<?php 
while($wr = mysqli_fetch_array($read)) {
 echo' <tr>
    <td>'.$wr['Who'].'</td>
    <td>'.$wr['Time'].'</td>
    <td>'.$wr['What'].'</td>
  </tr> ';
}


?>

1 个答案:

答案 0 :(得分:0)

您需要缓冲所有正在生成的HTML(这意味着不将其发送到浏览器,而是将其累积到缓冲区中),并将该缓冲区作为电子邮件发送给用户:

// Start buffering
ob_start();
// Generate HTML just like you're doing now
echo( "<html><body><table><tr><td></td></tr></table></body></html>" );
// Get the contents of the buffer
$html = ob_get_contents();
// Stop the buffering
ob_end_clean();

// Now use the content string you have to send an email just like the example at:
// http://il.php.net/manual/en/function.mail.php

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: to@domain.com' . "\r\n";
$headers .= 'From: from@domain.com' . "\r\n";

mail( "to@domain.com", "This is the subject", $html, $headers );