Skip to content

Commit 49e9d2c

Browse files
committed
bug [mailer] fix EsmtpTransport variable $code definition
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.
1 parent 7b03d9b commit 49e9d2c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Tests/Transport/Smtp/EsmtpTransportTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ public function testSetAuthenticators()
192192
$stream->getCommands()
193193
);
194194
}
195+
196+
public function testConstructorWithEmptyAuthenticator()
197+
{
198+
$stream = new DummyStream();
199+
$transport = new EsmtpTransport(stream: $stream);
200+
$transport->setUsername('testuser');
201+
$transport->setPassword('p4ssw0rd');
202+
$transport->setAuthenticators([]); // if no authenticators defined, then there needs to be a TransportException
203+
204+
$message = new Email();
205+
$message->from('[email protected]');
206+
$message->addTo('[email protected]');
207+
$message->text('.');
208+
209+
try {
210+
$transport->send($message);
211+
$this->fail('Symfony\Component\Mailer\Exception\TransportException to be thrown');
212+
} catch (TransportException $e) {
213+
$this->assertStringStartsWith('Failed to find an authenticator supported by the SMTP server, which currently supports: "plain", "login", "cram-md5", "xoauth2".', $e->getMessage());
214+
$this->assertEquals(504, $e->getCode());
215+
}
216+
}
195217
}
196218

197219
class CustomEsmtpTransport extends EsmtpTransport

Transport/Smtp/EsmtpTransport.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ private function handleAuth(array $modes): void
184184
return;
185185
}
186186

187+
$code = null;
187188
$authNames = [];
188189
$errors = [];
189190
$modes = array_map('strtolower', $modes);
@@ -192,7 +193,6 @@ private function handleAuth(array $modes): void
192193
continue;
193194
}
194195

195-
$code = null;
196196
$authNames[] = $authenticator->getAuthKeyword();
197197
try {
198198
$authenticator->authenticate($this);

0 commit comments

Comments
 (0)