user3519721
user3519721

Reputation: 21

How to send database data to html email

I am trying to send data from my database in the form of a HTML table when a user clicks the confirm button. I am able to send and receive the email but only the table headers are displayed in the email. I have tried adding in php to the html table to display the data but it just doesn't seem to be working and i can't figure out whether my code is just completely wrong or the code is jumbled :S

    <?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 = "[email protected]";

// Set the from address
$hdrs = array(
              'From'    => '[email protected]',
              '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 ([email protected])
$mail->send("[email protected]", $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']);*/


?>

Upvotes: 0

Views: 1528

Answers (1)

kuldeep.kamboj
kuldeep.kamboj

Reputation: 2606

Because you are using $sql instead of $query. So your line should be

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

instead of

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

Also no need for first mysql_fetch_array statement as While Loop taking care of all rows.

Upvotes: 1

Related Questions