script d'envoi de newsletter en masse terminé

This commit is contained in:
isUnknown 2025-02-06 17:28:09 +01:00
parent 2e718f61ae
commit c053ce2d27
2 changed files with 20 additions and 38 deletions

View file

@ -45,41 +45,31 @@ return [
? '[TEST] - ' . $emailPage->title()->value() ? '[TEST] - ' . $emailPage->title()->value()
: $emailPage->title()->value(); : $emailPage->title()->value();
// $from = new \Kirby\Cms\User([
// 'email' => 'info@actuel-inactuel.fr',
// 'name' => 'actuel-inactuel',
// ]);
$sentEmails = []; $sentEmails = [];
$errors = []; $errors = [];
$mailersend = new MailerSend(['api_key' => kirby()->option('mailerSendApiKey')]);
$bulkEmailParams = [];
foreach ($recipients as $recipient) { foreach ($recipients as $recipient) {
try { $bulkEmailParams[] = (new EmailParams())
->setFrom('info@actuel-inactuel.fr')
->setFromName('actuel-inactuel')
->setRecipients(new Recipient($recipient, 'abonné'))
->setSubject($subject)
->setHtml($emailPage->body())
->setReplyTo('info@actuel-inactuel.fr')
->setReplyToName('actuel-inactuel');
$mailersend = new MailerSend(['api_key' => kirby()->option('mailerSendApiKey')]); $sentEmails[] = $recipient;
}
$recipients = [ try {
new Recipient($recipient, 'abonné'), $response = $mailersend->bulkEmail->send($bulkEmailParams);
]; file_put_contents(__DIR__ . '/mailersend_log.json', json_encode($response, JSON_PRETTY_PRINT));
} catch (\Exception $e) {
$emailParams = (new EmailParams()) file_put_contents(__DIR__ . '/mailersend_errors.log', $e->getMessage());
->setFrom('info@actuel-inactuel.fr') $error[] = 'Error. See https://developers.mailersend.com/general.html#api-response for explanations : ' . $e->getMessage();
->setFromName('actuel-inactuel')
->setRecipients($recipients)
->setSubject($subject)
->setHtml($emailPage->body())
->setReplyTo('info@actuel-inactuel.fr')
->setReplyToName('actuel-inactuel');
$mailersend->email->send($emailParams);
$sentEmails[] = $recipient;
} catch (Exception $error) {
$errors[] = [
'email' => $recipient,
'message' => $error->getMessage(),
];
}
} }
if (!$data->isTest) { if (!$data->isTest) {
@ -90,9 +80,7 @@ return [
return json_encode([ return json_encode([
'status' => empty($errors) ? 'success' : 'partial', 'status' => empty($errors) ? 'success' : 'partial',
'message' => empty($errors) ? 'All emails sent successfully.' : 'Certains emails n\'ont pas pu être envoyé.', 'message' => empty($errors) ? 'All emails sent successfully.' : $errors,
'sent' => $sentEmails,
'failed' => $errors,
]); ]);
}, },
]; ];

View file

@ -109,12 +109,6 @@ async function send(event, isTest = false) {
}, 2000); }, 2000);
} else { } else {
let errorMessage = data.message || "Erreur inconnue"; let errorMessage = data.message || "Erreur inconnue";
if (data.failed && data.failed.length > 0) {
const failedEmails = data.failed.map((f) => f.email).join(", ");
errorMessage += `\nEmails non envoyés : ${failedEmails}`;
}
throw new Error(errorMessage); throw new Error(errorMessage);
} }
} catch (error) { } catch (error) {