Skip to content

Commit

Permalink
[10.x] Add url support for mail config (laravel#46964)
Browse files Browse the repository at this point in the history
* Add url support for mail config

* ci fix

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
chu121su12 and taylorotwell committed May 5, 2023
1 parent 1e6b467 commit 07a5e09
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Illuminate/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Mail/MailManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;

class MailManagerTest extends TestCase
{
Expand All @@ -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 [
Expand Down

0 comments on commit 07a5e09

Please sign in to comment.