2

In a Laravel 9 project I am using dynamic SMTP setup:

$transport = Transport::fromDsn("smtp://$department->email:$department->smtp_password@$department->smtp_host:$department->smtp_port");
$mailer = new Mailer($transport);

$email = (new Email())
            ->subject($request->email_subject)
            ->from($department->email)
            ->to($request->email_reply_to)
            ->html($request->body);

$headers = $email->getHeaders();

$mailer->send($email);

However I can't get the ID of the message. Before when Laravel was Using the Swift_Mailer I could do this:

$email->getId()

But now I am getting:

Call to undefined method Symfony\Component\Mime\Email::getId()

So how can I retrieve the ID of the message?

EDIT: So in order to get the MessageId I have to get the SentMessage object first and then in the object I can access the getMessageId method.

The problem now is, how to get the SentMessage object?

$mailer->send($email) returns null

Don40
  • 373
  • 3
  • 17
  • Symfony mailer does not provide an id for the mail i don't know why you need the id for – Youssef Saoubou Sep 07 '22 at 08:24
  • 4
    The `send` method returns a `SentMessage` object, and according to https://symfony.com/doc/current/mailer.html#debugging-emails that should have a `getMessageId` method. – CBroe Sep 07 '22 at 08:34
  • The function exists in [Mailer/SentMessage](https://github.com/symfony/symfony/blob/23e7c4a376d177026cb75ad3dbf6299f30033134/src/Symfony/Component/Mailer/SentMessage.php#L66) On which object are you currently calling the method? – Uwe Sep 07 '22 at 09:07
  • @CBroe how can I get the SentMessage object? I've tried everything I can think of, but cant really get how to reach the "SentMessage" and it's methods `$mailer->send($email)` returns null – Don40 Sep 08 '22 at 08:02
  • @Uwe I've tried to call the method on Mailer and Email and of course none of it worked. How can I get the SentMessage object? – Don40 Sep 08 '22 at 08:13
  • Could https://stackoverflow.com/questions/56693512/how-get-response-after-email-message-send-symfony-4-3-mailer-component help? `$mailer->send` itself does not return anything – Nico Haase Sep 08 '22 at 08:21

1 Answers1

4

https://github.com/symfony/symfony/pull/38517 provides a hint: don't use the Mailer, but the Transport itself. The Mailer class itself, when created manually, doesn't do that much more than calling send on the transport.

$transport = Transport::fromDsn("smtp://$department->email:$department->smtp_password@$department->smtp_host:$department->smtp_port");

$email = (new Email())
            ->subject($request->email_subject)
            ->from($department->email)
            ->to($request->email_reply_to)
            ->html($request->body);

$headers = $email->getHeaders();

$sentMessage = $transport->send($email);
Nico Haase
  • 11,420
  • 35
  • 43
  • 69