Home > Software design >  How to get email ID when using Symfony Mailer?
How to get email ID when using Symfony Mailer?

Time:09-08

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

CodePudding user response:

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);
  • Related