diff --git a/CHANGELOG.md b/CHANGELOG.md index 98d89cd..6498193 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to `initphp/mailer` are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- With `SMTPKeepAlive` on, a refused `RSET` after the server accepted the message + no longer makes `send()` throw. The message was delivered, so an exception led a + retrying caller to send it twice. The connection is closed and the next `send()` + opens a new one. +- A rejected `AUTH LOGIN` username step now throws a `TransportException` with + the message `SMTP AUTH LOGIN username step was rejected: `. The message + used to contain the base64-encoded username. + ## [3.1.0] - 2026-11-25 ### Security diff --git a/docs/SMTP-Transport.md b/docs/SMTP-Transport.md index b801647..f983124 100644 --- a/docs/SMTP-Transport.md +++ b/docs/SMTP-Transport.md @@ -79,7 +79,10 @@ $mailer = Mailer::newInstance([ With `'SMTPKeepAlive' => true` the connection is reused between messages — an `RSET` is sent instead of `QUIT`, and authentication happens only once. This avoids the connect/handshake cost when sending many messages in a loop. The -connection is closed when the mailer is garbage-collected. +connection is closed when the mailer is garbage-collected. If the server +refuses the `RSET` after it has accepted the message, `send()` still succeeds (the +message was delivered); the connection is closed and the next `send()` opens a new +one. If a send fails at any point — the greeting or `EHLO` is refused, `STARTTLS` is refused or the TLS handshake fails, authentication or a recipient is rejected, diff --git a/src/Transport/SmtpTransport.php b/src/Transport/SmtpTransport.php index 2dc12aa..d4ee176 100644 --- a/src/Transport/SmtpTransport.php +++ b/src/Transport/SmtpTransport.php @@ -187,7 +187,16 @@ private function authenticate(): void throw new TransportException('SMTP AUTH LOGIN was rejected: ' . trim($reply), $this->code($reply)); } - $this->command(base64_encode($this->user), 334); + // The username and password steps are written by hand, not through + // command(), so the base64 credential never reaches an exception message. + $this->client->write(base64_encode($this->user) . $this->newline); + $reply = $this->readReply(); + if (strncmp($reply, '334', 3) !== 0) { + throw new TransportException( + 'SMTP AUTH LOGIN username step was rejected: ' . trim($reply), + $this->code($reply), + ); + } $this->client->write(base64_encode($this->pass) . $this->newline); $reply = $this->readReply(); @@ -201,7 +210,16 @@ private function authenticate(): void private function finish(): void { if ($this->keepAlive) { - $this->command('RSET', 250); + // The message has already been accepted (250). If the reset fails, + // the conversation state is unknown, so the connection is dropped, + // but the send itself succeeded: throwing here would make a caller + // that retries deliver the message twice. The next send() opens a + // fresh connection. + try { + $this->command('RSET', 250); + } catch (Throwable) { + $this->disconnect(); + } return; } diff --git a/tests/Transport/SmtpTransportTest.php b/tests/Transport/SmtpTransportTest.php index db34977..0924b15 100644 --- a/tests/Transport/SmtpTransportTest.php +++ b/tests/Transport/SmtpTransportTest.php @@ -411,6 +411,45 @@ public function testAKeptAliveConnectionAuthenticatesAgainAfterAFailureFollowing $this->assertSame(2, substr_count($client->conversation(), 'AUTH LOGIN')); } + public function testAFailedResetAfterAnAcceptedMessageDoesNotFailTheSend(): void + { + $client = new FakeSocketClient([ + // First send: the message is queued, then RSET is refused. + "220 hi\r\n", "250 ehlo\r\n", "250 from\r\n", "250 rcpt\r\n", "354 data\r\n", "250 queued\r\n", "500 what\r\n", + // Second send: a fresh session. + "220 hi\r\n", "250 ehlo\r\n", "250 from\r\n", "250 rcpt\r\n", "354 data\r\n", "250 queued\r\n", "250 reset\r\n", + ]); + $transport = $this->transport($client, keepAlive: true); + + $transport->send($this->email()); + + $this->assertFalse($client->isOpen()); + $this->assertSame(1, $client->openCount); + + $transport->send($this->email()); + + $this->assertTrue($client->isOpen()); + $this->assertSame(2, $client->openCount); + $this->assertSame(2, substr_count($client->conversation(), 'DATA')); + } + + public function testARejectedAuthUsernameStepDoesNotLeakTheBase64UsernameInTheMessage(): void + { + $client = new FakeSocketClient(["220 hi\r\n", "250 ehlo\r\n", "334 VXNlcm5hbWU6\r\n", "535 no such user\r\n"]); + $transport = $this->transport($client, auth: true, user: 'user@example.com', pass: 'secret'); + + try { + $transport->send($this->email()); + $this->fail('Expected a TransportException.'); + } catch (TransportException $e) { + $this->assertSame(535, $e->getCode()); + $this->assertStringContainsString('username step was rejected', $e->getMessage()); + $this->assertStringContainsString('no such user', $e->getMessage()); + $this->assertStringNotContainsString(base64_encode('user@example.com'), $e->getMessage()); + $this->assertFalse($client->isOpen()); + } + } + public function testTheOrcptParameterIsXtextEncodedSoAnAddressCannotAddParameters(): void { $client = new FakeSocketClient([