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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
@ -2,43 +2,42 @@
|
|||
|
||||
return [
|
||||
'pattern' => '/subscribe.json',
|
||||
'method' => 'POST',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
$jsonRequest = file_get_contents("php://input");
|
||||
$jsonRequest = file_get_contents('php://input');
|
||||
$email = Str::lower(json_decode($jsonRequest));
|
||||
|
||||
if (V::email($email)) {
|
||||
kirby()->impersonate('kirby');
|
||||
|
||||
$page = page("inscription");
|
||||
|
||||
$page = page('lettre');
|
||||
$subscribers = $page->subscribers()->yaml();
|
||||
|
||||
$emailExists = in_array(["email" => $email], $subscribers);
|
||||
$emailExists = in_array(['email' => $email], $subscribers);
|
||||
|
||||
if ($emailExists) {
|
||||
return [
|
||||
"status" => "error",
|
||||
"message" => "Cet email est déjà inscris."
|
||||
'status' => 'error',
|
||||
'message' => 'Cet email est déjà inscris.',
|
||||
];
|
||||
}
|
||||
|
||||
$newSubscriber = ["email" => $email];
|
||||
$newSubscriber = ['email' => $email];
|
||||
$subscribers[] = $newSubscriber;
|
||||
|
||||
|
||||
$page->update([
|
||||
'subscribers' => $subscribers
|
||||
'subscribers' => $subscribers,
|
||||
]);
|
||||
|
||||
return [
|
||||
"status" => "success",
|
||||
"message" => "Inscription réussie."
|
||||
'status' => 'success',
|
||||
'message' => 'lettre réussie.',
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
"status" => "error",
|
||||
"message" => "Email invalide."
|
||||
'status' => 'error',
|
||||
'message' => 'Email invalide.',
|
||||
];
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue