Hello,
When I send emails using the PHP client, I randomly get 500 errors. I think it’s the Brevo platform that is unstable because it works if I wait a few moments and do exactly the same call.
My function is like this :
public function sendEmail(string $templateName, string $lang, array $data, array $emailConfig, array $files = [])
{
$templateId = $this->getTemplateId($templateName, $lang);
$params = array_merge($data, [
'siteHost' => $this->conf->siteHost,
'siteFullHost' => $this->conf->siteFullHost,
]);
$sendConfig = [
'templateId' => $templateId,
'params' => $params,
];
// Destinataires
if (isset($emailConfig['to']) && count($emailConfig['to']) != 0) {
$sendConfig['to'] = $emailConfig['to'];
}
if (isset($emailConfig['cc']) && count($emailConfig['cc']) != 0) {
$sendConfig['cc'] = $emailConfig['cc'];
}
if (isset($emailConfig['bcc']) && count($emailConfig['bcc']) != 0) {
$sendConfig['bcc'] = $emailConfig['bcc'];
}
// Fichiers attaches
if (count($files) != 0) {
$attachment = [];
foreach ($files as $file) {
$b64Doc = $this->fileToBase64($file['path']);
$attachment[] = [
'content' => $b64Doc,
'name' => $file['name'],
];
}
$sendConfig['attachment'] = $attachment;
}
// Envoi
$sendSmtpEmail = new SendSmtpEmail($sendConfig);
try {
$transactionalEmailsApiInstance = $this->getTransactionalEmailsApiInstance();
$result = $transactionalEmailsApiInstance->sendTransacEmail($sendSmtpEmail);
} catch (Exception $e) {
$this->logger->error("BrevoService sendEmail " . $e->getMessage());
echo $e->getMessage();
die();
}
}
So, I thought I would launch a recursive retry in case of an error. Something like this:
public function sendEmail(string $templateName, string $lang, array $data, array $emailConfig, array $files = [], int $retryNb = 0)
{
...
try {
...
} catch (Exception $e) {
$this->logger->error("BrevoService sendEmail " . $e->getMessage());
if(2 < $retryNb) {
echo $e->getMessage();
die();
} else {
wait(2s);
return sendEmail($templateName, $lang, $data, $files, $retryNb + 1);
}
}
}
Do you think this is a good idea? Could this cause other problems?
Thx,
Th.