我正在尝试在mysql表的电子邮件中填充html表。我遇到的问题是电子邮件中的表只显示最后的结果。我不确定如何在html电子邮件表中显示多个结果。
初始化电子邮件的变量
$sql = "SELECT * FROM tblUnitMaster WHERE JobNumber = '" . $JobNumber . "'";
$result=$conn->query($sql);
while ($row=$result->fetch_array())
{
$UnitID = $row[0];
$JobCode = $row[5];
$RTU = $row[17];
$ModelNumber = $row[2];
$SerialNumber = $row[3];
$Scope = $row[4];
}
电子邮件字符串
require 'Mail.php';
require 'Mail/mime.php';
$to = '$FUSER';
$headers['From'] = 'forest@fisenusa.net';
$headers['Subject'] = $JobNumber . " Job Handoff";
// create MIME object
$mime = new Mail_mime;
// add body parts
$html = '<html><body><p>Job: ' . $JobNumber . ' has been handed off from sales. Priority set as ' . $PriorityCode . ' .</p>
<p>Please review and assign an <a href="http://fisenusa.net/pm/pmAssignEngineer2.php?JobNumber=' . $JobNumber. '"> Engineer </a> as soon as possible.</p>
<table border=2 cellpadding=4 cellspacing=10>
<tr>
<th><b><font color=STEELBLUE size=3>Unit Number</font></b></th>
<th><b><font color=STEELBLUE size=3>Job Code</font></b></th>
<th><b><font color=STEELBLUE size=3>RTU</font></b></th>
<th><b><font color=STEELBLUE size=3>Model Number</font></b></th>
<th><b><font color=STEELBLUE size=3>Scope</font></b></th>
</tr>
<tr>
<td><font color=DIMGRAY size=2>' . $UnitID . '</font></td>
<td><font color=DIMGRAY size=2>' . $JobCode . '</font></td>
<td><font color=DIMGRAY size=2>' . $RTU . '</font></td>
<td><font color=DIMGRAY size=2>' . $ModelNumber . '</font></td>
<td><font color=DIMGRAY size=2>' . $Scope . '</font></td>
</tr>
</table>
<p>Click link to vist master record of Job Number: <a href="http://fisenusa.net/pm/pmUnitMaster2.php?JobNumber=' . $JobNumber. '">' . $JobNumber . '</a></p>
<p>Thank You,</p>
<p>FOREST</p>
<p>' . $quote . '</p>
</body></html>';
$mime->setHTMLBody($html);
// get MIME formatted message headers and body
$headers = $mime->headers($headers);
$body = $mime->get();
$message =& Mail::factory('mail');
$message->send($to, $headers, $body);
答案 0 :(得分:1)
第一步。您应该将数据聚合到数组。例如
$data = array();
while ($row=$result->fetch_array())
{
$data[] = array(
'UnitID' => $row[0],
'JobCode' => $row[5],
'RTU' => $row[17],
'ModelNumber' => $row[2],
'SerialNumber' => $row[3],
'Scope' => $row[4],
);
}
第二步,你在循环中连接字符串,例如:
foreach ($data as $row) {
$html .=
'<tr>
<td><font color=DIMGRAY size=2>' . $row['UnitID'] . '</font></td>
<td><font color=DIMGRAY size=2>' . $row['JobCode'] . '</font></td>
<td><font color=DIMGRAY size=2>' . $row['RTU'] . '</font></td>
<td><font color=DIMGRAY size=2>' . $row['ModelNumber'] . '</font></td>
<td><font color=DIMGRAY size=2>' . $row['Scope'] . '</font></td>
</tr>';
}