Description
Description
To allow routing different emails to different messenger transports (not mailer transport), it would be good if there was a way to choose the message class used, instead of having Mailer::send hardcode it to Symfony\Component\Mailer\Messenger\SendEmailMessage
.
P.S: I found the docs a little confusing as I tried setting up Mailer + Messenger together for the first time, and as both use "transport", the Mailer docs say to send to another transport you can use an X-Transport header, so I tried setting that to my messenger transport I wanted to route to, and then got an error that the transport didn't exist. Maybe the docs should be more clear there.
Example
I had to copy the entire Mailer class to do this, as there is no easy way to change the message class used, and as anyway it is final. But anyway I hope it illustrates what I am trying to get to.
<?php
namespace App;
use Symfony\Component\Mailer\Messenger\SendEmailMessage as BaseSendEmailMessage;
class SendEmailMessage extends BaseSendEmailMessage
{
}
class SendUrgentEmailMessage extends BaseSendEmailMessage
{
}
class Mailer implements MailerInterface
{
public function send(RawMessage $message, Envelope $envelope = null): void
{
$this->doSend(SendEmailMessage::class, $message, $envelope);
}
public function sendUrgently(RawMessage $message, Envelope $envelope = null): void
{
$this->doSend(SendUrgentEmailMessage::class, $message, $envelope);
}
private function doSend(string $messageClass, RawMessage $message, Envelope $envelope = null)
{
if (null === $this->bus) {
$this->transport->send($message, $envelope);
return;
}
if (null !== $this->dispatcher) {
$message = clone $message;
$envelope = null !== $envelope ? clone $envelope : Envelope::create($message);
$event = new MessageEvent($message, $envelope, (string) $this->transport, true);
$this->dispatcher->dispatch($event);
}
$this->bus->dispatch(new $messageClass($message, $envelope));
}
}
Now with this I am able to route urgent emails to a different messenger queue which is processed faster.