Мне нужно отправить данные из mysql db по электронной почте.Я использую php mailer в качестве отправителя почты.Я нашел решения, где я могу отправить его в виде вложения, но не в виде отформатированного тела.Когда я делаю get_file_contents, «displaywebpage.php» считывает только форматирование Html, и весь php выходит в виде строк.Не звоню БД или что-нибудь.Когда я проверяю в браузере на 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>