From b4b493c8f365b63987b5bdf3ef813d5915a70b5b Mon Sep 17 00:00:00 2001 From: Czirkos Zoltan Date: Wed, 26 Oct 2022 18:05:11 +0200 Subject: [PATCH] Avoid POP3 client error message when TCP connection failed The disconnect() method throws a TypeError when the TCP connection cannot be created. Error and trace: fgets(): Argument #1 ($stream) must be of type resource, bool given phpmailer/phpmailer/src/POP3.php(372): fgets() phpmailer/phpmailer/src/POP3.php(345): PHPMailer\PHPMailer\POP3->getResponse() phpmailer/phpmailer/src/POP3.php(230): PHPMailer\PHPMailer\POP3->disconnect() PHPMailer\PHPMailer\POP3->authorise() Reproduce with: include __DIR__ . "/POP3.php"; \PHPMailer\PHPMailer\POP3::popBeforeSmtp('doesnotexist', 110); --- src/POP3.php | 5 +++++ test/POP3/PopBeforeSmtpTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/POP3.php b/src/POP3.php index 8f8c4530..5018d1b0 100644 --- a/src/POP3.php +++ b/src/POP3.php @@ -337,6 +337,11 @@ class POP3 */ public function disconnect() { + // If could not connect at all, no need to disconnect + if ($this->pop_conn === false) { + return; + } + $this->sendString('QUIT' . static::LE); // RFC 1939 shows POP3 server sending a +OK response to the QUIT command. diff --git a/test/POP3/PopBeforeSmtpTest.php b/test/POP3/PopBeforeSmtpTest.php index 76fe017d..f60fb12e 100644 --- a/test/POP3/PopBeforeSmtpTest.php +++ b/test/POP3/PopBeforeSmtpTest.php @@ -120,4 +120,16 @@ final class PopBeforeSmtpTest extends TestCase @shell_exec('kill -TERM ' . escapeshellarg($pid)); sleep(2); } + + /** + * Test case when POP3 server is unreachable. + */ + public function testPopBeforeSmtpUnreachable() + { + // There is no POP3 server at all. Port is different again. + self::assertFalse( + POP3::popBeforeSmtp('localhost', 1102, 10, 'user', 'xxx'), + 'POP before SMTP should have failed' + ); + } }