Sending dynamic table in phpmailer

北城余情 提交于 2019-12-23 05:14:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!