add unsubscribe and view mail on the web buttons
This commit is contained in:
parent
8bdd63afe2
commit
f095954a33
20 changed files with 289 additions and 224 deletions
|
|
@ -1,75 +0,0 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'pattern' => '/send-newsletter.json',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
$jsonRequest = file_get_contents("php://input");
|
||||
$data = json_decode($jsonRequest);
|
||||
|
||||
if (!$data || !isset($data->pageUri) || !isset($data->isTest)) {
|
||||
return json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Invalid request data. Required fields: pageUri, isTest.'
|
||||
]);
|
||||
}
|
||||
|
||||
$kirby = kirby();
|
||||
$emailPage = page('inscription')->childrenAndDrafts()->find($data->pageUri);
|
||||
|
||||
if (!$emailPage) {
|
||||
return json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'The specified page does not exist.'
|
||||
]);
|
||||
}
|
||||
|
||||
$recipients = $data->isTest
|
||||
? $kirby->users()->pluck('email', null, true)
|
||||
: page('inscription')->subscribers()->toStructure()->pluck('email', true, true);
|
||||
|
||||
if (empty($recipients)) {
|
||||
return json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'No recipients found.'
|
||||
]);
|
||||
}
|
||||
|
||||
$subject = $data->isTest
|
||||
? "[TEST] - " . $emailPage->title()->value()
|
||||
: $emailPage->title()->value();
|
||||
|
||||
$from = new \Kirby\Cms\User([
|
||||
'email' => 'info@actuel-inactuel.fr',
|
||||
'name' => 'actuel-inactuel',
|
||||
]);
|
||||
|
||||
try {
|
||||
foreach ($recipients as $recipient) {
|
||||
$kirby->email([
|
||||
'from' => $from,
|
||||
'to' => $recipient,
|
||||
'subject' => $subject,
|
||||
'template' => 'newsletter',
|
||||
'data' => [
|
||||
'body' => $emailPage->body()
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$data->isTest) {
|
||||
$emailPage->changeStatus('listed');
|
||||
}
|
||||
|
||||
return json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'Email(s) sent successfully.'
|
||||
]);
|
||||
} catch (Exception $error) {
|
||||
return json_encode([
|
||||
'status' => 'error',
|
||||
'message' => $error->getMessage() . " file " . $error->getFile() . " line " . $error->getLine()
|
||||
]);
|
||||
}
|
||||
}
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue