【问题标题】:How to send data in email using HTML table & php如何使用 HTML 表格和 php 在电子邮件中发送数据
【发布时间】:2016-01-31 13:03:39
【问题描述】:

当用户单击提交按钮时,我正在尝试以 HTML 表的形式从数据库发送数据。我能够收到电子邮件,但只收到表。好像不是数据库数据。我想通过电子邮件发送包含数据库中数据的表格。这是我的代码。

<?php
require '../Mailer/PHPMailerAutoload.php';

$mail = new PHPMailer;  
$mail->isSMTP();     
//$mail->SMTPSecure = 'tls';
//$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Port = ;  
$mail->Username = '';
$mail->Password = '';
$mail->setFrom('');
$mail->addAddress('');
$mail->isHTML(true);
$mail->Subject = 'Student data';
$body = '<html>
        <head>
        <title></title>
         <style>
    table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
  border: 1px solid #ddd;
    text-align: left;
    padding: 8px;
    color:black
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #4CAF50;
    color: white;
}
</style>
</head>
<body>
  
 <?php
      $username = "root";
      $password = "";
      $host = "localhost";

      $connector = mysqli_connect("$host,$username,$password");
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysqli_select_db("school"," $connector");
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysqli_query("SELECT*FROM student ");
      ?>
       <table>
      <thead>
        <tr>
          <th>id</th>
          <th>name</th>
          <th>age</th>
          <th>class</th>
          <th>address</th>
          <th>comment</th>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = mysql_fetch_assoc( $result ) ){?>
          <tr>
              
              
              <td><?php echo $row["id"] ?></td>
              <td><?php echo $row["name"]?></td>
              <td><?php echo $row["age"] ?></td>
              <td><?php echo $row["class"] ?></td>
              <td><?php echo $row["address"] ?></td>
              <td><?php echo $row["comment"] ?></td> 

            </tr>
       <?php   }
        ?>
      </tbody>
    </table>
     <?php mysql_close($connector); ?>
    

 

</body>
</html> ';

$mail->Body = $body;


//send the message, check for errors
if (!$mail->send()) {
    echo "ERROR: " . $mail->ErrorInfo;
} else {
    echo "SUCCESS";
}

【问题讨论】:

  • 在发送之前打印电子邮件并检查它是否正确,我认为这是一个 php 查询问题

标签: php html mysql email


【解决方案1】:

感谢大家回答我的问题。这是我的系统的工作代码。

   <?php
    require '../Mailer/PHPMailerAutoload.php';

  $username = "root";
  $password = "";
  $host = "localhost";

  $connector = mysql_connect($host,$username,$password)
      or die("Unable to connect");
    echo "Connections are made successfully::";
  $selected = mysql_select_db("school", $connector)
    or die("Unable to connect");

  //execute the SQL query and return records
  $result= mysql_query("SELECT * FROM student ORDER BY id DESC limit 1");


    $mail = new PHPMailer;  
    $mail->isSMTP();     
    //$mail->SMTPSecure = 'tls';
    //$mail->SMTPAuth = true;
    $mail->Host = '';
    $mail->Port = ;  
    $mail->Username = '';
    $mail->Password = '';
    $mail->setFrom('');
    $mail->addAddress('');
    $mail->isHTML(true);
    $mail->Subject = 'Student Data';
    $body = "<html>
    <head>
    <title>Student Data</title>
     <style>
     table {
     border-collapse: collapse;
     width: 100%;
      }

    th, td {
    border: 1px solid #ddd;
    text-align: left;
    padding: 8px;
    color:black
     }

   tr:nth-child(even){background-color: #f2f2f2}

     th {
background-color: #4CAF50;
color: white;
    }
  </style>
  </head>
  <body>

   <table>
  <thead>
    <tr>
      <th>id</th>
      <th>Name</th>
      <th>Age</th>
      <th>Class</th>
      <th>Address</th>
      <th>comment</th>
    </tr>
  </thead>
  <tbody>";

while( $row = mysql_fetch_assoc( $result ) ){ 

      $body.= "<tr> 
      <td>".$row['id']."</td>
      <td>".$row['name']."</td>
      <td>".$row['age']."</td>
      <td>".$row['class']."</td>
      <td>".$row['address']."</td>
      <td>".$row['comment']."</td>
      </tr>";  
   }

   $body.="</tbody></table></body></html>";
   ?>
    <?php mysql_close($connector); ?>

    <?php
    $mail->Body = $body;


  //send the message, check for errors
   if (!$mail->send()) {
   echo "ERROR: " . $mail->ErrorInfo;
   } else {
       echo "SUCCESS";
    }
     ?>

【讨论】:

    【解决方案2】:

    在你的mysql连接数据($username, ...)之上仍然是一个php开始标签。

    【讨论】:

      【解决方案3】:

      您的代码全错了,您在 php 标签内打开 php 标签。

      另外,您以错误的方式使用 mysqli_,然后尝试使用 mysql_ 关闭。

      这里,我为你修好了:

      <?php
      require '../Mailer/PHPMailerAutoload.php';
      
      $mail = new PHPMailer;  
      $mail->isSMTP();     
      //$mail->SMTPSecure = 'tls';
      //$mail->SMTPAuth = true;
      $mail->Host = '';
      $mail->Port = ;  
      $mail->Username = '';
      $mail->Password = '';
      $mail->setFrom('');
      $mail->addAddress('');
      $mail->isHTML(true);
      $mail->Subject = 'Student data';
      $body = '<html>
              <head>
              <title></title>
               <style>
          table {
          border-collapse: collapse;
          width: 100%;
      }
      
      th, td {
        border: 1px solid #ddd;
          text-align: left;
          padding: 8px;
          color:black
      }
      
      tr:nth-child(even){background-color: #f2f2f2}
      
      th {
          background-color: #4CAF50;
          color: white;
      }
      </style>
      </head>
      <body>
      ';
            $username = "root";
            $password = "";
            $host = "localhost";
      
            $connector = mysqli_connect($host,$username,$password, "school");
                or die("Unable to connect");
              echo "Connections are made successfully::";
      
            //execute the SQL query and return records
            $result = mysqli_query($connector, "SELECT*FROM student ");
            $body .='
             <table>
            <thead>
              <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>
                <th>class</th>
                <th>address</th>
                <th>comment</th>
              </tr>
            </thead>
            <tbody>';
                while( $row = mysqli_fetch_assoc($connector, $result ) ){
                $body .= "<tr>
                    <td>{$row['id']}</td>
                    <td>{$row['name']}</td>
                    <td>{$row['age']}</td>
                    <td>{$row['class']}</td>
                    <td>{$row['address']}</td>
                    <td>{$row['comment']}</td> 
                </tr>"
              }
              $body .='
            </tbody>
          </table>
      </body>
      </html> ';
      $mail->Body = $body;
      //send the message, check for errors
      if (!$mail->send()) {
          echo "ERROR: " . $mail->ErrorInfo;
      } else {
          echo "SUCCESS";
      }
      

      【讨论】:

      • 我已经尝试了上面的代码,但是我得到了语法错误。
      • 有人帮我修好了,我没注意到 PHP 开头的标签,现在试试
      猜你喜欢
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 2016-11-03
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多