Skip to content

Commit

Permalink
bug [mailer] fix EsmtpTransport variable $code definition
Browse files Browse the repository at this point in the history
the variable $code defined in the foreach loop, if there is no authenticators,
then the $code will not be defined and therefore the following TransportException
gives a PHP error.

the use-case defined as a unit test in EsmtpTransportTest class.
  • Loading branch information
kgnblg committed Sep 5, 2023
1 parent 7b03d9b commit 49e9d2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Tests/Transport/Smtp/EsmtpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ public function testSetAuthenticators()
$stream->getCommands()
);
}

public function testConstructorWithEmptyAuthenticator()
{
$stream = new DummyStream();
$transport = new EsmtpTransport(stream: $stream);
$transport->setUsername('testuser');
$transport->setPassword('p4ssw0rd');
$transport->setAuthenticators([]); // if no authenticators defined, then there needs to be a TransportException

$message = new Email();
$message->from('[email protected]');
$message->addTo('[email protected]');
$message->text('.');

try {
$transport->send($message);
$this->fail('Symfony\Component\Mailer\Exception\TransportException to be thrown');
} catch (TransportException $e) {
$this->assertStringStartsWith('Failed to find an authenticator supported by the SMTP server, which currently supports: "plain", "login", "cram-md5", "xoauth2".', $e->getMessage());
$this->assertEquals(504, $e->getCode());
}
}
}

class CustomEsmtpTransport extends EsmtpTransport
Expand Down
2 changes: 1 addition & 1 deletion Transport/Smtp/EsmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private function handleAuth(array $modes): void
return;
}

$code = null;
$authNames = [];
$errors = [];
$modes = array_map('strtolower', $modes);
Expand All @@ -192,7 +193,6 @@ private function handleAuth(array $modes): void
continue;
}

$code = null;
$authNames[] = $authenticator->getAuthKeyword();
try {
$authenticator->authenticate($this);
Expand Down

0 comments on commit 49e9d2c

Please sign in to comment.