From 2b8b386969d60358eeb8ed5ece2282af4ea68552 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 12 Dec 2024 23:40:28 +0800 Subject: [PATCH] Revert "set schema to smtps if MAIL_ENCRYPTION === tls" (#53863) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert "adjust MailManager to detect if smtp or smtps is needed by checking e…" This reverts commit c07f426e8bfb6fff5a005d40f76ca9502df7318c. * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * Update comment Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Mail/MailManager.php | 4 +- tests/Mail/MailManagerTest.php | 65 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Mail/MailManager.php b/src/Illuminate/Mail/MailManager.php index 148134132cf4..330a13ecaf42 100644 --- a/src/Illuminate/Mail/MailManager.php +++ b/src/Illuminate/Mail/MailManager.php @@ -194,9 +194,7 @@ protected function createSmtpTransport(array $config) $scheme = $config['scheme'] ?? null; if (! $scheme) { - $scheme = ! empty($config['encryption']) && $config['encryption'] === 'tls' - ? 'smtps' - : 'smtp'; + $scheme = ($config['port'] == 465) ? 'smtps' : 'smtp'; } $transport = $factory->create(new Dsn( diff --git a/tests/Mail/MailManagerTest.php b/tests/Mail/MailManagerTest.php index e357457e7648..43d974a2e254 100644 --- a/tests/Mail/MailManagerTest.php +++ b/tests/Mail/MailManagerTest.php @@ -50,6 +50,71 @@ public function testMailUrlConfig($scheme, $port) $this->assertSame('127.0.0.2', $transport->getStream()->getHost()); $this->assertSame($port, $transport->getStream()->getPort()); $this->assertSame($port === 465, $transport->getStream()->isTLS()); + + if (method_exists($transport, 'isAutoTls')) { + // Only available from Symfony Mailer 7.1 + $this->assertTrue($transport->isAutoTls()); + } + } + + #[TestWith([null, 5876])] + #[TestWith([null, 465])] + #[TestWith(['smtp', 25])] + #[TestWith(['smtp', 2525])] + #[TestWith(['smtps', 465])] + #[TestWith(['smtp', 465])] + public function testMailUrlConfigWithAutoTls($scheme, $port) + { + $this->app['config']->set('mail.mailers.smtp_url', [ + 'scheme' => $scheme, + 'url' => "smtp://usr:pwd@127.0.0.2:{$port}?auto_tls=true", + ]); + + $mailer = $this->app['mail.manager']->mailer('smtp_url'); + $transport = $mailer->getSymfonyTransport(); + + $this->assertInstanceOf(EsmtpTransport::class, $transport); + $this->assertSame('usr', $transport->getUsername()); + $this->assertSame('pwd', $transport->getPassword()); + $this->assertSame('127.0.0.2', $transport->getStream()->getHost()); + $this->assertSame($port, $transport->getStream()->getPort()); + $this->assertSame($port === 465, $transport->getStream()->isTLS()); + + if (method_exists($transport, 'isAutoTls')) { + // Only available from Symfony Mailer 7.1 + $this->assertTrue($transport->isAutoTls()); + } + } + + #[TestWith([null, 5876])] + #[TestWith([null, 465])] + #[TestWith(['smtp', 25])] + #[TestWith(['smtp', 2525])] + #[TestWith(['smtps', 465])] + #[TestWith(['smtp', 465])] + public function testMailUrlConfigWithAutoTlsDisabled($scheme, $port) + { + $this->app['config']->set('mail.mailers.smtp_url', [ + 'scheme' => $scheme, + 'url' => "smtp://usr:pwd@127.0.0.2:{$port}?auto_tls=false", + ]); + + $mailer = $this->app['mail.manager']->mailer('smtp_url'); + $transport = $mailer->getSymfonyTransport(); + + $this->assertInstanceOf(EsmtpTransport::class, $transport); + $this->assertSame('usr', $transport->getUsername()); + $this->assertSame('pwd', $transport->getPassword()); + $this->assertSame('127.0.0.2', $transport->getStream()->getHost()); + $this->assertSame($port, $transport->getStream()->getPort()); + + if (method_exists($transport, 'isAutoTls')) { + // Only available from Symfony Mailer 7.1 + $this->assertFalse($transport->isAutoTls()); + $this->assertSame($port === 465 && $scheme !== 'smtp', $transport->getStream()->isTLS()); + } else { + $this->assertSame($port === 465, $transport->getStream()->isTLS()); + } } public function testBuild()