diff --git a/src/Illuminate/Mail/MailManager.php b/src/Illuminate/Mail/MailManager.php index a9b72f419834..6cd752e8a56d 100644 --- a/src/Illuminate/Mail/MailManager.php +++ b/src/Illuminate/Mail/MailManager.php @@ -120,8 +120,28 @@ protected function resolve($name) // Once we have created the mailer instance we will set a container instance // on the mailer. This allows us to resolve mailer classes via containers // for maximum testability on said classes instead of passing Closures. + $mailer = $this->build(['name' => $name, ...$config]); + + // Next we will set all of the global addresses on this mailer, which allows + // for easy unification of all "from" addresses as well as easy debugging + // of sent messages since these will be sent to a single email address. + foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { + $this->setGlobalAddress($mailer, $config, $type); + } + + return $mailer; + } + + /** + * Build a new mailer instance. + * + * @param array $config + * @return \Illuminate\Mail\Mailer + */ + public function build($config) + { $mailer = new Mailer( - $name, + $config['name'] ?? 'ondemand', $this->app['view'], $this->createSymfonyTransport($config), $this->app['events'] @@ -131,13 +151,6 @@ protected function resolve($name) $mailer->setQueue($this->app['queue']); } - // Next we will set all of the global addresses on this mailer, which allows - // for easy unification of all "from" addresses as well as easy debugging - // of sent messages since these will be sent to a single email address. - foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { - $this->setGlobalAddress($mailer, $config, $type); - } - return $mailer; } diff --git a/src/Illuminate/Support/Facades/Mail.php b/src/Illuminate/Support/Facades/Mail.php index 7223a76e87cc..fb4019b25aae 100755 --- a/src/Illuminate/Support/Facades/Mail.php +++ b/src/Illuminate/Support/Facades/Mail.php @@ -7,6 +7,7 @@ /** * @method static \Illuminate\Contracts\Mail\Mailer mailer(string|null $name = null) * @method static \Illuminate\Mail\Mailer driver(string|null $driver = null) + * @method static \Illuminate\Mail\Mailer build(array $config) * @method static \Symfony\Component\Mailer\Transport\TransportInterface createSymfonyTransport(array $config) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) diff --git a/tests/Mail/MailManagerTest.php b/tests/Mail/MailManagerTest.php index 4ae036416be9..91a4be5875f0 100644 --- a/tests/Mail/MailManagerTest.php +++ b/tests/Mail/MailManagerTest.php @@ -43,6 +43,28 @@ public function testMailUrlConfig() $this->assertSame(5876, $transport->getStream()->getPort()); } + public function testBuild() + { + $config = [ + 'transport' => 'smtp', + 'host' => '127.0.0.2', + 'port' => 5876, + 'encryption' => 'tls', + 'username' => 'usr', + 'password' => 'pwd', + 'timeout' => 5, + ]; + + $mailer = $this->app['mail.manager']->build($config); + $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 [