try send emails through mailerSend API
This commit is contained in:
parent
04b04ccf6a
commit
cb5e08d3fc
5 changed files with 1776 additions and 33 deletions
|
|
@ -23,11 +23,15 @@
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
|
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
|
||||||
"getkirby/cms": "^4.5"
|
"getkirby/cms": "^4.5",
|
||||||
|
"php-http/guzzle7-adapter": "^1.1",
|
||||||
|
"nyholm/psr7": "^1.8",
|
||||||
|
"mailersend/mailersend": "^0.28.0"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
"getkirby/composer-installer": true
|
"getkirby/composer-installer": true,
|
||||||
|
"php-http/discovery": true
|
||||||
},
|
},
|
||||||
"optimize-autoloader": true
|
"optimize-autoloader": true
|
||||||
},
|
},
|
||||||
|
|
|
||||||
1641
composer.lock
generated
1641
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -6,7 +6,8 @@ return [
|
||||||
'menu' => require __DIR__ . '/menu.php',
|
'menu' => require __DIR__ . '/menu.php',
|
||||||
'css' => 'assets/css/panel.css',
|
'css' => 'assets/css/panel.css',
|
||||||
],
|
],
|
||||||
'email' => [
|
'mailerSendApiKey' => 'mlsn.0a9f20751951e3c2d130b1d6c3749b0a0f5b14f1c52da65a3369d658c736513c',
|
||||||
|
'email' => [
|
||||||
'transport' => [
|
'transport' => [
|
||||||
'type' => 'smtp',
|
'type' => 'smtp',
|
||||||
'host' => 'smtp.mailersend.net',
|
'host' => 'smtp.mailersend.net',
|
||||||
|
|
|
||||||
95
site/plugins/send-button/routes/save-send-newsletter.php
Normal file
95
site/plugins/send-button/routes/save-send-newsletter.php
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
set_time_limit(600);
|
||||||
|
|
||||||
|
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('lettre')->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('lettre')->subscribers()->toStructure()->pluck('email', ',', 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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sentEmails = [];
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
$batchSize = 5;
|
||||||
|
$pauseBetweenBatches = 1;
|
||||||
|
|
||||||
|
foreach (array_chunk($recipients, $batchSize) as $batch) {
|
||||||
|
foreach ($batch as $recipient) {
|
||||||
|
try {
|
||||||
|
$kirby->email([
|
||||||
|
'from' => $from,
|
||||||
|
'to' => $recipient,
|
||||||
|
'subject' => $subject,
|
||||||
|
'template' => 'newsletter',
|
||||||
|
'data' => [
|
||||||
|
'body' => $emailPage->body(),
|
||||||
|
'url' => (string) $emailPage->url(),
|
||||||
|
'recipient' => $recipient,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
$sentEmails[] = $recipient;
|
||||||
|
} catch (Exception $error) {
|
||||||
|
$errors[] = [
|
||||||
|
'email' => $recipient,
|
||||||
|
'message' => $error->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep($pauseBetweenBatches);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
]);
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use MailerSend\MailerSend;
|
||||||
|
use MailerSend\Helpers\Builder\Recipient;
|
||||||
|
use MailerSend\Helpers\Builder\EmailParams;
|
||||||
|
|
||||||
set_time_limit(600);
|
set_time_limit(600);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
@ -41,41 +45,41 @@ return [
|
||||||
? '[TEST] - ' . $emailPage->title()->value()
|
? '[TEST] - ' . $emailPage->title()->value()
|
||||||
: $emailPage->title()->value();
|
: $emailPage->title()->value();
|
||||||
|
|
||||||
$from = new \Kirby\Cms\User([
|
// $from = new \Kirby\Cms\User([
|
||||||
'email' => 'info@actuel-inactuel.fr',
|
// 'email' => 'info@actuel-inactuel.fr',
|
||||||
'name' => 'actuel-inactuel',
|
// 'name' => 'actuel-inactuel',
|
||||||
]);
|
// ]);
|
||||||
|
|
||||||
$sentEmails = [];
|
$sentEmails = [];
|
||||||
$errors = [];
|
$errors = [];
|
||||||
|
|
||||||
$batchSize = 5;
|
foreach ($recipients as $recipient) {
|
||||||
$pauseBetweenBatches = 1;
|
try {
|
||||||
|
|
||||||
foreach (array_chunk($recipients, $batchSize) as $batch) {
|
$mailersend = new MailerSend();
|
||||||
foreach ($batch as $recipient) {
|
|
||||||
try {
|
$recipients = [
|
||||||
$kirby->email([
|
new Recipient($recipient),
|
||||||
'from' => $from,
|
];
|
||||||
'to' => $recipient,
|
|
||||||
'subject' => $subject,
|
$emailParams = (new EmailParams())
|
||||||
'template' => 'newsletter',
|
->setFrom('info@actuel-inactuel.com')
|
||||||
'data' => [
|
->setFromName('actuel-inactuel')
|
||||||
'body' => $emailPage->body(),
|
->setRecipients($recipients)
|
||||||
'url' => (string) $emailPage->url(),
|
->setSubject($subject)
|
||||||
'recipient' => $recipient,
|
->setHtml($emailPage->body())
|
||||||
],
|
->setReplyTo('info@actuel-inactuel.com')
|
||||||
]);
|
->setReplyToName('actuel-inactuel');
|
||||||
$sentEmails[] = $recipient;
|
|
||||||
} catch (Exception $error) {
|
$mailersend->email->send($emailParams);
|
||||||
$errors[] = [
|
|
||||||
'email' => $recipient,
|
$sentEmails[] = $recipient;
|
||||||
'message' => $error->getMessage(),
|
} 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