envoi des mails un par un pour ne pas partager toutes les adresses
This commit is contained in:
parent
b1923e8d4c
commit
aa237d8628
1 changed files with 58 additions and 29 deletions
|
|
@ -7,40 +7,69 @@ return [
|
|||
$jsonRequest = file_get_contents("php://input");
|
||||
$data = json_decode($jsonRequest);
|
||||
|
||||
$kirby = kirby();
|
||||
$emailPage = page("inscription")->childrenAndDrafts()->find($data->pageUri);
|
||||
if (!$data || !isset($data->pageUri) || !isset($data->isTest)) {
|
||||
return json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Invalid request data. Required fields: pageUri, isTest.'
|
||||
]);
|
||||
}
|
||||
|
||||
$to = $data->isTest ? $kirby->users() : page("inscription")->subscribers()->toStructure()->pluck("email", true, true);
|
||||
$subject = $data->isTest ? "[TEST] - " . (string) $emailPage->title() : (string) $emailPage->title();
|
||||
$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', true, 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 {
|
||||
if ($kirby->email([
|
||||
'from' => "info@actuel-inactuel.fr",
|
||||
'to' => $to,
|
||||
'cc' => 'contact@adrien-payet.fr',
|
||||
foreach ($recipients as $recipient) {
|
||||
$kirby->email([
|
||||
'from' => $from,
|
||||
'to' => $recipient,
|
||||
'subject' => $subject,
|
||||
'template' => 'newsletter',
|
||||
'data' => [
|
||||
"body" => $emailPage->body()
|
||||
'body' => $emailPage->body()
|
||||
]
|
||||
])->isSent()) {
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$data->isTest) {
|
||||
$emailPage->changeStatus("listed");
|
||||
$emailPage->changeStatus('listed');
|
||||
}
|
||||
|
||||
return json_encode([
|
||||
'status' => 'success',
|
||||
'message' =>
|
||||
'Email envoyé avec succès.'
|
||||
'message' => 'Email(s) sent successfully.'
|
||||
]);
|
||||
} else {
|
||||
return json_encode([
|
||||
"status" => "error",
|
||||
"message" => "Erreur lors de l'envoi de l'email.",
|
||||
]);
|
||||
}
|
||||
} catch (Exception $error) {
|
||||
return json_encode(['status' => 'error', 'message' => $error->getMessage()]);
|
||||
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