发送 Mysql $body 作为 table 数据而不是附件

Sending Mysql $body as table data not attachment

我需要通过电子邮件从 mysql 数据库发送数据。我正在使用 php 邮件程序作为邮件发件人。我找到了可以将其作为附件而不是格式化正文发送的解决方案。当我这样做时 get_file_contents 'displaywebpage.php' 读出任何 Html 格式并且所有 php 都以字符串形式出现。不调用 db 或任何东西。当我在 myserver/displaywebpage 上签入浏览器时,php 工作完美,但在发送电子邮件时它不起作用。

这里是mailsend.php

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);  
$body = file_get_contents('displaywebpage.php');


try {
//Server settings
$mail->SMTPDebug = 2;                                
$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPAuth = true;                             
$mail->Username = 'xx@gmail.com';                
$mail->Password = 'mypassword';                           
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;                                   

//Recipients
$mail->setFrom('xx@gmail.com', 'Mailer');
$mail->addAddress('to_xy@gmail.com', 'Joe User');     
// $mail->addAddress('ellen@example.com');              
$mail->addReplyTo('xx', 'Mailer');
// $mail->addCC('cc@example.com');
//  $mail->addBCC('bcc@example.com');

//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//  $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $body;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
  echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

这里是displaywebpage.php

 <?php

$con = mysqli_connect("localhost","user","pass");
 if(!$con){
       die("Database Connection failed".mysqli_error());
}else{
$db_select = mysqli_select_db($con,'test2');
     if(!$db_select){
           die("Database selection failed".mysqli_error());
}else{

   }
}

 $records = mysqli_query($con,"SELECT * FROM test2 ORDER by field2");



?>

<!-- This piece of PHP code defines the structure of the html table -->

 <!DOCTYPE html>
<html>
     <head>
    <title> web report </title>
    </head>

    <body>

    <table width="500" border="2" cellpadding="2" cellspacing='1'>

       <tr bgcolor="#2ECCFA">
                 <th> user</th>
                 <th>Device</th>
                 <th>Last Contact</th>
       </tr>

<!-- We use while loop to fetch data and display rows of date on html table -->

<?php

 while ($course = mysqli_fetch_assoc($records)){

       print "<tr>";
           print "<td>".$course['user']."</td>";
           print "<td>".$course['Device_ID']."</td>";
           print "<td>".$course['Last_Contact']."</td>";
       print "</tr>";

 }
?>
        </table>

   </body>

</html>

file_get_content 获取文件中的所有内容但不会执行文件 !

要解决您的问题,您有两种选择(如果您想保持逻辑):

  1. 使用Ajax调用displaywebpage.php并回显内容(给你执行的代码)

  2. 使用 file_get_content 获取 displaywebpage.php 的内容,然后 运行 您的 PHP 代码在 mailsend.php 里面,然后用你已经得到的内容替换你的字符串。

如果您想使用第二个选项(我认为这对您来说应该更容易),您应该从 displaywebpage.php 中删除所有 PHP 代码并将它们添加到 mailsend.php 并使用 str_replace 将字符串替换为 html :

$body = '<tr>
 <td>[user]</td>
 <td>[Device_ID]</td>
 <td>[Last_Contact]</td>
</tr>'; // this is inside your displaywebpage.php which you got using *file_get_content*


$user = $course['user'];
$device_id = $course['Device_ID'];
$last_content = $course['Last_Contact'];

$body .= str_replace ('[user]', $user, $body);
$body .= str_replace ('[Device_ID]', $device_id, $body);
$body .= str_replace ('[Last_Contact]', $last_content, $body);

include 文件并使用 output buffering:

捕获其输出
ob_start();
include 'displaywebpage.php';
$body = ob_get_contents();
ob_end_clean();