diff --git a/src/Symfony/Component/Mailer/CHANGELOG.md b/src/Symfony/Component/Mailer/CHANGELOG.md index cca04f11aa78e..182a3e7980136 100644 --- a/src/Symfony/Component/Mailer/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * added `NativeTransportFactory` to configure a transport based on php.ini settings * added `local_domain`, `restart_threshold`, `restart_threshold_sleep` and `ping_threshold` options for `smtp` * added `command` option for `sendmail` + * added `MessageSentEvent` 4.4.0 ----- diff --git a/src/Symfony/Component/Mailer/Event/MessageSentEvent.php b/src/Symfony/Component/Mailer/Event/MessageSentEvent.php new file mode 100644 index 0000000000000..eda738b52483d --- /dev/null +++ b/src/Symfony/Component/Mailer/Event/MessageSentEvent.php @@ -0,0 +1,47 @@ + + * + * 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\Envelope; +use Symfony\Component\Mailer\SentMessage; +use Symfony\Component\Mime\RawMessage; +use Symfony\Contracts\EventDispatcher\Event; + +/** + * Allows access to SentMessage after the mail has been sent. + */ +final class MessageSentEvent extends Event +{ + private $message; + private $transport; + + public function __construct(SentMessage $message, string $transport) + { + $this->message = $message; + $this->transport = $transport; + } + + public function getMessage(): SentMessage + { + return $this->message; + } + + public function setMessage(SentMessage $message): void + { + $this->message = $message; + } + + public function getTransport(): string + { + return $this->transport; + } +} diff --git a/src/Symfony/Component/Mailer/Tests/Transport/AbstractTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/AbstractTransportTest.php index 12e5d526760ff..3000fd8901fd8 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/AbstractTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/AbstractTransportTest.php @@ -12,7 +12,11 @@ namespace Symfony\Component\Mailer\Tests\Transport; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Mailer\Envelope; +use Symfony\Component\Mailer\Event\MessageEvent; +use Symfony\Component\Mailer\Event\MessageSentEvent; use Symfony\Component\Mailer\Exception\LogicException; use Symfony\Component\Mailer\Transport\NullTransport; use Symfony\Component\Mime\Address; @@ -55,4 +59,25 @@ public function testSendingRawMessages() $transport = new NullTransport(); $transport->send(new RawMessage('Some raw email message')); } + + public function testEventDispatching() + { + $message = new RawMessage(''); + $envelope = new Envelope(new Address('fabien@example.com'), [new Address('helene@example.com')]); + + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); + + // we expect to calls to the dispatcher, first with a MessageEvent, and then with a MessageSentEvent + $expectedEvents = [MessageEvent::class, MessageSentEvent::class]; + $eventDispatcher + ->expects($this->exactly(2)) + ->method('dispatch') + ->with($this->callback(function($event) use (&$expectedEvents) { + $currentEvent = array_shift($expectedEvents); + return $event instanceof $currentEvent; + })); + + $transport = new NullTransport($eventDispatcher, $this->createMock(LoggerInterface::class)); + $transport->send($message, $envelope); + } } diff --git a/src/Symfony/Component/Mailer/Transport/AbstractTransport.php b/src/Symfony/Component/Mailer/Transport/AbstractTransport.php index 0bee2bdb2bbe0..b9e479afc32a4 100644 --- a/src/Symfony/Component/Mailer/Transport/AbstractTransport.php +++ b/src/Symfony/Component/Mailer/Transport/AbstractTransport.php @@ -17,6 +17,7 @@ use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\Event\MessageEvent; +use Symfony\Component\Mailer\Event\MessageSentEvent; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\RawMessage; @@ -67,6 +68,9 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa $message = new SentMessage($message, $envelope); $this->doSend($message); + if (null !== $this->dispatcher) { + $this->dispatcher->dispatch(new MessageSentEvent($message, (string) $this)); + } $this->checkThrottling(); return $message;