Skip to content

Commit cc3c6a0

Browse files
committed
[Mailer] fixed dispatcher not available in Mailer
1 parent 54af084 commit cc3c6a0

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Mailer.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Mailer\Transport\TransportInterface;
1818
use Symfony\Component\Messenger\MessageBusInterface;
1919
use Symfony\Component\Mime\RawMessage;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2021

2122
/**
2223
* @author Fabien Potencier <[email protected]>
@@ -25,8 +26,9 @@ class Mailer implements MailerInterface
2526
{
2627
private $transport;
2728
private $bus;
29+
private $dispatcher;
2830

29-
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null)
31+
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
3032
{
3133
$this->transport = $transport;
3234
$this->bus = $bus;
@@ -40,18 +42,20 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
4042
return;
4143
}
4244

43-
$message = clone $message;
44-
if (null !== $envelope) {
45-
$envelope = clone $envelope;
46-
} else {
47-
try {
48-
$envelope = new DelayedSmtpEnvelope($message);
49-
} catch (\Exception $e) {
50-
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
45+
if (null !== $this->dispatcher) {
46+
$message = clone $message;
47+
if (null !== $envelope) {
48+
$envelope = clone $envelope;
49+
} else {
50+
try {
51+
$envelope = new DelayedSmtpEnvelope($message);
52+
} catch (\Exception $e) {
53+
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
54+
}
5155
}
56+
$event = new MessageEvent($message, $envelope, $this->transport->getName());
57+
$this->dispatcher->dispatch($event);
5258
}
53-
$event = new MessageEvent($message, $envelope, $this->transport->getName());
54-
$this->dispatcher->dispatch($event);
5559

5660
$this->bus->dispatch(new SendEmailMessage($message, $envelope));
5761
}

0 commit comments

Comments
 (0)