PHPMailer无法正确合并日志内容问题排查求助
问题:PHPMailer发送邮件丢失日志内容,网页显示正常
问题背景
我编写了LogFileReader类用于读取当日更新的日志文件内容,并通过PHPMailer发送给自己复核。网页端可正常显示完整内容,但邮件中仅能收到硬编码的头部内容,日志内容丢失。
调试信息
开启PHPMailer调试后得到以下日志:
04-May-2023,18:06:13log_contents completed. 2023-05-04 09:06:13 Connection: opening to XXX.XXX:XXX, timeout=300, options=array () 2023-05-04 09:06:13 Connection: opened 2023-05-04 09:06:13 SERVER -> CLIENT: 220 SPSVRMAILDB02.seapower.mo Microsoft ESMTP MAIL Service ready at Thu, 4 May 2023 18:07:07 +0900 2023-05-04 09:06:13 CLIENT -> SERVER: EHLO XX.DOMAIN.XXX 2023-05-04 09:06:13 SERVER -> CLIENT: 250-SPSVRMAILDB02.seapower.mo Hello [192.168.XXX.XX]250-SIZE 37748736250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-8BITMIME250-BINARYMIME250 CHUNKING 2023-05-04 09:06:13 CLIENT -> SERVER: MAIL FROM:<XX@XXXXX.XXX> 2023-05-04 09:06:13 SERVER -> CLIENT: 250 2.1.0 Sender OK 2023-05-04 09:06:13 CLIENT -> SERVER: RCPT TO:<XX.XX@XX.XX> 2023-05-04 09:06:13 SERVER -> CLIENT: 250 2.1.5 Recipient OK 2023-05-04 09:06:13 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.28 (https://github.com/PHPMailer/PHPMailer) 2023-05-04 09:06:13 CLIENT -> SERVER: MIME-Version: 1.0 2023-05-04 09:06:13 CLIENT -> SERVER: Content-Type: multipart/alternative; 2023-05-04 09:06:13 CLIENT -> SERVER: boundary="b1_377eff022806XXXXXX3ea4f92bb02" 2023-05-04 09:06:13 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2023-05-04 09:06:13 CLIENT -> SERVER: 2023-05-04 09:06:13 CLIENT -> SERVER: This is a multi-part message in MIME format. 2023-05-04 09:06:13 CLIENT -> SERVER: 2023-05-04 09:06:13 CLIENT -> SERVER: --b1_377eff022806cce60XXXXXXf92bb02 2023-05-04 09:06:13 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
已设置
$mail->CharSet = "UTF-8",但仍显示charset=us-ascii是否正常?
测试$mail->MsgHTML($log_contents)仍无效。
相关代码
LogFileReader类
define("TIMESTAMP", date("d-M-Y,H:i:s")); class LogFileReader { private $dir; // Declare a property to store the current directory private $today; // Declare a property to store today's date private $log_contents; // Declare a property to store the log contents private $timestamp; // Declare a property to store system datetime // Declare a constructor that takes the directory as an argument public function __construct($dir) { // Assign the directory to the property $this->dir = $dir; // Get today's date and time in Y-m-d H:i:s format $this->today = date("Y-m-d"); // Prepare params, timestamp with initiates. $this->timestamp = TIMESTAMP; $this->log_contents = " <h2>$this->timestamp daily cron job running.</h2><br>\n"; } // Declare a method that reads and processes the files public function readFiles() { // List all the .txt files in the directory $files = glob($this->dir . "/*.txt"); // Loop through the files and display their content after today's date and time foreach ($files as $file) { $this->log_contents .= "<h4>Filename: " . basename($file) . "</h4><br>\n"; // Read the file content as an array of lines $file_content_lines = file($file); // Get the last 8 lines of the array $last_eight_lines = array_slice($file_content_lines, -8); // Filter out the lines that do not contain today's date and time or later foreach ($last_eight_lines as $line) { // If the line contains today's date and time or later, set the flag to true if (strpos($line, $this->today) !== false || strtotime($line) > strtotime($this->today)) { $flag = true; } // set flag for lines // If the flag is true, print the line if ($flag) { $this->log_contents .= $line; } } $this->log_contents .= " \n ---- ---- <hr>\n"; } } // Declare a method that returns the log contents public function getLogContents() { return $this->log_contents; } }
使用示例
// Create an instance of the FileReader class with the current directory $log_files = new LogFileReader(getcwd()); // Call the readFiles method to process the files $log_files->readFiles(); // Get and print the log contents $log_contents = $log_files->getLogContents();
PHPMailer配置
if (!empty($log_contents)){ // after log_contents completed echo TIMESTAMP . "log_contents completed.<br/>\n"; // confirm log content completed. $email_content_head = " <h1>My Host XXXX daily crontab re-check</h1> <p>--- --- --- ---</p> "; // adding content head $emailContent = $email_content_head . $log_contents; // merge contents // 此处无法正确合并内容
$mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Our Host XXX.XXX.XXX daily crontab re-check'; $mail->Body = $emailContent; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
已尝试添加:
$mail->CharSet = "UTF-8"; // set email encode to UTF-8 $mail->Encoding = 'base64'; // if UTF-8 not working
疑问
$log_contents在邮件中仍为空,var_dump显示其为string类型。请问我遗漏了什么?问题出在哪里?还有其他解决办法吗?
内容的提问来源于stack exchange,提问作者swlok
相关产品推荐
相关产品推荐

