如何使用HTML表格在电子邮件中发送数据PHP

时间:2015-12-28 04:11:03

标签: php html mysql email

当用户单击“提交”按钮时,我正在尝试以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";
}

3 个答案:

答案 0 :(得分:0)

你的代码都错了,你在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";
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

感谢所有回答我问题的人。这是我系统的工作代码。

   <?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";
    }
     ?>