No email variables are stored in the queue

Created on 4 September 2024, 3 months ago
Updated 12 September 2024, 2 months ago

Problem/Motivation

When creating a custom email if I want to use replacement tokens with the setVariables() method this information is not stored in the queue or applied in the sent email.

Steps to reproduce

Create a custom policy ex:
type: test
sub_type: queue

Create a body like this:
{{ test }}

Create an email like this:

$this->emailFactory->newTypedEmail('test', 'queue')
        ->setVariable('test','Custom text')
        ->send();

If you don't add the queue to the policy the email sent will have the text "Custom text"
If you add the queue element to the policy the email will have the text "{{ test }}"

Proposed resolution

Add the variables to the SymfonyMailerQueueItem class
And on the SymfonyMailerQueueWorker class when attempting to send the email

// Attempt to send the email. Considered done when successfully delivered.
    $email = $this->emailFactory->newTypedEmail(
      $item->type,
      $item->subType,
      ...$item->params,
    );

Add the variables if they exist, something like:

if ($item->variables) {
   $email->setVariables($item->variables);
}
🐛 Bug report
Status

Needs work

Version

1.1

Component

Code

Created by

🇵🇹Portugal kallado

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kallado
  • 🇵🇹Portugal kallado

    To add the variables to the SymfonyMailerQueueItem I think it shoul be somewere in QueueSendingEmailAdjuster
    probably here:

    if (!$email->isInQueue()) {
          $queue = $this->queueFactory->get(SymfonyMailerQueueWorker::QUEUE_NAME, TRUE);
          $item = new SymfonyMailerQueueItem(
            $email->getType(),
            $email->getSubType(),
            $email->getParams(),
            $this->configuration,
          );
          $queue->createItem($item);
    

    Where we could use the Email method getVariables() to retrieve the values

  • Status changed to Needs work 3 months ago
  • 🇩🇪Germany simonbaese Berlin

    Thanks for the report. I am traveling right now. Will be able to get to the issue next week.

  • 🇵🇹Portugal kallado

    @simonbaese Thanks I'm available to help if you need to just a lot of work at the moment to dive into this.

  • Assigned to simonbaese
  • 🇩🇪Germany simonbaese Berlin

    I checked the issue today. Building emails programmatically does not work correctly, as reported. Unfortunately, this applies not only to variables but also to other email properties. The issue stems from the Drupal Symfony Mailer overloading the responsibilities of the email object. Hence, the email object can not be serialized easily to pass data to the queue worker.

    Probably, the only way forward is to carry all the build properties to the queue worker and reinstate them again. This approach is slightly ugly and I will sleep on it.

    A workaround for now is to use an email builder plugin and delegate the assignment of variables to it.

    Bumping to major priority because this bug affects all emails built in the described way.

Production build 0.71.5 2024