Closed
Description
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
- Create a free MailJet account for using the MailJet API
- Create a new Symfony app
- Require both
symfony/mailer
andsymfony/mailjet-mailer
using composer - 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 - 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