Skip to content

Commit b90c6b1

Browse files
committed
[Mailer] Add support for multiple mailers
1 parent 8da8f82 commit b90c6b1

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* added support for multiple transports on a `Mailer` instance
78
* [BC BREAK] removed the `auth_mode` DSN option (it is now always determined automatically)
89
* STARTTLS cannot be enabled anymore (it is used automatically if TLS is disabled and the server supports STARTTLS)
910
* [BC BREAK] Removed the `encryption` DSN option (use `smtps` instead)

Mailer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* @author Fabien Potencier <fabien@symfony.com>
2424
*/
25-
class Mailer implements MailerInterface
25+
final class Mailer implements MailerInterface
2626
{
2727
private $transport;
2828
private $bus;

Transport.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
2828
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
2929
use Symfony\Component\Mailer\Transport\TransportInterface;
30+
use Symfony\Component\Mailer\Transport\Transports;
3031
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3132
use Symfony\Contracts\HttpClient\HttpClientInterface;
3233

@@ -54,6 +55,13 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher
5455
return $factory->fromString($dsn);
5556
}
5657

58+
public static function fromDsns(array $dsns, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
59+
{
60+
$factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client, $logger)));
61+
62+
return $factory->fromStrings($dsns);
63+
}
64+
5765
/**
5866
* @param TransportFactoryInterface[] $factories
5967
*/
@@ -62,6 +70,16 @@ public function __construct(iterable $factories)
6270
$this->factories = $factories;
6371
}
6472

73+
public function fromStrings(array $dsns): Transports
74+
{
75+
$transports = [];
76+
foreach ($dsns as $name => $dsn) {
77+
$transports[$name] = $this->fromString($dsn);
78+
}
79+
80+
return new Transports($transports);
81+
}
82+
6583
public function fromString(string $dsn): TransportInterface
6684
{
6785
$dsns = preg_split('/\s++\|\|\s++/', $dsn);

Transport/Transports.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Transport;
13+
14+
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
15+
use Symfony\Component\Mailer\Exception\LogicException;
16+
use Symfony\Component\Mailer\SentMessage;
17+
use Symfony\Component\Mailer\SmtpEnvelope;
18+
use Symfony\Component\Mime\Message;
19+
use Symfony\Component\Mime\RawMessage;
20+
21+
/**
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
*/
24+
class Transports implements TransportInterface
25+
{
26+
private $transports;
27+
private $default;
28+
29+
/**
30+
* @param TransportInterface[] $transports
31+
*/
32+
public function __construct(iterable $transports)
33+
{
34+
$this->transports = [];
35+
foreach ($transports as $name => $transport) {
36+
if (null === $this->default) {
37+
$this->default = $transport;
38+
}
39+
$this->transports[$name] = $transport;
40+
}
41+
42+
if (!$this->transports) {
43+
throw new LogicException(sprintf('"%s" must have at least one transport configured.', __CLASS__));
44+
}
45+
}
46+
47+
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage
48+
{
49+
/** @var Message $message */
50+
if (RawMessage::class === \get_class($message) || !$message->getHeaders()->has('X-Transport')) {
51+
return $this->default->send($message, $envelope);
52+
}
53+
54+
$headers = $message->getHeaders();
55+
$transport = $headers->get('X-Transport');
56+
$headers->remove('X-Transport');
57+
58+
if (!isset($this->transports[$transport])) {
59+
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
60+
}
61+
62+
return $this->transports[$transport]->send($message, $envelope);
63+
}
64+
65+
public function __toString(): string
66+
{
67+
return 'all';
68+
}
69+
}

0 commit comments

Comments
 (0)