Skip to content

Commit

Permalink
bug #32951 [Mailer] Fix dispatcher not available in Mailer (fabpot)
Browse files Browse the repository at this point in the history
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
-------

77951de [Mailer] fixed dispatcher not available in Mailer
  • Loading branch information
fabpot committed Aug 5, 2019
2 parents 0c95aad + 77951de commit e769d52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Expand Up @@ -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" />
Expand Down
26 changes: 15 additions & 11 deletions src/Symfony/Component/Mailer/Mailer.php
Expand Up @@ -17,6 +17,7 @@
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>
Expand All @@ -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;
Expand All @@ -40,18 +42,20 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
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));
}
Expand Down

0 comments on commit e769d52

Please sign in to comment.