doubijiao2094
2014-06-26 17:24
浏览 18
已采纳

在phpmailer中发送动态表

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>"; }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwo1862 2014-06-26 17:36
    已采纳

    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>';
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanre1891 2014-06-26 17:34

    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
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题