Skip to content

Commit fb0e5c0

Browse files
authored
[11.x] Add ability to dynamically build mailers on-demand using Mail::build (#53411)
* Add ability to dynamically build a mailer instance * Add test * Use ondemand as a default mailer name
1 parent e2f1d01 commit fb0e5c0

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/Illuminate/Mail/MailManager.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,28 @@ protected function resolve($name)
120120
// Once we have created the mailer instance we will set a container instance
121121
// on the mailer. This allows us to resolve mailer classes via containers
122122
// for maximum testability on said classes instead of passing Closures.
123+
$mailer = $this->build(['name' => $name, ...$config]);
124+
125+
// Next we will set all of the global addresses on this mailer, which allows
126+
// for easy unification of all "from" addresses as well as easy debugging
127+
// of sent messages since these will be sent to a single email address.
128+
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
129+
$this->setGlobalAddress($mailer, $config, $type);
130+
}
131+
132+
return $mailer;
133+
}
134+
135+
/**
136+
* Build a new mailer instance.
137+
*
138+
* @param array $config
139+
* @return \Illuminate\Mail\Mailer
140+
*/
141+
public function build($config)
142+
{
123143
$mailer = new Mailer(
124-
$name,
144+
$config['name'] ?? 'ondemand',
125145
$this->app['view'],
126146
$this->createSymfonyTransport($config),
127147
$this->app['events']
@@ -131,13 +151,6 @@ protected function resolve($name)
131151
$mailer->setQueue($this->app['queue']);
132152
}
133153

134-
// Next we will set all of the global addresses on this mailer, which allows
135-
// for easy unification of all "from" addresses as well as easy debugging
136-
// of sent messages since these will be sent to a single email address.
137-
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
138-
$this->setGlobalAddress($mailer, $config, $type);
139-
}
140-
141154
return $mailer;
142155
}
143156

src/Illuminate/Support/Facades/Mail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/**
88
* @method static \Illuminate\Contracts\Mail\Mailer mailer(string|null $name = null)
99
* @method static \Illuminate\Mail\Mailer driver(string|null $driver = null)
10+
* @method static \Illuminate\Mail\Mailer build(array $config)
1011
* @method static \Symfony\Component\Mailer\Transport\TransportInterface createSymfonyTransport(array $config)
1112
* @method static string getDefaultDriver()
1213
* @method static void setDefaultDriver(string $name)

tests/Mail/MailManagerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ public function testMailUrlConfig()
4343
$this->assertSame(5876, $transport->getStream()->getPort());
4444
}
4545

46+
public function testBuild()
47+
{
48+
$config = [
49+
'transport' => 'smtp',
50+
'host' => '127.0.0.2',
51+
'port' => 5876,
52+
'encryption' => 'tls',
53+
'username' => 'usr',
54+
'password' => 'pwd',
55+
'timeout' => 5,
56+
];
57+
58+
$mailer = $this->app['mail.manager']->build($config);
59+
$transport = $mailer->getSymfonyTransport();
60+
61+
$this->assertInstanceOf(EsmtpTransport::class, $transport);
62+
$this->assertSame('usr', $transport->getUsername());
63+
$this->assertSame('pwd', $transport->getPassword());
64+
$this->assertSame('127.0.0.2', $transport->getStream()->getHost());
65+
$this->assertSame(5876, $transport->getStream()->getPort());
66+
}
67+
4668
public static function emptyTransportConfigDataProvider()
4769
{
4870
return [

0 commit comments

Comments
 (0)