Skip to content

Commit a0021ab

Browse files
nerg4ltaylorotwell
andauthored
[8.x] Add Failover Swift Transport driver (#38344)
* [8.x] Add Failover Swift Transport driver * Apply fixes from StyleCI * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 747f166 commit a0021ab

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/Illuminate/Mail/MailManager.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Postmark\Transport as PostmarkTransport;
1919
use Psr\Log\LoggerInterface;
2020
use Swift_DependencyContainer;
21+
use Swift_FailoverTransport as FailoverTransport;
2122
use Swift_Mailer;
2223
use Swift_SendmailTransport as SendmailTransport;
2324
use Swift_SmtpTransport as SmtpTransport;
@@ -341,6 +342,34 @@ protected function createPostmarkTransport(array $config)
341342
});
342343
}
343344

345+
/**
346+
* Create an instance of the Failover Swift Transport driver.
347+
*
348+
* @param array $config
349+
* @return \Swift_FailoverTransport
350+
*/
351+
protected function createFailoverTransport(array $config)
352+
{
353+
$transports = [];
354+
355+
foreach ($config['mailers'] as $name) {
356+
$config = $this->getConfig($name);
357+
358+
if (is_null($config)) {
359+
throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
360+
}
361+
362+
// Now, we will check if the "driver" key exists and if it does we will set
363+
// the transport configuration parameter in order to offer compatibility
364+
// with any Laravel <= 6.x application style mail configuration files.
365+
$transports[] = $this->app['config']['mail.driver']
366+
? $this->createTransport(array_merge($config, ['transport' => $name]))
367+
: $this->createTransport($config);
368+
}
369+
370+
return new FailoverTransport($transports);
371+
}
372+
344373
/**
345374
* Create an instance of the Log Swift Transport driver.
346375
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Mail;
4+
5+
use Illuminate\Mail\Transport\ArrayTransport;
6+
use Orchestra\Testbench\TestCase;
7+
8+
class MailFailoverTransportTest extends TestCase
9+
{
10+
public function testGetFailoverTransportWithConfiguredTransports()
11+
{
12+
$this->app['config']->set('mail.default', 'failover');
13+
14+
$this->app['config']->set('mail.mailers', [
15+
'failover' => [
16+
'transport' => 'failover',
17+
'mailers' => [
18+
'sendmail',
19+
'array',
20+
],
21+
],
22+
23+
'sendmail' => [
24+
'transport' => 'sendmail',
25+
'path' => '/usr/sbin/sendmail -bs',
26+
],
27+
28+
'array' => [
29+
'transport' => 'array',
30+
],
31+
]);
32+
33+
$transport = app('mailer')->getSwiftMailer()->getTransport();
34+
$this->assertInstanceOf(\Swift_FailoverTransport::class, $transport);
35+
36+
$transports = $transport->getTransports();
37+
$this->assertCount(2, $transports);
38+
$this->assertInstanceOf(\Swift_SendmailTransport::class, $transports[0]);
39+
$this->assertEquals('/usr/sbin/sendmail -bs', $transports[0]->getCommand());
40+
$this->assertInstanceOf(ArrayTransport::class, $transports[1]);
41+
}
42+
43+
public function testGetFailoverTransportWithLaravel6StyleMailConfiguration()
44+
{
45+
$this->app['config']->set('mail.driver', 'failover');
46+
47+
$this->app['config']->set('mail.mailers', [
48+
'sendmail',
49+
'array',
50+
]);
51+
52+
$this->app['config']->set('mail.sendmail', '/usr/sbin/sendmail -bs');
53+
54+
$transport = app('mailer')->getSwiftMailer()->getTransport();
55+
$this->assertInstanceOf(\Swift_FailoverTransport::class, $transport);
56+
57+
$transports = $transport->getTransports();
58+
$this->assertCount(2, $transports);
59+
$this->assertInstanceOf(\Swift_SendmailTransport::class, $transports[0]);
60+
$this->assertEquals('/usr/sbin/sendmail -bs', $transports[0]->getCommand());
61+
$this->assertInstanceOf(ArrayTransport::class, $transports[1]);
62+
}
63+
}

0 commit comments

Comments
 (0)