newsletter : handle errors
This commit is contained in:
parent
72fff85a8c
commit
f76e3d9fb3
3 changed files with 33 additions and 20 deletions
|
|
@ -44,8 +44,11 @@ return [
|
|||
'name' => 'actuel-inactuel',
|
||||
]);
|
||||
|
||||
try {
|
||||
foreach ($recipients as $recipient) {
|
||||
$sentEmails = [];
|
||||
$errors = [];
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
try {
|
||||
$kirby->email([
|
||||
'from' => $from,
|
||||
'to' => $recipient,
|
||||
|
|
@ -57,23 +60,26 @@ return [
|
|||
'recipient' => $recipient,
|
||||
],
|
||||
]);
|
||||
$sentEmails[] = $recipient;
|
||||
} catch (Exception $error) {
|
||||
$errors[] = [
|
||||
'email' => $recipient,
|
||||
'message' => $error->getMessage(),
|
||||
];
|
||||
}
|
||||
|
||||
if (!$data->isTest) {
|
||||
$emailPage->changeStatus('listed');
|
||||
} else {
|
||||
$emailPage->changeStatus('unlisted');
|
||||
}
|
||||
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$data->isTest) {
|
||||
$emailPage->changeStatus('listed');
|
||||
} else {
|
||||
$emailPage->changeStatus('unlisted');
|
||||
}
|
||||
|
||||
return json_encode([
|
||||
'status' => empty($errors) ? 'success' : 'partial',
|
||||
'message' => empty($errors) ? 'All emails sent successfully.' : 'Certains emails n\'ont pas pu être envoyé.',
|
||||
'sent' => $sentEmails,
|
||||
'failed' => $errors,
|
||||
]);
|
||||
},
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue