Skip to content

[Mailer] Add new events #47080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add a `mailer:test` command
* Add `SentMessageEvent` and `FailedMessageEvent` events

6.1
---
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Mailer/Event/FailedMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Event;

use Symfony\Component\Mime\RawMessage;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
final class FailedMessageEvent extends Event
{
public function __construct(
private RawMessage $message,
private \Throwable $error,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not public and no getter ?

For further potential BC handling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it, there is a getError() method here 😕

) {
}

public function getMessage(): RawMessage
{
return $this->message;
}

public function getError(): \Throwable
{
return $this->error;
}
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Mailer/Event/SentMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Event;

use Symfony\Component\Mailer\SentMessage;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
final class SentMessageEvent extends Event
{
public function __construct(private SentMessage $message)
{
}

public function getMessage(): SentMessage
{
return $this->message;
}
}
31 changes: 25 additions & 6 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Event\FailedMessageEvent;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\SentMessageEvent;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;
Expand Down Expand Up @@ -58,18 +60,35 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
$message = clone $message;
$envelope = null !== $envelope ? clone $envelope : Envelope::create($message);

if (null !== $this->dispatcher) {
try {
if (!$this->dispatcher) {
$sentMessage = new SentMessage($message, $envelope);
$this->doSend($sentMessage);

return $sentMessage;
}

$event = new MessageEvent($message, $envelope, (string) $this);
$this->dispatcher->dispatch($event);
$envelope = $event->getEnvelope();
}

$message = new SentMessage($message, $envelope);
$this->doSend($message);
$sentMessage = new SentMessage($message, $envelope);

$this->checkThrottling();
try {
$this->doSend($sentMessage);
} catch (\Throwable $error) {
$this->dispatcher->dispatch(new FailedMessageEvent($message, $error));
$this->checkThrottling();

return $message;
throw $error;
}

$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));

return $sentMessage;
} finally {
$this->checkThrottling();
}
}

abstract protected function doSend(SentMessage $message): void;
Expand Down