Sending emails with symfony+swiftmail, so that the messages are grouped by conversation

Easy enough to send emails with Symfony + SymfonyMailer messages out. The issue was once received in a program like Outlook, how do you group them into the same conversation.

Instead of flooding an inbox with a ton of emails, the emails would be grouped by conversation.

To accomplish this:

 <?php

// in the __constructor, autowire MailerInterface into $this->mailer

$subject = "something";
$somethingHashedThatRepresentsThisConversation = "someHash";

$messageId = "$somethingHashedThatRepresentsThisConversation@domain.tld";

       $email = (new Email())->from('from@example.com')
                              ->to($targetEmail)
                              ->subject($subject)
                              ->html($html)
                              ->text(strip_tags($html));

        $headers = $email->getHeaders();
        $headers->addIdHeader('Message-ID', $messageId);  // Original Message ID, I've found reusing it workss.
        $headers->addIdHeader('References', $messageId);  // Original MEssage ID to group by... could add others... I've found that this can reference itself.

        $email->setHeaders($headers);


        $this->mailer->send($email);

There you go.

PHP 8.0.x, Symfony 5.3


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *