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