batch email send
This commit is contained in:
parent
dde483ee20
commit
9b9e5a2ef7
1 changed files with 29 additions and 20 deletions
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
set_time_limit(600);
|
||||
|
||||
return [
|
||||
'pattern' => '/send-newsletter.json',
|
||||
'method' => 'POST',
|
||||
|
|
@ -26,7 +28,7 @@ return [
|
|||
|
||||
$recipients = $data->isTest
|
||||
? $kirby->users()->pluck('email', null, true)
|
||||
: page('lettre')->subscribers()->toStructure()->pluck('email', true, true);
|
||||
: page('lettre')->subscribers()->toStructure()->pluck('email', ',', true);
|
||||
|
||||
if (empty($recipients)) {
|
||||
return json_encode([
|
||||
|
|
@ -47,26 +49,33 @@ return [
|
|||
$sentEmails = [];
|
||||
$errors = [];
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
try {
|
||||
$kirby->email([
|
||||
'from' => $from,
|
||||
'to' => $recipient,
|
||||
'subject' => $subject,
|
||||
'template' => 'newsletter',
|
||||
'data' => [
|
||||
'body' => $emailPage->body(),
|
||||
'url' => (string) $emailPage->url(),
|
||||
'recipient' => $recipient,
|
||||
],
|
||||
]);
|
||||
$sentEmails[] = $recipient;
|
||||
} catch (Exception $error) {
|
||||
$errors[] = [
|
||||
'email' => $recipient,
|
||||
'message' => $error->getMessage(),
|
||||
];
|
||||
$batchSize = 5;
|
||||
$pauseBetweenBatches = 1;
|
||||
|
||||
foreach (array_chunk($recipients, $batchSize) as $batch) {
|
||||
foreach ($batch as $recipient) {
|
||||
try {
|
||||
$kirby->email([
|
||||
'from' => $from,
|
||||
'to' => $recipient,
|
||||
'subject' => $subject,
|
||||
'template' => 'newsletter',
|
||||
'data' => [
|
||||
'body' => $emailPage->body(),
|
||||
'url' => (string) $emailPage->url(),
|
||||
'recipient' => $recipient,
|
||||
],
|
||||
]);
|
||||
$sentEmails[] = $recipient;
|
||||
} catch (Exception $error) {
|
||||
$errors[] = [
|
||||
'email' => $recipient,
|
||||
'message' => $error->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
sleep($pauseBetweenBatches);
|
||||
}
|
||||
|
||||
if (!$data->isTest) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue