8000 bug #51568 [Mailer] bug - fix EsmtpTransport variable $code definitio… · symfony/symfony@29f427f · GitHub
[go: up one dir, main page]

Skip to content

Commit 29f427f

Browse files
committed
bug #51568 [Mailer] bug - fix EsmtpTransport variable $code definition (kgnblg)
This PR was merged into the 6.3 branch. Discussion ---------- [Mailer] bug - fix EsmtpTransport variable $code definition | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | --- | License | MIT | Doc PR | --- This PR contains a basic fix of a bug in SymfonyMailer component (`Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport` class). If we use `EsmtpTransport` like the given example, it needs to throw TransportException which has been defined in the `handleAuth()` of `EsmtpTransport` class. (Because we called `setAuthenticators()` with empty array and the function emptied the authenticators. We can imagine that we forget to set authenticators.) ``` $stream = new DummyStream(); $transport = new EsmtpTransport(stream: $stream); $transport->setUsername('testuser'); $transport->setPassword('p4ssw0rd'); $transport->setAuthenticators([]); .. .. $transport->send(...); ``` This code piece needs to throw following TransportException with code 504; ``` Failed to find an authenticator supported by the SMTP server, which currently supports: "plain", "login", "cram-md5", "xoauth2". ``` However it shows a PHP Error; ``` Undefined variable $code ``` I included a unit test function `testConstructorWithEmptyAuthenticator()` inside of the `EsmtpTransportTest` class with a fix of the problem. Commits ------- 772050b bug [mailer] fix EsmtpTransport variable $code definition
2 parents 64303aa + 772050b commit 29f427f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 22 additions & 0 deletions
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('sender@example.org');
206+
$message->addTo('recipient@example.org');
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

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

Lines changed: 1 addition & 1 deletion
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< 5CD3 /code>
}
194195

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

0 commit comments

Comments
 (0)
0