diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index ee63b9f8ce..74230b5ba1 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -130,14 +130,10 @@ //define('MAIL_DEBUG',true); //define('LOG_CALLER', true); -//require_once(e_HANDLER.'phpmailer/class.phpmailer.php'); -//require_once(e_HANDLER.'phpmailer/class.smtp.php'); -//require_once(e_HANDLER.'phpmailer/PHPMailerAutoload.php'); - +use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\PHPMailer; -// use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\POP3; -use PHPMailer\PHPMailer\Exception; + require_once(e_HANDLER.'vendor/autoload.php'); @@ -438,7 +434,7 @@ public function makePrintableAddress($email,$to) */ protected function openLog($logInfo = true) { - if ($this->logEnable && ($this->logHandle === false)) + if ($this->logEnable && !is_resource($this->logHandle)) { $logFileName = MAIL_LOG_PATH.'mailoutlog.log'; $this->logHandle = fopen($logFileName, 'a'); // Always append to file @@ -487,7 +483,7 @@ protected function openLog($logInfo = true) */ protected function logLine($text) { - if ($this->logEnable && ($this->logHandle > 0)) + if ($this->logEnable && is_resource($this->logHandle)) { fwrite($this->logHandle,date('H:i:s y.m.d').' - '.$text."\r\n"); } @@ -496,11 +492,12 @@ protected function logLine($text) } /** - * Close log + * Close log + * @return void */ protected function closeLog() { - if ($this->logEnable && ($this->logHandle > 0)) + if ($this->logEnable && is_resource($this->logHandle)) { fclose($this->logHandle); } diff --git a/e107_tests/tests/unit/e107EmailTest.php b/e107_tests/tests/unit/e107EmailTest.php index c493e6d4fc..4ba19680ef 100644 --- a/e107_tests/tests/unit/e107EmailTest.php +++ b/e107_tests/tests/unit/e107EmailTest.php @@ -276,4 +276,50 @@ function testSentMimeMessage() } + /** + * @see https://github.com/e107inc/e107/issues/5131 + * @throws Exception if the {@link e107Email} object cannot be created. + */ + function testLogFileHandle() + { + $logFilePath = e_ROOT . MAIL_LOG_PATH . 'mailoutlog.log'; + + $randomString1 = uniqid(); + $randomString2 = uniqid(); + + $this->assertFalse($this->fileContainsString($logFilePath, $randomString1)); + $this->assertFalse($this->fileContainsString($logFilePath, $randomString2)); + + $eml = $this->make('e107Email', ['send' => function() { return true; }]); + $eml->logEnable(2); + $eml->sendEmail( + 'nobody@example.com', + "$randomString1 Example", + ['body' => 'Message body'], + ); + $this->assertTrue($this->fileContainsString($logFilePath, $randomString1)); + $eml->sendEmail( + 'nobody2@example.com', + "$randomString2 Example", + ['body' => 'Message body'], + ); + $this->assertTrue($this->fileContainsString($logFilePath, $randomString2)); + } + + /** + * @param $filePath + * @param $string + * @return bool + */ + private function fileContainsString($filePath, $string) + { + if (!file_exists($filePath)) return false; + $handle = fopen($filePath, 'r'); + while (($buffer = fgets($handle)) !== false) { + if (strpos($buffer, $string) !== false) { + return true; + } + } + return false; + } }