Skip to content

[8.x] Add Failover Swift Transport driver #38344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Illuminate/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Postmark\Transport as PostmarkTransport;
use Psr\Log\LoggerInterface;
use Swift_DependencyContainer;
use Swift_FailoverTransport as FailoverTransport;
use Swift_Mailer;
use Swift_SendmailTransport as SendmailTransport;
use Swift_SmtpTransport as SmtpTransport;
Expand Down Expand Up @@ -341,6 +342,34 @@ protected function createPostmarkTransport(array $config)
});
}

/**
* Create an instance of the Failover Swift Transport driver.
*
* @param array $config
* @return \Swift_FailoverTransport
*/
protected function createFailoverTransport(array $config)
{
$transports = [];

foreach ($config['mailers'] as $name) {
$config = $this->getConfig($name);

if (is_null($config)) {
throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
}

// Now, we will check if the "driver" key exists and if it does we will set
// the transport configuration parameter in order to offer compatibility
// with any Laravel <= 6.x application style mail configuration files.
$transports[] = $this->app['config']['mail.driver']
? $this->createTransport(array_merge($config, ['transport' => $name]))
: $this->createTransport($config);
}

return new FailoverTransport($transports);
}

/**
* Create an instance of the Log Swift Transport driver.
*
Expand Down
63 changes: 63 additions & 0 deletions tests/Mail/MailFailoverTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Illuminate\Tests\Mail;

use Illuminate\Mail\Transport\ArrayTransport;
use Orchestra\Testbench\TestCase;

class MailFailoverTransportTest extends TestCase
{
public function testGetFailoverTransportWithConfiguredTransports()
{
$this->app['config']->set('mail.default', 'failover');

$this->app['config']->set('mail.mailers', [
'failover' => [
'transport' => 'failover',
'mailers' => [
'sendmail',
'array',
],
],

'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],

'array' => [
'transport' => 'array',
],
]);

$transport = app('mailer')->getSwiftMailer()->getTransport();
$this->assertInstanceOf(\Swift_FailoverTransport::class, $transport);

$transports = $transport->getTransports();
$this->assertCount(2, $transports);
$this->assertInstanceOf(\Swift_SendmailTransport::class, $transports[0]);
$this->assertEquals('/usr/sbin/sendmail -bs', $transports[0]->getCommand());
$this->assertInstanceOf(ArrayTransport::class, $transports[1]);
}

public function testGetFailoverTransportWithLaravel6StyleMailConfiguration()
{
$this->app['config']->set('mail.driver', 'failover');

$this->app['config']->set('mail.mailers', [
'sendmail',
'array',
]);

$this->app['config']->set('mail.sendmail', '/usr/sbin/sendmail -bs');

$transport = app('mailer')->getSwiftMailer()->getTransport();
$this->assertInstanceOf(\Swift_FailoverTransport::class, $transport);

$transports = $transport->getTransports();
$this->assertCount(2, $transports);
$this->assertInstanceOf(\Swift_SendmailTransport::class, $transports[0]);
$this->assertEquals('/usr/sbin/sendmail -bs', $transports[0]->getCommand());
$this->assertInstanceOf(ArrayTransport::class, $transports[1]);
}
}