如何将数据库数据发送到HTML电子邮件

时间:2014-05-07 09:07:30

标签: php html mysql email

当用户单击确认按钮时,我尝试以HTML表格的形式从我的数据库发送数据。我能够发送和接收电子邮件,但只有表格标题显示在电子邮件中。我已经尝试在html表中添加php来显示数据,但它似乎没有工作,我无法弄清楚我的代码是完全错误还是代码混乱:S < / p>

    <?php
include("../config/cn.php");
include("/usr/share/php/Mail.php");
include("/usr/share/php/Mail/mime.php");

// Get the purchase order details
$query = mysql_query("SELECT Jobs.* FROM Jobs WHERE order_ref = '".$_GET['order_ref']."'");
$row = mysql_fetch_array ($query);



// Set the email content
$text = 'Text version of email';
$html = '<html><body>';
$html .= '<table border="1" cellspacing="0" cellpadding="0"><tr><th width="200">Order Ref.</th><th width="200">First Name</th><th width="200">Last Name</th><th width="200">Tracking No.</th></tr>';

while($row = mysql_fetch_array($sql)) {

    $html .= '<tr><td>'.$row['order_ref'].'</td>';
    $html .=    '<td>'. $row['first_name'].'</td>';
    $html .=    '<td>'.$row['last_name'].'</td>';
    $html .=    '<td>'.$row['tracking_number'].'</td>';
    $html .= '</tr>';

'</table></body></html>';
}

//$file = "/mnt/Jobs/Purchase_Orders/".date("Y", strtotime($row['created']))."/".date("F", strtotime($row['created']))."/PO".$row['id'].".pdf";
$crlf = "\n";



// Set customers email
$sendAddress = "mail@variouk.com";

// Set the from address
$hdrs = array(
              'From'    => 'mail@variouk.com',
              'Subject' => 'Spineless System'
              );

// Create the email
$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
//$mime->addAttachment($file, 'application/pdf');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

// Send the email
$mail =& Mail::factory('mail');
// Paper company address
$mail->send($sendAddress, $hdrs, $body);
// Production email address (production@variouk.com)
$mail->send("mail@variouk.com", $hdrs, $body);

header("location: joblist.php");

/*
// Update the sent status of the order
mysql_query("UPDATE purchase_orders SET sent = '".date("Y-m-d H:i:s")."' WHERE id = '".$_GET['edit']."'", $link);


header("Location: ../production/pdf_email.php?jobno=".$_GET['jobno']."&edit=".$_GET['edit']);*/


?>

1 个答案:

答案 0 :(得分:1)

因为您使用的是$sql而不是$query。所以你的行应该是

while($row = mysql_fetch_array($query)) {

而不是

while($row = mysql_fetch_array($sql)) {

同样不需要第一个mysql_fetch_array语句,因为While Loop会处理所有行。