8000 [Mailer] Add `MessageSentEvent` by pableu · Pull Request #38519 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
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 @@ -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
-----
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Mailer/Event/MessageSentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
0