Skip to content

Add EventDispatcher support to MailerService#1

Open
booooza wants to merge 1 commit intoneos:mainfrom
onivaevents:event-dispatcher
Open

Add EventDispatcher support to MailerService#1
booooza wants to merge 1 commit intoneos:mainfrom
onivaevents:event-dispatcher

Conversation

@booooza
Copy link

@booooza booooza commented Sep 29, 2025

  • Adds an optional EventDispatcherInterface parameter to getMailer() method
  • Maintains backward compatibility with existing code
  • Enables listening to Symfony Mailer events (MessageEvent, SentMessageEvent, etc.)

This allows consuming packages to access debugging information like message IDs and transport debug data through Symfony's event system (see https://symfony.com/doc/current/mailer.html#debugging-emails).

For example you could define a simple event dispatcher that emits flow signals or execute any other logic.

<?php
declare(strict_types=1);

namespace Neos\SymfonyMailer\EventDispatcher;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\SignalSlot\Dispatcher;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Event\FailedMessageEvent;
use Symfony\Component\Mailer\Event\SentMessageEvent;

#[Flow\Scope("singleton")]
class FlowSignalEventDispatcher implements EventDispatcherInterface
{
    public function dispatch(object $event): object
    {
        if ($event instanceof SentMessageEvent) {
            $this->emitSentMessageEvent($event);
        } elseif ($event instanceof FailedMessageEvent) {
            $this->emitFailedMessageEvent($event);
        }

        return $event;
    }

    #[Flow\Signal]
    protected function emitSentMessageEvent(SentMessageEvent $event): void
    {}

    #[Flow\Signal]
    protected function emitFailedMessageEvent(FailedMessageEvent $event): void
    {}
}

If you think it makes sense, I could also include the above example as a default dispatcher with this PR.

@markusguenther
Copy link
Member

Thank you very much. As this changes the API, although it is optional, I would see this change for a coming 1.1 release. I will test it soon and let you know.

But great that you came up with that change :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments