1

How get provider response result after mail send. For example i send message via symfony/mailgun and want to get message-uid from provider after sending message

   $email = (new Email())
        ->from('test@mail.com')
        ->to('foo@bar')
        ->subject('Send email test')
        ->text('email text');

    $this->mailer->send($email); // is there any way to return response result instead void
  • 5
    Possible duplicate of [Swift Mailer Delivery Status](https://stackoverflow.com/questions/5768389/swift-mailer-delivery-status) – Cyprien Aubry Jun 20 '19 at 21:56
  • 2
    @CyprienAubry The new Mailer component is not the same as Swiftmailer though – Jeroen Jun 21 '19 at 09:01
  • Don't think it is possible right now to get a response right away. The emails are not showing up in the Symfony profiler either. check out this Github issue: https://github.com/symfony/symfony/issues/31592#issuecomment-495285055 – Jeroen Jun 21 '19 at 09:18
  • So I think this is not possible. – Cyprien Aubry Jun 21 '19 at 09:19
  • I think it is not possible right now, look here: https://github.com/symfony/mailer/blob/master/Messenger/MessageHandler.php -- `$this->transport->send(...)` actually returns some feedback (instance of class `SentMessage`), but that feedback is discarded. So, no way to get it. You could fork that repo on github, do modifications (not only in this file, there will be needed more changes), and use it in your project (merging changes from upstream from time to time). – alx Jun 22 '19 at 20:30

1 Answers1

2

Facing the same issue I came up with following solution:

  1. Create your own handler for SendEmailMessage (inject original handler and use it's result for your purpose)
<?php

namespace MessengerBundle\MessageHandler;

use Symfony\Component\Mailer\Messenger\MessageHandler;
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class SendEmailMessageHandler implements MessageHandlerInterface
{
    /**
     * @var MessageHandler
     */
    private $defaultHandler;

    /**
     * SendEmailMessageHandler constructor.
     * @param MessageHandler $defaultMessageHandler
     */
    public function __construct(MessageHandler $defaultMessageHandler)
    {
        $this->defaultHandler = $defaultMessageHandler;
    }

    /**
     * @param SendEmailMessage $message
     * @return SentMessage|null
     */
    public function __invoke(SendEmailMessage $message): ?SentMessage
    {
        $handler = $this->defaultHandler;
        $sentMessage = $handler($message);

        //your logic here

        return $sentMessage;
    }
}
  1. Register it as a service:
    MessengerBundle\MessageHandler\SendEmailMessageHandler:
        autowire: true
        autoconfigure: true
        public: false
  1. Replace original Symfony service with yours, otherwise mail will be sent twice (by original and your handlers):
    mailer.messenger.message_handler:
        class: MessengerBundle\MessageHandler\SendEmailMessageHandler

If this async approach don't fits to you, you can try this hack to extend mail service by adding getSentMessage into it.

Hush
  • 21
  • 2