8000 [Mailer] Add new events · symfony/symfony@d1138b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1138b2

Browse files
committed
[Mailer] Add new events
1 parent 7940cc4 commit d1138b2

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add a `mailer:test` command
8+
* Add `SentMessageEvent` and `FailedMessageEvent` events
89

910
6.1
1011
---
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Event;
13+
14+
use Symfony\Component\Mime\RawMessage;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Fabien Potencier <fabien@symfony.com>
19+
*/
20+
final class FailedMessageEvent extends Event
21+
{
22+
public function __construct(
23+
private RawMessage $message,
24+
private \Throwable $error,
25+
) {
26+
}
27+
28+
public function getMessage(): RawMessage
29+
{
30+
return $this->message;
31+
}
32+
33+
public function getError(): \Throwable
34+
{
35+
return $this->error;
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Event;
13+
14+
use Symfony\Component\Mailer\SentMessage;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Fabien Potencier <fabien@symfony.com>
19+
*/
20+
final class SentMessageEvent extends Event
21+
{
22+
public function __construct(private SentMessage $message)
23+
{
24+
}
25+
26+
public function getMessage(): SentMessage
27+
{
28+
return $this->message;
29+
}
30+
}

src/Symfony/Component/Mailer/Transport/AbstractTransport.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Psr\Log\LoggerInterface;
1616
use Psr\Log\NullLogger;
1717
use Symfony\Component\Mailer\Envelope;
18+
use Symfony\Component\Mailer\Event\FailedMessageEvent;
1819
use Symfony\Component\Mailer\Event\MessageEvent;
20+
use Symfony\Component\Mailer\Event\SentMessageEvent;
1921
use Symfony\Component\Mailer\SentMessage;
2022
use Symfony\Component\Mime\Address;
2123
use Symfony\Component\Mime\RawMessage;
@@ -58,18 +60,35 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
5860
$message = clone $message;
5961
$envelope = null !== $envelope ? clone $envelope : Envelope::create($message);
6062

61-
if (null !== $this->dispatcher) {
63+
try {
64+
if (!$this->dispatcher) {
65+
$sentMessage = new SentMessage($message, $envelope);
66+
$this->doSend($sentMessage);
67+
68+
return $sentMessage;
69+
}
70+
6271
$event = new MessageEvent($message, $envelope, (string) $this);
6372
$this->dispatcher->dispatch($event);
6473
$envelope = $event->getEnvelope();
65-
}
6674

67-
$message = new SentMessage($message, $envelope);
68-
$this->doSend($message);
75+
$sentMessage = new SentMessage($message, $envelope);
6976

70-
$this->checkThrottling();
77+
try {
78+
$this->doSend($sentMessage);
79+
} catch (\Throwable $error) {
80+
$this->dispatcher->dispatch(new FailedMessageEvent($message, $error));
81+
$this->checkThrottling();
7182

72-
return $message;
83+
throw $error;
84+
}
85+
86+
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
87+
88+
return $sentMessage;
89+
} finally {
90+
$this->checkThrottling();
91+
}
7392
}
7493

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

0 commit comments

Comments
 (0)
0