发送MySQL $ body作为表数据而不是附件

时间:2018-12-29 22:18:42

标签: php html html5 phpmailer

我需要通过电子邮件从mysql数据库发送数据。我正在使用php mailer作为邮件发件人。我找到了可以以附件形式发送但不能以格式正文发送的解决方案。当我这样做时,get_file_contents'displaywebpage.php'读取on 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>

2 个答案:

答案 0 :(得分:0)

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

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

  1. 使用Ajax调用displaywebpage.php并回显内容(为您提供执行的代码)

  2. 使用 file_get_content 获取displaywebpage.php的内容,然后在mailsend.php中运行PHP代码,然后将字符串替换为已经获得的内容。

    < / li>

如果要使用第二个选项(我认为这对您来说更容易),则应从displaywebpage.php中删除所有PHP代码,然后将它们添加到mailsend.php中,并使用 str_replace < / em>将您的字符串替换为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);

答案 1 :(得分:0)

min-height文件,并使用output buffering捕获其输出:

include