| 148 |
lars |
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;
|
|
|
13 |
|
|
|
14 |
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
|
15 |
use Symfony\Component\Mailer\Event\MessageEvent;
|
|
|
16 |
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
|
17 |
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
|
|
|
18 |
use Symfony\Component\Mailer\Transport\TransportInterface;
|
|
|
19 |
use Symfony\Component\Messenger\Exception\HandlerFailedException;
|
|
|
20 |
use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
21 |
use Symfony\Component\Mime\RawMessage;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
25 |
*/
|
|
|
26 |
final class Mailer implements MailerInterface
|
|
|
27 |
{
|
|
|
28 |
private TransportInterface $transport;
|
|
|
29 |
private ?MessageBusInterface $bus;
|
|
|
30 |
private ?EventDispatcherInterface $dispatcher;
|
|
|
31 |
|
|
|
32 |
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
|
|
|
33 |
{
|
|
|
34 |
$this->transport = $transport;
|
|
|
35 |
$this->bus = $bus;
|
|
|
36 |
$this->dispatcher = $dispatcher;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function send(RawMessage $message, Envelope $envelope = null): void
|
|
|
40 |
{
|
|
|
41 |
if (null === $this->bus) {
|
|
|
42 |
$this->transport->send($message, $envelope);
|
|
|
43 |
|
|
|
44 |
return;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$stamps = [];
|
|
|
48 |
if (null !== $this->dispatcher) {
|
|
|
49 |
// The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let
|
|
|
50 |
// listeners do something before a message is sent to the queue.
|
|
|
51 |
// We are using a cloned message as we still want to dispatch the **original** message, not the one modified by listeners.
|
|
|
52 |
// That's because the listeners will run again when the email is sent via Messenger by the transport (see `AbstractTransport`).
|
|
|
53 |
// Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.
|
|
|
54 |
$clonedMessage = clone $message;
|
|
|
55 |
$clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
|
|
|
56 |
$event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
|
|
|
57 |
$this->dispatcher->dispatch($event);
|
|
|
58 |
$stamps = $event->getStamps();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
try {
|
|
|
62 |
$this->bus->dispatch(new SendEmailMessage($message, $envelope), $stamps);
|
|
|
63 |
} catch (HandlerFailedException $e) {
|
|
|
64 |
foreach ($e->getNestedExceptions() as $nested) {
|
|
|
65 |
if ($nested instanceof TransportExceptionInterface) {
|
|
|
66 |
throw $nested;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
throw $e;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|