MessageEvent example



        $stamps = [];
        if (null !== $this->dispatcher) {
            // The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let             // listeners do something before a message is sent to the queue.             // We are using a cloned message as we still want to dispatch the **original** message, not the one modified by listeners.             // That's because the listeners will run again when the email is sent via Messenger by the transport (see `AbstractTransport`).             // Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.             $clonedMessage = clone $message;
            $clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
            $event = new MessageEvent($clonedMessage$clonedEnvelope(string) $this->transport, true);
            $this->dispatcher->dispatch($event);
            $stamps = $event->getStamps();

            if ($event->isRejected()) {
                return;
            }
        }

        try {
            $this->bus->dispatch(new SendEmailMessage($message$envelope)$stamps);
        } catch (HandlerFailedException $e) {
            


            public function __toString(): string
            {
                return '';
            }
        };

        $message = new DummyMessage();

        $series = [
            new MessageEvent($message),
            new FailedMessageEvent($message$transport->exception),
        ];

        $eventDispatcherMock->expects($this->exactly(2))
            ->method('dispatch')
            ->willReturnCallback(function Dobject $event) use (&$series) {
                $this->assertEquals(array_shift($series)$event);

                return $event;
            });
        try {
            
public function supports(MessageInterface $message): bool
    {
        return $this->transport->supports($message);
    }

    public function send(MessageInterface $message): ?SentMessage
    {
        if (null === $this->bus) {
            return $this->transport->send($message);
        }

        $this->dispatcher?->dispatch(new MessageEvent($message, true));

        $this->bus->dispatch($message);

        return null;
    }
}
public function supports(MessageInterface $message): bool
    {
        return $this->transport->supports($message);
    }

    public function send(MessageInterface $message): ?SentMessage
    {
        if (null === $this->bus) {
            return $this->transport->send($message);
        }

        $this->dispatcher?->dispatch(new MessageEvent($message, true));

        $this->bus->dispatch($message);

        return null;
    }
}
$message = clone $message;
        $envelope = null !== $envelope ? clone $envelope : Envelope::create($message);

        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);
            if ($event->isRejected()) {
                return null;
            }

            $envelope = $event->getEnvelope();
            $message = $event->getMessage();

            if ($message instanceof TemplatedEmail && !$message->isRendered()) {
                throw new LogicException(sprintf('You must configure a "%s" when a "%s" instance has a text or HTML template set.', BodyRendererInterface::classget_debug_type($message)));
            }

            
use Symfony\Component\Mime\Message;

class MessageListenerTest extends TestCase
{
    /** * @dataProvider provideHeaders */
    public function testHeaders(Headers $initialHeaders, Headers $defaultHeaders, Headers $expectedHeaders, array $rules = MessageListener::DEFAULT_RULES)
    {
        $message = new Message($initialHeaders);
        $listener = new MessageListener($defaultHeaders, null, $rules);
        $event = new MessageEvent($messagenew Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]), 'smtp');
        $listener->onMessage($event);

        $this->assertEquals($expectedHeaders$event->getMessage()->getHeaders());
    }

    public static function provideHeaders(): iterable
    {
        $initialHeaders = new Headers();
        $defaultHeaders = (new Headers())
            ->add(new MailboxListHeader('from', [new Address('from-default@example.com')]))
        ;
        
$this->port = $port;

        return $this;
    }

    public function send(MessageInterface $message): SentMessage
    {
        if (null === $this->dispatcher) {
            return $this->doSend($message);
        }

        $this->dispatcher->dispatch(new MessageEvent($message));

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

            throw $error;
        }

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

        
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Message;

class MessengerTransportListenerTest extends TestCase
{
    public function testNoMessengerTransportStampsByDefault()
    {
        $l = new MessengerTransportListener();
        $envelope = new Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]);
        $message = new Message(new Headers());
        $event = new MessageEvent($message$envelope, 'smtp', true);
        $l->onMessage($event);
        $this->assertEmpty($event->getStamps());
    }

    public function testMessengerTransportStampViaHeader()
    {
        $l = new MessengerTransportListener();
        $envelope = new Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]);
        $headers = (new Headers())->addTextHeader('X-Bus-Transport', 'async');
        $message = new Message($headers);
        $event = new MessageEvent($message$envelope, 'smtp', true);
        


    public function send(MessageInterface $message): SentMessage
    {
        $message = new NullMessage($message);
        $sentMessage = new SentMessage($message(string) $this);

        if (null === $this->dispatcher) {
            return $sentMessage;
        }

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

        return $sentMessage;
    }

    public function __toString(): string
    {
        return 'null';
    }

    public function supports(MessageInterface $message): bool
    {
Home | Imprint | This part of the site doesn't use cookies.