bug #32951 [Mailer] Fix dispatcher not available in Mailer (fabpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Fix dispatcher not available in Mailer

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

77951de0e3 [Mailer] fixed dispatcher not available in Mailer
This commit is contained in:
Fabien Potencier 2019-08-05 15:54:05 +02:00
commit e769d5296b
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));
}