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

View file

@ -109,12 +109,6 @@ async function send(event, isTest = false) {
}, 2000);
} else {
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);
}
} catch (error) {