Skip to content

Commit a570e63

Browse files
committed
[Mailer] Fix edge cases in STMP transports
1 parent d82f7df commit a570e63

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Mailer\Envelope;
16+
use Symfony\Component\Mailer\Exception\LogicException;
1617
use Symfony\Component\Mailer\Exception\TransportException;
1718
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
1819
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
@@ -133,6 +134,37 @@ public function testWriteEncodedRecipientAndSenderAddresses()
133134
$this->assertContains("RCPT TO:<recipient@xn--exmple-cua.org>\r\n", $stream->getCommands());
134135
$this->assertContains("RCPT TO:<recipient2@example.org>\r\n", $stream->getCommands());
135136
}
137+
138+
public function testAssertResponseCodeNoCodes()
139+
{
140+
$this->expectException(LogicException::class);
141+
$this->invokeAssertResponseCode('response', []);
142+
}
143+
144+
public function testAssertResponseCodeWithEmptyResponse()
145+
{
146+
$this->expectException(TransportException::class);
147+
$this->expectExceptionMessage('Expected response code "220" but got empty code.');
148+
$this->invokeAssertResponseCode('', [220]);
149+
}
150+
151+
public function testAssertResponseCodeWithNotValidCode()
152+
{
153+
$this->expectException(TransportException::class);
154+
$this->expectExceptionMessage('Expected response code "220" but got code "550", with message "550 Access Denied".');
155+
$this->expectExceptionCode(550);
156+
$this->invokeAssertResponseCode('550 Access Denied', [220]);
157+
}
158+
159+
private function invokeAssertResponseCode(string $response, array $codes): void
160+
{
161+
162+
$transport = new SmtpTransport($this->getMockForAbstractClass(AbstractStream::class));
163+
$m = new \ReflectionMethod($transport, 'assertResponseCode');
164+
$m->setAccessible(true);
165+
166+
$m->invoke($transport, $response, $codes);
167+
}
136168
}
137169

138170
class DummyStream extends AbstractStream

src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransport.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public function addAuthenticator(AuthenticatorInterface $authenticator): void
9494

9595
protected function doHeloCommand(): void
9696
{
97-
$capabilities = $this->callHeloCommand();
97+
if (!$capabilities = $this->callHeloCommand()) {
98+
return;
99+
}
98100

99101
/** @var SocketStream $stream */
100102
$stream = $this->getStream();
@@ -123,6 +125,8 @@ private function callHeloCommand(): array
123125
} catch (TransportExceptionInterface $e) {
124126
try {
125127
parent::doHeloCommand();
128+
129+
return [];
126130
} catch (TransportExceptionInterface $ex) {
127131
if (!$ex->getCode()) {
128132
throw $e;

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ private function assertResponseCode(string $response, array $codes): void
301301
if (!$valid || !$response) {
302302
$codeStr = $code ? sprintf('code "%s"', $code) : 'empty code';
303303
$responseStr = $response ? sprintf(', with message "%s"', trim($response)) : '';
304-
throw new TransportException(sprintf('Expected response code "%s" but got ', implode('/', $codes), $codeStr).$codeStr.$responseStr.'.', $code);
304+
305+
throw new TransportException(sprintf('Expected response code "%s" but got ', implode('/', $codes)).$codeStr.$responseStr.'.', $code ?: 0);
305306
}
306307
}
307308

0 commit comments

Comments
 (0)