[Mailer] fixed dispatcher not available in Mailer

This commit is contained in:
Fabien Potencier 2019-08-05 15:28:06 +02:00
parent 0c95aad721
commit 77951de0e3
2 changed files with 16 additions and 11 deletions

View File

@ -8,6 +8,7 @@
<service id="mailer.mailer" class="Symfony\Component\Mailer\Mailer">
<argument type="service" id="mailer.default_transport" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
</service>
<service id="mailer" alias="mailer.mailer" />
<service id="Symfony\Component\Mailer\MailerInterface" alias="mailer.mailer" />

View File

@ -17,6 +17,7 @@ use Symfony\Component\Mailer\Messenger\SendEmailMessage;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Mime\RawMessage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
@ -25,8 +26,9 @@ class Mailer implements MailerInterface
{
private $transport;
private $bus;
private $dispatcher;
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null)
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
{
$this->transport = $transport;
$this->bus = $bus;
@ -40,18 +42,20 @@ class Mailer implements MailerInterface
return;
}
$message = clone $message;
if (null !== $envelope) {
$envelope = clone $envelope;
} else {
try {
$envelope = new DelayedSmtpEnvelope($message);
} catch (\Exception $e) {
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
if (null !== $this->dispatcher) {
$message = clone $message;
if (null !== $envelope) {
$envelope = clone $envelope;
} else {
try {
$envelope = new DelayedSmtpEnvelope($message);
} catch (\Exception $e) {
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
}
}
$event = new MessageEvent($message, $envelope, $this->transport->getName());
$this->dispatcher->dispatch($event);
}
$event = new MessageEvent($message, $envelope, $this->transport->getName());
$this->dispatcher->dispatch($event);
$this->bus->dispatch(new SendEmailMessage($message, $envelope));
}