Skip to content

[Mailer] messageId set after SentMessageEvent when using SmtpTransport  #52515

Closed
@tomme87

Description

@tomme87

Symfony version(s) affected

6.3.7

Description

When using the SMTP transport and listending for the SentMessageEvent the SentMessage contains the messageId generated by symfony instead of the messageId that is returned from the SMTP server.

This is because the code for setting the messageId is in the send method here:

if ($this->mtaResult && $messageId = $this->parseMessageId($this->mtaResult)) {
$message->setMessageId($messageId);
}

witch is after the event is sent.

How to reproduce

Send a email using the SMTP transport and listen for the SentMessageEvent. In this event, the messageId is wrong.

<?php

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Mailer\Event\SentMessageEvent;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;

require_once 'vendor/autoload.php';

$dispatcher = new EventDispatcher();
$dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
  var_dump($event->getMessage()->getMessageId());
});

$transport = Transport::fromDsn('smtps://usr:pw@email-smtp.eu-west-1.amazonaws.com', $dispatcher);

$email = (new Email())
  ->from('from@example.com')
  ->to('to@example.com')
  ->subject('Test')
  ->text('Testing');

$sentMessage = $transport->send($email);
var_dump($sentMessage->getMessageId()); // This is different from the messageId dumped in the listener!

Possible Solution

Move the code for setting messageId into the doSend method?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions