Skip to content

[symfony/mailjet-mailer] JSON payload for X-MJ-Vars only allows for a max depth of two (2) #47701

Closed
@nurtext

Description

@nurtext

Symfony version(s) affected

6.1.*

Description

When using der MailJet Mailer component using the MailJet API in conjunction with template variables, the MailJet component only allows for a max depth of two (2), thus we're getting the following error while trying to send emails:

JsonException
in vendor/symfony/mailjet-mailer/Transport/MailjetApiTransport.php (line 204)

199    private function castCustomHeader(string $value, string $type)
200   {
201        return match ($type) {
202            'bool' => filter_var($value, \FILTER_VALIDATE_BOOLEAN),
203            'int' => (int) $value,
204            'json' => json_decode($value, true, 2, \JSON_THROW_ON_ERROR), <--
205            'string' => $value,
206       };
207    }
208 }

When using the MailJet PHP API (https://github.com/mailjet/mailjet-apiv3-php) there's no limitation to the depth of the variable template payload.

How to reproduce

  1. Create a free MailJet account for using the MailJet API
  2. Create a new Symfony app
  3. Require both symfony/mailer and symfony/mailjet-mailer using composer
  4. Configure the MAILER_DSN to use the MailJet API transport as described in the documentation https://symfony.com/doc/current/mailer.html#using-a-3rd-party-transport
  5. Try to send an email that uses MailJet templates and add a multi-dimensional array for template variables

Example:

$templateId = 1337;

$templateVars = [
    'foo' => [
        'bar' => 'baz',
    ],
];

$email = (new Symfony\Component\Mime\Email())
    ->from('foo@localhost')
    ->to('bar@localhost')
    ->subject('Test email');
    
$email->text('Ignored, because using MailJet templates');

$email
    ->getHeaders()
    ->addTextHeader('X-MJ-TemplateLanguage', 'true')
    ->addTextHeader('X-MJ-TemplateID', $templateId)
    ->addTextHeader('X-MJ-Vars', \json_encode($templateVars));
    
$this->mailer->send($email);

Possible Solution

Remove the limitation that allows for only a max depth of 2 levels here: https://github.com/symfony/mailjet-mailer/blob/6.1/Transport/MailjetApiTransport.php#L204

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