问题
I am having an issue with sending an email through php mailer. I am trying to send a dynamically populated table but I am only receiving the first row in the emails. If anyone has any knowledge on how I can send the full table in an email please share! Thank you!
Here is my code where I am having issues.
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
While($row=mysql_fetch_assoc($result)){
$end_dt = $row['end_dt'];
$dpd = floor((abs(strtotime(date("Y-m-d")) - strtotime($end_dt))/(60*60*24)));
$mail->Body =
"<table><tr><th>Name</th><th>IDCourseTax</th><th>TDCourse</th><th>End Date</th><th>Tax Rate</th><th>Days past due</th></tr>
<tr><td>".$row["course"]."</td><td>".$row["IDCourseTax"]."</td><td>".$row["IDCourse"]." </td><td>".$row["end_dt"]."</td><td>".$row["tax_rate"]."</td><td>".$dpd."</td></tr> </table>"; }
回答1:
You are writing the table in the loop so on each iteration it is getting overwritten.
You will want to create your table in a variable then set the body to that variable:
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$body = '<table><tr><th>Name</th><th>IDCourseTax</th><th>TDCourse</th><th>End Date</th><th>Tax Rate</th><th>Days past due</th></tr>';
while($row=mysql_fetch_assoc($result)) {
$end_dt = $row['end_dt'];
$dpd = floor((abs(strtotime(date("Y-m-d")) - strtotime($end_dt))/(60*60*24)));
$body .= "<tr><td>".$row['course']."</td><td>".$row['IDCourseTax']."</td><td>".$row['IDCourse']." </td><td>".$row['end_dt']."</td><td>".$row['tax_rate']."</td><td>".$dpd."</td></tr>";
}
$mail->Body = $body.'</table>';
回答2:
The problem is that you're not appending table rows but every time rewrite the next one in the email body, to solve your problem you simply need to concatenate at every interation the single row.
$mail->isHTML(true);// Set email format to HTML
$mail->Subject = $_POST['subject'];
//starting table
$mail->Body = "<table><tr><th>Name</th><th>IDCourseTax</th><th>TDCourse</th><th>End Date</th><th>Tax Rate</th><th>Days past due</th></tr>";
while($row=mysql_fetch_assoc($result)){
$end_dt = $row['end_dt'];
$dpd = floor((abs(strtotime(date("Y-m-d")) - strtotime($end_dt))/(60*60*24)));
$mail->Body .= "
<tr>
<td>".$row["course"]."</td><td>".$row["IDCourseTax"]."</td><td>".$row["IDCourse"]." </td><td>".$row["end_dt"]."</td><td>".$row["tax_rate"]."</td><td>".$dpd."</td>
</tr> ";
}
$mail->Body .= "</table>"; //close table
来源:https://stackoverflow.com/questions/24436694/sending-dynamic-table-in-phpmailer