Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <reply>`. The message
used to contain the base64-encoded username.

## [3.1.0] - 2026-11-25

### Security
Expand Down
5 changes: 4 additions & 1 deletion docs/SMTP-Transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions src/Transport/SmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Transport/SmtpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading