在html类型的电子邮件中显示错误消息并显示查询结果

时间:2017-12-13 02:35:20

标签: php html email phpmailer

我制作了一个电子邮件功能,在$mail->Body我使用了html&我的$mail->Body中的PHP代码,但我没有得到我想要的结果,我认为我的html& PHP代码仍然是错误,但我没有收到错误消息显示所以我不知道我的HTML和PHP代码有什么问题。我可以为$mail->Body内容显示错误消息吗?我认为我的代码错误,因为当我使用简单的代码在数据库中显示数据时,我设法发送了一封电子邮件

这是我的代码:

$query = mysql_query("this is my query");

  while ($data = mysql_fetch_assoc($query)) {

--I show my query result in `$mail->Body`--


$mail = new PHPMailer();
 $mail->IsSMTP();
            $mail->Host = "172.16.x.xx"; //my company host
            $mail->PORT = "25";
            $mail->SMTPAuth = true;  
            $mail->Username = "myemail@acc.co.id"; //my company email  
            $mail->Password = "mypassword"; // SMTP password
            $mail->IsHTML(true); 

            $mail->From = "myemail@acc.co.id";
            $mail->FromName = "ITCareHelpdesk ACC";
            $mail->AddAddress("myemail@gmail.com");                            
            $mail->mailtype = "html";
            $mail->Subject = "Weekly monitoring tiket open ITcare"; 
    $mail->Body    ="
    <table border=1>
      <tr>
        <th>Ticket ID</th>
        <th>Kategori</th>
        <th>Sub kategori</th>                         
        <th>Deskripsi</th>                         
        <th>NPK</th>                         
        <th>Nama</th>                         
        <th>Posisi</th>                         
        <th>Branch</th>                         
        <th>Waktu mulai</th>                         
        <th>Group section</th>                         
        <th>Pic Handle</th>
        <th>Pic Group Section</th>                                                  
      </tr>


      <tr $color>
          <td>$data[TICKET_ID]</td>
          <td>$data[kategori]</td>
          <td>$data[sub_sub_sub_kategori]</td>
          <td>$data[description]</td>
          <td>$data[npk]</td>
          <td>$data[customer_name]</td>
          <td>$data[position]</td>
          <td>$data[branch]</td>
          <td>$data[start_IT]</td>
          <td>$data[group_section]</td>
          <td>$data[pic_handle]</td>
          <td>$data[pic_group_section]</td>

        </tr>

      </table>";
    }

我尝试发送它并收到电子邮件但我的查询结果只显示1行而另一行未显示。

2 个答案:

答案 0 :(得分:0)

评论中的一些内容是正确的,但您也在循环中创建一个新的邮件对象。这将导致发送一封电子邮件(最后一封)。这是你的结构:

while(...) {
    // create a new object
    $obj = new Object();
    ... do stuff to $obj
    ... repeat, but never use $obj
}

// Guessing, now is where you do stuff with $obj, 
// but this will be the last $obj created
$obj->send();

我看到两个解决方案,创建,修改和使用循环中的每个对象:

while(...) {
    // create a new object
    $obj = new Object();
    ... do stuff to $obj
    $obj->send(); 
}

或创建单个对象,修改并构建它,然后使用单个对象

// create a single object outside the loop
$obj = new Object();
while(...) {
    ... do stuff to $obj
}
// Send the single object
$obj->send(); 

答案 1 :(得分:0)

您的电子邮件对象不得在while循环中反复重复。它必须只在您的页面中出现一次,在while循环中您必须获得$ mail-&gt; Body的完整详细信息,如下所示:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "172.16.x.xx"; //my company host
$mail->PORT = "25";
$mail->SMTPAuth = true;  
$mail->Username = "myemail@acc.co.id"; //my company email  
$mail->Password = "mypassword"; // SMTP password
$mail->IsHTML(true); 

$mail->From = "myemail@acc.co.id";
$mail->FromName = "ITCareHelpdesk ACC";
$mail->AddAddress("myemail@gmail.com");                            
$mail->mailtype = "html";
$mail->Subject = "Weekly monitoring tiket open ITcare"; 

$query = mysql_query("this is my query");
$body .= "<table border=1>";
while ($data = mysql_fetch_assoc($query)) {
      // I show my query result in `$mail->Body`
$body .= "
     <tr>
    <th>Ticket ID</th>
    <th>Kategori</th>
    <th>Sub kategori</th>                         
    <th>Deskripsi</th>                         
    <th>NPK</th>                         
    <th>Nama</th>                         
    <th>Posisi</th>                         
    <th>Branch</th>                         
    <th>Waktu mulai</th>                         
    <th>Group section</th>                         
    <th>Pic Handle</th>
    <th>Pic Group Section</th>                                                  
  </tr>
  <tr $color>
      <td>$data[TICKET_ID]</td>
      <td>$data[kategori]</td>
      <td>$data[sub_sub_sub_kategori]</td>
      <td>$data[description]</td>
      <td>$data[npk]</td>
      <td>$data[customer_name]</td>
      <td>$data[position]</td>
      <td>$data[branch]</td>
      <td>$data[start_IT]</td>
      <td>$data[group_section]</td>
      <td>$data[pic_handle]</td>
      <td>$data[pic_group_section]</td>
    </tr>";
}
$body .= "</table>";
$mail->Body = $body;
$mail->send();