From 07a5e097418132a364ac79445685bacb72c7d1af Mon Sep 17 00:00:00 2001 From: Saya <379924+chu121su12@users.noreply.github.com> Date: Sat, 6 May 2023 01:36:04 +0800 Subject: [PATCH] [10.x] Add url support for mail config (#46964) * Add url support for mail config * ci fix * formatting --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Mail/MailManager.php | 11 ++++++++++- tests/Mail/MailManagerTest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Mail/MailManager.php b/src/Illuminate/Mail/MailManager.php index daa5a032209c..ecf094af2c15 100644 --- a/src/Illuminate/Mail/MailManager.php +++ b/src/Illuminate/Mail/MailManager.php @@ -12,6 +12,7 @@ use Illuminate\Mail\Transport\SesTransport; use Illuminate\Mail\Transport\SesV2Transport; use Illuminate\Support\Arr; +use Illuminate\Support\ConfigurationUrlParser; use Illuminate\Support\Str; use InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -446,9 +447,17 @@ protected function getConfig(string $name) // Here we will check if the "driver" key exists and if it does we will use // the entire mail configuration file as the "driver" config in order to // provide "BC" for any Laravel <= 6.x style mail configuration files. - return $this->app['config']['mail.driver'] + $config = $this->app['config']['mail.driver'] ? $this->app['config']['mail'] : $this->app['config']["mail.mailers.{$name}"]; + + if (isset($config['url'])) { + $config = array_merge($config, (new ConfigurationUrlParser)->parseConfiguration($config)); + + $config['transport'] = Arr::pull($config, 'driver'); + } + + return $config; } /** diff --git a/tests/Mail/MailManagerTest.php b/tests/Mail/MailManagerTest.php index a3b3d51c55ab..deb824da5ec0 100644 --- a/tests/Mail/MailManagerTest.php +++ b/tests/Mail/MailManagerTest.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use Orchestra\Testbench\TestCase; +use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; class MailManagerTest extends TestCase { @@ -27,6 +28,22 @@ public function testEmptyTransportConfig($transport) $this->app['mail.manager']->mailer('custom_smtp'); } + public function testMailUrlConfig() + { + $this->app['config']->set('mail.mailers.smtp_url', [ + 'url' => 'smtp://usr:pwd@127.0.0.2:5876', + ]); + + $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(5876, $transport->getStream()->getPort()); + } + public static function emptyTransportConfigDataProvider() { return [